circular queue

Solutions on MaxInterview for circular queue by the best coders in the world

showing results for - "circular queue"
Elena
03 Apr 2016
1// Circular Queue implementation in C++
2
3#include <iostream>
4#define SIZE 5 /* Size of Circular Queue */
5
6using namespace std;
7
8class Queue {
9   private:
10  int items[SIZE], front, rear;
11
12   public:
13  Queue() {
14    front = -1;
15    rear = -1;
16  }
17  // Check if the queue is full
18  bool isFull() {
19    if (front == 0 && rear == SIZE - 1) {
20      return true;
21    }
22    if (front == rear + 1) {
23      return true;
24    }
25    return false;
26  }
27  // Check if the queue is empty
28  bool isEmpty() {
29    if (front == -1)
30      return true;
31    else
32      return false;
33  }
34  // Adding an element
35  void enQueue(int element) {
36    if (isFull()) {
37      cout << "Queue is full";
38    } else {
39      if (front == -1) front = 0;
40      rear = (rear + 1) % SIZE;
41      items[rear] = element;
42      cout << endl
43         << "Inserted " << element << endl;
44    }
45  }
46  // Removing an element
47  int deQueue() {
48    int element;
49    if (isEmpty()) {
50      cout << "Queue is empty" << endl;
51      return (-1);
52    } else {
53      element = items[front];
54      if (front == rear) {
55        front = -1;
56        rear = -1;
57      }
58      // Q has only one element,
59      // so we reset the queue after deleting it.
60      else {
61        front = (front + 1) % SIZE;
62      }
63      return (element);
64    }
65  }
66
67  void display() {
68    // Function to display status of Circular Queue
69    int i;
70    if (isEmpty()) {
71      cout << endl
72         << "Empty Queue" << endl;
73    } else {
74      cout << "Front -> " << front;
75      cout << endl
76         << "Items -> ";
77      for (i = front; i != rear; i = (i + 1) % SIZE)
78        cout << items[i];
79      cout << items[i];
80      cout << endl
81         << "Rear -> " << rear;
82    }
83  }
84};
85
86int main() {
87  Queue q;
88
89  // Fails because front = -1
90  q.deQueue();
91
92  q.enQueue(1);
93  q.enQueue(2);
94  q.enQueue(3);
95  q.enQueue(4);
96  q.enQueue(5);
97
98  // Fails to enqueue because front == 0 && rear == SIZE - 1
99  q.enQueue(6);
100
101  q.display();
102
103  int elem = q.deQueue();
104
105  if (elem != -1)
106    cout << endl
107       << "Deleted Element is " << elem;
108
109  q.display();
110
111  q.enQueue(7);
112
113  q.display();
114
115  // Fails to enqueue because front == rear + 1
116  q.enQueue(8);
117
118  return 0;
119}
queries leading to this page
the circular queue is that in whichwhy use circular queuecircular queue and linear queuecircular queue implementation using arraythrough what is circular queue implementedwhat are circular queue usedimplement a circular queuecreate a circular queue in cdesign circular queuecircular queue array implementationcircular queue implementation in ccreate a circular queuecircular queue operationsdeletion in circular queuehow to create a circular queue in cuse of circular queue in data structurecircular queue enqueue algorithmthe concept of a circular queue iscases of circular queueadd elements to circular queue 5b 5d operator for circular queue implementationcircular queue ccircular queue with examplea circular queue is also known ascircular queue algorithmcircular queue is also known ascircular queue c codecircular queue is used forcircular queue code in ccircular queue qusetionin circular queue which of the following is correctcircular queue enqueuewhat is used to make circular queueimplement a circular queue using an arraycircular queues using arraycircular queue in cwhen does a circular queue become fullcircular queue usesa circular queue is better than a simple queuecircular queue display functionimplementation of circular queuecircular queue stlcircular queue dequeuehow is circular queue implementedoperations used in circular queuewrite a program to implement a circular queue circular queue using circular queuecircular queue is sometimes known as 3adisplay function for circular queueimplement circular queuewhat is the need of circular queuewrite a program using functions for implementation of circular queuein a circular queue 2cwhich of the following circular queue using queuecircular queue array basedby which of the following a circular queue can be createdwhy circular queue is requireddefine circular queueexplain circular queue what are the advantages of circular queue 3flimitations of circular queuewhat is a circular queuecircular queue in c 2b 3dcircular queue addressingimplementation of circular queue cwhich is another name for the circular queue circular queue is known asdequeue in circular queuecircular queues using arrayscircular queue in data structurecircular queue examplewhat is circular queuehow to break a circular queuealgorithm of circular queuecircular queue using stlcircular queue benefitsrepresentation of circular queuecircular queue indexingcircular queue definitionneed of circular queueconcept of circular queue queue and circular queueexplain the queue full condition for circular queue 3fcircular queue c 2bwhat is need of circular queuecircular queue using circuilar queuecircular queue number of elementsapplications of circular queuedesign a circular queuecircular queue implementation in data structurewhat is the use of circular queuewhy to use a circular queue instead of a simple queue 3fa circular queue is implementedthe circular queuethis type of queue is also called 3acircular queue example in ccircular queue implementationenqueue in circular queuecreates a circular queueimplementation of circular queue in ccircular queuecircular queue fron operating using circular queuecircular queue deletionwhat is circular queue explain with examplethe circular queue is that queue in whichalgorithm to implement circular queuewhen circular queue is fullchange queue into circular queuewhy circular queue is neededhow do circular queue workdequeue circular queuecircular queue using arraydisplay circular queue in ccircular queue codewhy are circular queue usedwhat are the uses of circular queuewhat is the need for a circular queuethe circular queue also called 3acircular queue program in chow to implmenet circular queuecircular queue data structureimplementation of circular queue using arrayexplain about circular queues implementation 3fcircular queue formulacircular queue enqueue in carray is implemented as a circular queuecircular queue is also known as 3acircular queue