Definition of Queue :
Queue
is an abstract data type that serves as a collection of elements. queue works
with two operations which are enqueue
and dequeue these
operations
follow the principle of FIFO .
operations
follow the principle of FIFO .
What is Abstract Data Type
Examples:
·
Queue of people in front
of bank
·
Queue of requests to
processor
·
Queue of people in front
of immigration office
What Are Enqueue And Dequeue In Queue:
Enqueue
adds an element to the queue at the back/rear end of the queue and dequeue
removes the element from the front end of the queue.
What is FIFO Principle of Queue:
FIFO is the principle of queue whose means “first in first
out”. That is anything which we add first in queue can be remove/return at the
first and anything which we add at the last in queue can be remove/return at
last. That is which come first in queue will be served at first.
Example of queue with source code:
This source code has a queue a size 10.this code add numbers in queue ,
remove them from queue and display the numbers of queue.
#include<iostream>
using namespace std;
enum{ MAX = 10 };
class queue{
private:
int item[MAX];
int rear;
int front;
public:
queue()
{
rear = -1;
front = 0;
}
void enqueue(int a)
{
if (rear == MAX - 1){
cout << "queue is full"
<< endl;
}
else{
item[++rear] = a;
}
}
void dequeue()
{
if (rear == front)
{
cout << "queue is
empty" << endl;
}
else
{
cout << item[front]
<< " has removed from queue
"<< endl;
}
}
void display()
{ if (rear
== front)
{
cout << "queue is empty"
<< endl;
}
else
{
for (int i = front; i <= rear; i++)
cout << item[i] <<
endl;
}
}
};
int main(){
queue q;
int choice, data;
while (1){
cout << "\n1. enqueue\n2. dequeue\n3.
display all element\n4. quit";
cout << "\nenter your choice: ";
cin >> choice;
switch (choice){
case 1:
cout << "\nenter data:
";
cin >> data;
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
break;
}
}
return 0;
}