{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/circularQueues","result":{"data":{"blog":{"frontmatter":{"title":"CIRCULAR QUEUES","thumbnail":"blog36","date":"January 4, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1W6fgHvki-TSCzfG-kFJohu_wyN0nC0Dc/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study why to use circular queue, solution\n                to the problem faced by an ordinary queue, operations and C++\n                code file for implementation of circular queue.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Need of circular queues?</h4>\n              <div class=\"m-2\">\n                <p>\n                  Consider a situation in oridinary queue where the queue is\n                  full and you start deleting some items(not completely). Now if\n                  you want to insert new items then you get an error saying\n                  queue is full.\n                </p>\n                <p>\n                  But why? It's because we check that if rear index is equal to\n                  one less than the size of the queue then it's a queue full\n                  condition. Even though there are rooms at the front which can\n                  be alotted for more data, due to this one condition there will\n                  be wastage of memory.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>How to overcome this problem?</h4>\n              <div class=\"m-2\">\n                <p>\n                  <strong>Shift the elements. </strong>As and when the dequeue\n                  operation is performed, we can shift the elements towards\n                  front such that there will be rooms empty at the rear index\n                  which can be used to fill. But shifting operation is expensive\n                  and it's a worst case operation in this solution.\n                </p>\n                <p>\n                  There is an easiar way for implementing the queue that is to\n                  change the values of front and rear in dequeue and enqueue\n                  operations respectively by incrementing them by one and taking\n                  modula with size of the queue(You can clearly understand this\n                  in code).\n                </p>\n                <p>\n                  In this way, an ordinary queue becomes a circular one. We can\n                  use a count variable to keep track of no. of items in the\n                  queue.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Standard Operations</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Enqueue:</strong> Insert a new item at the rear end of\n                  the queue. Increment the rear value by one and take modula\n                  with the size. If there is no place then queue is in overflow\n                  state.\n                </li>\n                <li>\n                  <strong>Dequeue:</strong> Remove an item from the front end of\n                  the queue. Increment the front value by one and take modula\n                  with the size. If there are no elements then the queue is in\n                  underflow state.\n                </li>\n                <li>\n                  <strong>isEmpty:</strong> To check whether queue is empty or\n                  not.\n                </li>\n                <li>\n                  <strong>isFull:</strong> To check if queue is full or not.\n                </li>\n                <li>\n                  <strong>Display</strong> Display the items in the queue.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/queue\">Ordinary Queues</a>\n              |\n              <a href=\"/blogs/priorityQueues\">Priority Queues</a>\n              |\n              <a href=\"/blogs/queueUsingLinkedLists\"\n                >Queue Using Linked Lists</a\n              >\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study why to use circular queue, solution\n                to the problem faced by an ordinary queue, operations and C++\n                code file for implementation of circular queue.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Need of circular queues?</h4>\n              <div class=\"m-2\">\n                <p>\n                  Consider a situation in oridinary queue where the queue is\n                  full and you start deleting some items(not completely). Now if\n                  you want to insert new items then you get an error saying\n                  queue is full.\n                </p>\n                <p>\n                  But why? It's because we check that if rear index is equal to\n                  one less than the size of the queue then it's a queue full\n                  condition. Even though there are rooms at the front which can\n                  be alotted for more data, due to this one condition there will\n                  be wastage of memory.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>How to overcome this problem?</h4>\n              <div class=\"m-2\">\n                <p>\n                  <strong>Shift the elements. </strong>As and when the dequeue\n                  operation is performed, we can shift the elements towards\n                  front such that there will be rooms empty at the rear index\n                  which can be used to fill. But shifting operation is expensive\n                  and it's a worst case operation in this solution.\n                </p>\n                <p>\n                  There is an easiar way for implementing the queue that is to\n                  change the values of front and rear in dequeue and enqueue\n                  operations respectively by incrementing them by one and taking\n                  modula with size of the queue(You can clearly understand this\n                  in code).\n                </p>\n                <p>\n                  In this way, an ordinary queue becomes a circular one. We can\n                  use a count variable to keep track of no. of items in the\n                  queue.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Standard Operations</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Enqueue:</strong> Insert a new item at the rear end of\n                  the queue. Increment the rear value by one and take modula\n                  with the size. If there is no place then queue is in overflow\n                  state.\n                </li>\n                <li>\n                  <strong>Dequeue:</strong> Remove an item from the front end of\n                  the queue. Increment the front value by one and take modula\n                  with the size. If there are no elements then the queue is in\n                  underflow state.\n                </li>\n                <li>\n                  <strong>isEmpty:</strong> To check whether queue is empty or\n                  not.\n                </li>\n                <li>\n                  <strong>isFull:</strong> To check if queue is full or not.\n                </li>\n                <li>\n                  <strong>Display</strong> Display the items in the queue.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/queue\">Ordinary Queues</a>\n              |\n              <a href=\"/blogs/priorityQueues\">Priority Queues</a>\n              |\n              <a href=\"/blogs/queueUsingLinkedLists\"\n                >Queue Using Linked Lists</a\n              >\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M27%2092c-7%205-6%2018%202%2018%203%200%208-5%206-7-1-1-2-1-3%201l-3%201v-8c1-3%203-3%202%200%200%202%200%202%202%202%204%200%204-7%200-8-3-1-3-1-6%201m11%200l-1%2010v8h5c5%200%205%200%206-3v-3l2%202%202%203h2c2-1%202-2%201-4v-6c3-6%201-9-7-8-4%200-4%200-4%204-1%203-1%203-1-1s0-4-2-4-3%201-3%202m25-1c-7%204-6%2019%201%2019%202%200%207-4%207-6s-2-2-4%200c-4%204-5%200-3-6%201-4%203-4%202-1%200%202%200%202%203%202%203-1%203-1%203%203%200%207%206%2010%2011%206%202-2%203-3%203-9%200-8%200-8-2-8-3%200-3%200-3%207%200%205-1%207-2%207l-1-7c0-7%200-7-2-7-3%200-3%200-3%204v4l-1-4c-1-4-6-6-9-4m25%200l-1%2010-1%209h9c7%200%209-1%209-2l1-1%202-1c2%200%202%200%202%202%200%201%201%202%203%202s2-1%201-10c-2-12-7-13-11-2-3%2010-4%2011-4%208%200-2-1-2-3-2h-3v-7c0-6%200-7-2-7l-2%201m32%200c-4%200-4%200-4%203l-1%209c0%207%200%207%202%207s3-1%203-3c0-4%201-4%203%200%201%203%201%203%204%202%202-1%202-2%200-4v-5c4-6%201-10-7-9m23%200c-5%202-7%2011-3%2016%202%203%203%203%206%203%205-1%205-1%206-8%200-8-3-13-9-11m29%200c-4%200-5%202-5%2013v6h4c6%200%209-1%209-3s-1-2-4-2c-5%200-5-2%200-3%204-1%203-3-1-4-4%200-4-2%201-3%204%200%205-1%205-3s-2-2-9-1m28%200c-4%200-4%200-4%204l-1%204v-4c0-4%200-4-2-4-3%200-3%200-3%204%200%206-1%2010-3%2010l-1-7c1-7%201-7-2-7-2%200-3%203-3%2011%200%206%202%208%206%208s6-2%207-8l1-4v12h4c6%200%209-1%209-3s-1-2-4-2c-5%200-5-2%200-3%205%200%204-4%200-4-6%200-5-2%201-3%203%200%204-1%204-2-1-3-2-3-9-2m-46%202v16c2%202%207%201%2010-1%203-4%203-17%200-17-2%200-2%201-3%207-1%209-3%209-3%200%200-6%200-7-2-7l-2%202m57%200c-2%204-1%207%202%209%204%202%204%204%200%204h-3c-2%204%207%205%2010%202s3-5-1-8l-3-5%201%201c1%202%204%202%205%200%201-5-8-7-11-3m57%209c-3%203-5%2013-3%2017%201%202%204%203%205%201%201-1-1-9-3-10-4-1%202-9%208-9%207%200%209%205%206%2011-2%205-2%208%200%209%204%201%207-8%204-9l-1-4c0-8-10-12-16-6m31%204l-1%208v7h17l-5%205a66%2066%200%2000-19%2037l-2%2010v1l6-7c7-9%2017-15%2017-10%200%202%201%203%203%203l2-2-1-3h3c3%200%202%201%200%204l-2%204v3h-17v7c0%206%200%207-2%208-3%202-3%203%200%203%202%200%203%204%201%206l-6%201c-9%200-11-4-7-9l2-5c0-3-8%206-8%2010-1%203-1%203-10%204-8%200-8%200-8%202s1%203%205%203c5%200%206%200%204%205-1%202%201%202%2048%202h49l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2-1-2c-7-1-7-4-3-12%207-13%207-17%200-22-4-3-5-4-5-8l-1-4c-1-1-1%201-1%204%200%205%200%205-2%204-3-1-19%200-23%201l-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7m-31%2027l1%209h12v-18h-13v9m55%2016c0%201-1%202-3%202s-2%201-2%204v3h6c9%200%209%200%208%207l1%208v2l-1%203c0%204%200%204%205%204%207%201%208%200%208-5l-1-6-2-4-2-5-1-3h-1v-2l-3-5c-1-2-3-3-7-4l-5-2v3m-52%207l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%204-2%203-2-2-1-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/ab1932ca0166af90462ce21503395bd5/ee604/blog36.png","srcSet":"/static/ab1932ca0166af90462ce21503395bd5/69585/blog36.png 200w,\n/static/ab1932ca0166af90462ce21503395bd5/497c6/blog36.png 400w,\n/static/ab1932ca0166af90462ce21503395bd5/ee604/blog36.png 800w,\n/static/ab1932ca0166af90462ce21503395bd5/f3583/blog36.png 1200w,\n/static/ab1932ca0166af90462ce21503395bd5/e4d72/blog36.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"circularQueues","thumbnail":"thumbnails/blog36.png"}},"staticQueryHashes":["2987289216","63159454"]}