{"id":356,"date":"2021-02-07T07:52:31","date_gmt":"2021-02-07T07:52:31","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/how-can-i-implement-queue-in-php\/"},"modified":"2024-10-28T03:45:22","modified_gmt":"2024-10-28T03:45:22","slug":"how-can-i-implement-queue-in-php","status":"publish","type":"programming_with_php","link":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/how-can-i-implement-queue-in-php\/","title":{"rendered":"How can i implement QUEUE in PHP?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the circular queue, we can use an empty data cell again when we remove the cell value using a dequeue. Another important thing is we can not add value before the end of the head index( for example, if my array size is 5 and the head point to an index of 0, then we need to empty the array index 4. Which means we can add 4 value when the array size is 5). We can use this queue implementation in banking software( ex: give tokens to customers and call them according to that token). Here is the complete code below:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$devisor  = 5;\n$data_array = [];\n$head = 0;\n$tail = 0;\n\nfunction enqueue( $item = NULL ){\n\tglobal $head, $tail, $data_array, $devisor;\n\tif(empty($item)){\n\t\techo 'invalid function call. Need to pass item as a function parametter';\n\t\treturn;\n\t}\n\t$tail = $tail % $devisor;\n\t$next_tindex = $tail + 1;\n\tif( $head == $next_tindex % $devisor){\n\t\techo 'Queue is full';\n\t\treturn;\n\t}else{\n\t\t$data_array[$tail] = $item;\n\t\t$tail++;\n\t}\n\treturn $data_array;\n}\n\nfunction dequeue(){\n\tglobal $head,$tail,$devisor,$data_array;\n\tif($tail == $head){\n\t\techo 'Queue is empty';\n\t\treturn -1;\n\t}\n\t\n\t$item = $data_array[$head];\n\t$head = ++$head % $devisor;\n\treturn $item;\n}\n\necho '&lt;pre>';\nprint_r(enqueue(1));\necho '&lt;\/pre>';\necho '&lt;pre>';\nprint_r(dequeue());\necho '&lt;\/pre>';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A data structure called QUEUE operates data based on First In, First Out. When we talk about queue implementation, we need to implement circular QUEUE. We can use this QUEUE implementation in banking software.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"class_list":["post-356","programming_with_php","type-programming_with_php","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"A data structure called QUEUE operates data based on First In, First Out. When we talk about queue implementation, we need to implement circular QUEUE. We can use this QUEUE implementation in banking software.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/programming_with_php"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=356"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}