{"id":767,"date":"2023-09-05T07:10:37","date_gmt":"2023-09-05T07:10:37","guid":{"rendered":"https:\/\/learnlearn.uk\/ibcs\/?page_id=767"},"modified":"2023-09-05T07:21:06","modified_gmt":"2023-09-05T07:21:06","slug":"circular-queue","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/","title":{"rendered":"Circular Queue"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Circular Queue<\/h2>\n<div class=\"tabcontent\">\n\n<p>&nbsp;<\/p>\n<h3>Circular Queue<a href=\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignright size-medium wp-image-762\" src=\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue-300x300.png 300w, https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue-150x150.png 150w, https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue-768x768.png 768w, https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/h3>\n<p>A circular queue is similar to a linear queue, but it uses a circular buffer to efficiently reuse empty slots when elements are dequeued. This prevents the queue from becoming full in the usual sense. Circular queues are often implemented using arrays or linked lists with wraparound logic.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Operations<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Circular Queue Operations<\/h3>\n<p>These are the same as linear queues:<\/p>\n<p><strong>Enqueue<\/strong><br \/>\nAdding an element to the back (also known as the rear or tail) of the queue.<\/p>\n<p><strong>Dequeue<\/strong><br \/>\nRemoving and returning the element from the front (also known as the head) of the queue.<\/p>\n<p><strong>Peek<\/strong><br \/>\nViewing the element at the front of the queue without removing it.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Demo<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Circular Queue Interactive Demonstration<\/h3>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Circular Queue Python Code<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">class<\/span> <span style=\"color: #00aa00; text-decoration: underline;\">CircularQueue<\/span>:\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">__init__<\/span>(<span style=\"color: #00aaaa;\">self<\/span>, max_size):\r\n        <span style=\"color: #00aaaa;\">self<\/span>.max_size = max_size\r\n        <span style=\"color: #00aaaa;\">self<\/span>.queue = [<span style=\"color: #00aaaa;\">None<\/span>] * max_size\r\n        <span style=\"color: #00aaaa;\">self<\/span>.front = <span style=\"color: #00aaaa;\">self<\/span>.rear = -<span style=\"color: #009999;\">1<\/span>\r\n\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">is_empty<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">self<\/span>.front == -<span style=\"color: #009999;\">1<\/span>\r\n\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">is_full<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">return<\/span> (<span style=\"color: #00aaaa;\">self<\/span>.rear + <span style=\"color: #009999;\">1<\/span>) % <span style=\"color: #00aaaa;\">self<\/span>.max_size == <span style=\"color: #00aaaa;\">self<\/span>.front\r\n\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">enqueue<\/span>(<span style=\"color: #00aaaa;\">self<\/span>, item):\r\n        <span style=\"color: #0000aa;\">if<\/span> <span style=\"color: #00aaaa;\">self<\/span>.is_full():\r\n            <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Queue is full\"<\/span>)\r\n        <span style=\"color: #0000aa;\">elif<\/span> <span style=\"color: #00aaaa;\">self<\/span>.is_empty():\r\n            <span style=\"color: #00aaaa;\">self<\/span>.front = <span style=\"color: #00aaaa;\">self<\/span>.rear = <span style=\"color: #009999;\">0<\/span>\r\n            <span style=\"color: #00aaaa;\">self<\/span>.queue[<span style=\"color: #00aaaa;\">self<\/span>.rear] = item\r\n        <span style=\"color: #0000aa;\">else<\/span>:\r\n            <span style=\"color: #00aaaa;\">self<\/span>.rear = (<span style=\"color: #00aaaa;\">self<\/span>.rear + <span style=\"color: #009999;\">1<\/span>) % <span style=\"color: #00aaaa;\">self<\/span>.max_size\r\n            <span style=\"color: #00aaaa;\">self<\/span>.queue[<span style=\"color: #00aaaa;\">self<\/span>.rear] = item\r\n\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">dequeue<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">if<\/span> <span style=\"color: #00aaaa;\">self<\/span>.is_empty():\r\n            <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Queue is empty\"<\/span>)\r\n        <span style=\"color: #0000aa;\">elif<\/span> <span style=\"color: #00aaaa;\">self<\/span>.front == <span style=\"color: #00aaaa;\">self<\/span>.rear:\r\n            item = <span style=\"color: #00aaaa;\">self<\/span>.queue[<span style=\"color: #00aaaa;\">self<\/span>.front]\r\n            <span style=\"color: #00aaaa;\">self<\/span>.front = <span style=\"color: #00aaaa;\">self<\/span>.rear = -<span style=\"color: #009999;\">1<\/span>\r\n            <span style=\"color: #0000aa;\">return<\/span> item\r\n        <span style=\"color: #0000aa;\">else<\/span>:\r\n            item = <span style=\"color: #00aaaa;\">self<\/span>.queue[<span style=\"color: #00aaaa;\">self<\/span>.front]\r\n            <span style=\"color: #00aaaa;\">self<\/span>.front = (<span style=\"color: #00aaaa;\">self<\/span>.front + <span style=\"color: #009999;\">1<\/span>) % <span style=\"color: #00aaaa;\">self<\/span>.max_size\r\n            <span style=\"color: #0000aa;\">return<\/span> item\r\n\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">size<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">if<\/span> <span style=\"color: #00aaaa;\">self<\/span>.is_empty():\r\n            <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #009999;\">0<\/span>\r\n        <span style=\"color: #0000aa;\">elif<\/span> <span style=\"color: #00aaaa;\">self<\/span>.front &lt;= <span style=\"color: #00aaaa;\">self<\/span>.rear:\r\n            <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">self<\/span>.rear - <span style=\"color: #00aaaa;\">self<\/span>.front + <span style=\"color: #009999;\">1<\/span>\r\n        <span style=\"color: #0000aa;\">else<\/span>:\r\n            <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">self<\/span>.max_size - (<span style=\"color: #00aaaa;\">self<\/span>.front - <span style=\"color: #00aaaa;\">self<\/span>.rear) + <span style=\"color: #009999;\">1<\/span>\r\n\r\n<span style=\"color: #aaaaaa; font-style: italic;\"># Example usage:<\/span>\r\n<span style=\"color: #0000aa;\">if<\/span> __name__ == <span style=\"color: #aa5500;\">\"__main__\"<\/span>:\r\n    queue = CircularQueue(<span style=\"color: #009999;\">5<\/span>)\r\n    queue.enqueue(<span style=\"color: #009999;\">1<\/span>)\r\n    queue.enqueue(<span style=\"color: #009999;\">2<\/span>)\r\n    queue.enqueue(<span style=\"color: #009999;\">3<\/span>)\r\n    queue.enqueue(<span style=\"color: #009999;\">4<\/span>)\r\n    queue.enqueue(<span style=\"color: #009999;\">5<\/span>)\r\n\r\n    <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Queue:\"<\/span>, queue.queue)\r\n    <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Dequeue:\"<\/span>, queue.dequeue())\r\n    <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Queue:\"<\/span>, queue.queue)\r\n    <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Is empty?\"<\/span>, queue.is_empty())\r\n    <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Is full?\"<\/span>, queue.is_full())\r\n    <span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Size:\"<\/span>, queue.size())\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Resources<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Resources<\/h3>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/docs.google.com\/presentation\/d\/1fOis4OyXRg7hnt1Fq_A23dwj-eehiWZBu3DOkJf3mNs\/edit?usp=sharing\">Presentation<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Circular Queue A circular queue is similar to a linear queue, but it uses a circular buffer to efficiently reuse empty slots when elements are dequeued. This prevents the queue from becoming full in the usual sense. Circular queues are often implemented using arrays or linked lists with wraparound logic. &nbsp; &nbsp; Circular Queue&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Circular Queue<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Circular Queue - IB Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Circular Queue - IB Computer Science\" \/>\n<meta property=\"og:description\" content=\"&nbsp; Circular Queue A circular queue is similar to a linear queue, but it uses a circular buffer to efficiently reuse empty slots when elements are dequeued. This prevents the queue from becoming full in the usual sense. Circular queues are often implemented using arrays or linked lists with wraparound logic. &nbsp; &nbsp; Circular Queue&hellip;&nbsp;Read More &raquo;Circular Queue\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/\" \/>\n<meta property=\"og:site_name\" content=\"IB Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-05T07:21:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue-300x300.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/\",\"name\":\"Circular Queue - IB Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\"},\"datePublished\":\"2023-09-05T07:10:37+00:00\",\"dateModified\":\"2023-09-05T07:21:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"IB Computer Science\",\"item\":\"https:\/\/learnlearn.uk\/ibcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Circular Queue\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"name\":\"IB Computer Science\",\"description\":\"- learnlearn..uk\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\",\"name\":\"IB Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"width\":300,\"height\":41,\"caption\":\"IB Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Circular Queue - IB Computer Science","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/","og_locale":"en_GB","og_type":"article","og_title":"Circular Queue - IB Computer Science","og_description":"&nbsp; Circular Queue A circular queue is similar to a linear queue, but it uses a circular buffer to efficiently reuse empty slots when elements are dequeued. This prevents the queue from becoming full in the usual sense. Circular queues are often implemented using arrays or linked lists with wraparound logic. &nbsp; &nbsp; Circular Queue&hellip;&nbsp;Read More &raquo;Circular Queue","og_url":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/","og_site_name":"IB Computer Science","article_modified_time":"2023-09-05T07:21:06+00:00","og_image":[{"url":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2023\/09\/ciruclar-queue-300x300.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/","url":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/","name":"Circular Queue - IB Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#website"},"datePublished":"2023-09-05T07:10:37+00:00","dateModified":"2023-09-05T07:21:06+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/ibcs\/circular-queue\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/ibcs\/circular-queue\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"IB Computer Science","item":"https:\/\/learnlearn.uk\/ibcs\/"},{"@type":"ListItem","position":2,"name":"Circular Queue"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/ibcs\/#website","url":"https:\/\/learnlearn.uk\/ibcs\/","name":"IB Computer Science","description":"- learnlearn..uk","publisher":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/ibcs\/#organization","name":"IB Computer Science","url":"https:\/\/learnlearn.uk\/ibcs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","contentUrl":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","width":300,"height":41,"caption":"IB Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/ibcs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"&nbsp; Circular Queue A circular queue is similar to a linear queue, but it uses a circular buffer to efficiently reuse empty slots when elements are dequeued. This prevents the queue from becoming full in the usual sense. Circular queues are often implemented using arrays or linked lists with wraparound logic. &nbsp; &nbsp; Circular Queue&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/767"}],"collection":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":5,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/767\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/767\/revisions\/779"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/media?parent=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}