{"id":387,"date":"2023-06-28T11:36:59","date_gmt":"2023-06-28T11:36:59","guid":{"rendered":"http:\/\/localhost\/embeddedwala\/?p=387"},"modified":"2023-07-27T04:13:10","modified_gmt":"2023-07-27T04:13:10","slug":"circular-queue-basics","status":"publish","type":"post","link":"https:\/\/embeddedwala.com\/beta\/blogs\/digital-communication\/circular-queue-basics\/","title":{"rendered":"Circular Queue Basics"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"387\" class=\"elementor elementor-387\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-top-section elementor-element elementor-element-69a04e4 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"69a04e4\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-8ea46c3\" data-id=\"8ea46c3\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-inner-section elementor-element elementor-element-792292e elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"792292e\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-88ba1fc\" data-id=\"88ba1fc\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-74c373f elementor-widget elementor-widget-text-editor\" data-id=\"74c373f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<h2><span data-preserver-spaces=\"true\">What is Circular Queue?<\/span><\/h2><p><span data-preserver-spaces=\"true\">The\u00a0<strong>Circular Queue<\/strong>\u00a0is the Extention of a queue that is a linear user-defined data structure. The circular queue is often used in situations where elements can be added to or removed from the queue at any point in a program, rather than following a predetermined enqueue or dequeue order.<\/span><\/p><p><span data-preserver-spaces=\"true\">A circular queue is often used in embedded systems to manage memory. It is also utilized in the process management of real-time operating systems\u00a0<strong>(RTOS)<\/strong>\u00a0for scheduling.<\/span><\/p><p><span data-preserver-spaces=\"true\"><strong>A circular queue typically includes four main functions:<\/strong><\/span><\/p><ol><li><span data-preserver-spaces=\"true\">Checking if the queue is empty<\/span><\/li><li><span data-preserver-spaces=\"true\">Checking if the queue is full<\/span><\/li><li><span data-preserver-spaces=\"true\">Adding an element to the queue (enqueue)<\/span><\/li><li><span data-preserver-spaces=\"true\">Removing an element from the queue (dequeue)<\/span><\/li><\/ol><p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-390\" src=\"http:\/\/localhost\/embeddedwala\/wp-content\/uploads\/2023\/06\/Circular-Queue.gif\" alt=\"\" width=\"1152\" height=\"648\" \/><\/p><p>\u00a0<\/p><p><strong>This code is an implementation of a circular queue in C<\/strong>. The circular queue is a data structure that behaves like a queue but has a fixed size and wraps around when the end of the queue is reached.<\/p><p>The circular queue is defined using a struct called\u00a0<strong><code>CircularQueue<\/code><\/strong>, which has an array\u00a0<code>data<\/code>\u00a0to store the elements, and two pointers\u00a0<code>front<\/code>\u00a0and\u00a0<code>end<\/code>\u00a0to keep track of the front and end of the queue. The\u00a0<strong><code>init_circular_queue<\/code>\u00a0<\/strong>function initializes the queue by setting the front and end pointers to -1 and clearing the data array.<\/p><p>The\u00a0<strong><code>is_queue_full<\/code>\u00a0<\/strong>function checks if the queue is full by checking if the front pointer is at the beginning of the array and the end pointer is at the end of the array, or if the end pointer is just before the front pointer (which indicates that the queue has overlapped). The\u00a0<strong><code>is_queue_empty<\/code>\u00a0<\/strong>function checks if the queue is empty by checking if the front pointer is -1.<\/p><p>The\u00a0<strong><code>add_element_in_queue<\/code>\u00a0<\/strong>function adds an element to the queue by first checking if the queue is full. If the queue is not full, it updates the end pointer to the next index (using modulo arithmetic to wrap around to the beginning of the array if necessary) and stores the element at that index. The\u00a0<strong><code>remove_element_from_queue<\/code>\u00a0<\/strong>function removes an element from the queue by first checking if the queue is empty. If the queue is not empty, it updates the front pointer to the next index (using modulo arithmetic to wrap around to the beginning of the array if necessary).<\/p><p>The\u00a0<strong><code>print_full_queue<\/code>\u00a0<\/strong>function is a helper function that prints the entire contents of the queue.<\/p><p>Finally, the\u00a0<strong><code>main<\/code>\u00a0<\/strong>function initializes the circular queue using\u00a0<strong><code>init_circular_queue<\/code>\u00a0<\/strong>and then adds elements to it using\u00a0<strong><code>add_element_in_queue<\/code>\u00a0<\/strong>and prints the full queue using\u00a0<strong><code>print_full_queue<\/code><\/strong>. It adds 14 elements to the queue (the size of the queue is 5), which causes the circular queue to wrap around and overwrite the oldest elements in the queue.<\/p><div><pre>#include &lt;stdio.h&gt;\n#include &lt;stdint.h&gt;<br \/>\n\/* \n * Size of Queue in Bytes\n *\/\n#define SIZE_OF_QUEUE_IN_BYTES       5\n\n\/*\n * Structure holds the circular queue\n *\/\ntypedef struct CircularQueue {\n    char data[SIZE_OF_QUEUE_IN_BYTES];\n    int front;\n    int end;\n} CircularQueue;\n\n\/*\n * Global variable holding instance of the circular queue\n *\/\nCircularQueue Circular_Queue;\n\nvoid init_circular_queue(CircularQueue * queue) {\n    queue-&gt;front = -1;\n    queue-&gt;end   = -1;\n    for(int idx = 0; idx &lt; SIZE_OF_QUEUE_IN_BYTES; idx++) {\n        queue-&gt;data[idx] = 0;\n    }    \n}\n<br \/>\/*<br \/> * There are two ways to find out if the circular queue is full.<br \/> * 1) If linearly the queue is full with no overlap<br \/> * 2) If the circular queue has overlapped. <br \/> *\/\nbool is_queue_full(CircularQueue queue) {\n    if(((queue.front == 0) &amp;&amp; (queue.end == SIZE_OF_QUEUE_IN_BYTES)) ||\n        (queue.front == queue.end + 1)) {\n        return true;\n    }\n    return false;\n}\n<br \/>\/*<br \/> * Simply by making sure with a placeholder value (-1) if the queue is empty. <br \/> *\/\nbool is_queue_empty(CircularQueue queue) {\n    if(queue.front == -1) {\n        return true;\n    }\n    return false;\n}\n\nbool add_element_in_queue(CircularQueue * queue, int data) {\n    if(is_queue_full(*queue) == false) {\n        if(queue-&gt;front == -1) {\n            queue-&gt;front = 0;\n        }\n        queue-&gt;end = (queue-&gt;end + 1) % SIZE_OF_QUEUE_IN_BYTES;\n        queue-&gt;data[queue-&gt;end] = data;    \n        return true;\n    }\n    return false;\n}\n\nbool remove_element_from_queue(CircularQueue * queue) {\n    if(is_queue_empty(*queue) == false) {\n        if(queue-&gt;front == queue-&gt;end) {\n            queue-&gt;front = -1;\n            queue-&gt;end   = -1;\n        } else {\n            queue-&gt;front = (queue-&gt;front + 1) % SIZE_OF_QUEUE_IN_BYTES;\n        }\n        return true;\n    }\n    return false;\n}\n\nvoid print_full_queue(CircularQueue queue) {\n    for(int idx = 0; idx &lt; SIZE_OF_QUEUE_IN_BYTES; idx++) {\n        printf(\"%d \", queue.data[idx]);\n    }\n    printf(\"\\n\");\n}\n\nint main()\n{\n    init_circular_queue(&amp;Circular_Queue);\n    for(int idx = 1; idx &lt; 15; idx++) {\n        add_element_in_queue(&amp;Circular_Queue, idx);\n        print_full_queue(Circular_Queue);\n    }\n    return 0;\n}\n<\/pre><\/div>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>What is Circular Queue? The&nbsp;Circular Queue&nbsp;is the Extention of a queue that is a linear&#8230;<\/p>\n","protected":false},"author":1,"featured_media":388,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[90,91],"class_list":["post-387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-digital-communication","tag-circular-queue","tag-queue"],"acf":[],"views":372,"featured_image_urls":{"full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"thumbnail":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",150,101,false],"medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",300,202,false],"medium_large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"1536x1536":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"2048x2048":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"tp-image-grid":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"covernews-slider-full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"covernews-slider-center":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"covernews-featured":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",520,350,false],"covernews-medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",505,340,false],"covernews-medium-square":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",371,250,false],"_nx_notification_thumb":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Circular-quene-c-banner.png",100,67,false]},"author_info":{"display_name":"embeddedwala","author_link":"https:\/\/embeddedwala.com\/beta\/author\/embeddedwala\/"},"category_info":"<a href=\"https:\/\/embeddedwala.com\/beta\/category\/blogs\/digital-communication\/\" rel=\"category tag\">Digital Communication<\/a>","tag_info":"Digital Communication","comment_count":"0","_links":{"self":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":5,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":6198,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/387\/revisions\/6198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media\/388"}],"wp:attachment":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}