Professional Documents
Culture Documents
2019-2020
College of Engineering
Mustansiriyah University
Page 1 of 8
: :القسم كلية الهندسة – الجامعة المستنصرية
الشعبة المرحلة :االسم
Introduction to queue
Queue is a data structure designed to operate in FIFO (First in First out)
context. In queue elements are inserted from rear end and get removed
from front end.
Queue class is container adapter. Container is an objects that hold data
of same type. Queue can be created from different sequence containers.
Container adapters do not support iterators therefore we cannot use them
for data manipulation. However they support push() and pop() member
functions for data insertion and deletion respectively.
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
Page 2 of 8
: :القسم كلية الهندسة – الجامعة المستنصرية
الشعبة المرحلة :االسم
rear++;
queue[rear] = val;
void Delete() {
return ;
} else {
front++;;
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
int main() {
int ch;
Page 3 of 8
: :القسم كلية الهندسة – الجامعة المستنصرية
الشعبة المرحلة :االسم
cout<<"4) Exit"<<endl;
do {
cin<<ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
In the above program, the function Insert() inserts an element into the
queue. If the rear is equal to n-1, then the queue is full and overflow is
displayed. If front is -1, it is incremented by 1. Then rear is incremented
by 1 and the element is inserted in index of rear. This is shown below −
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
return ;
Page 5 of 8
: :القسم كلية الهندسة – الجامعة المستنصرية
الشعبة المرحلة :االسم
}
else {
front++;;
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
The function main() provides a choice to the user if they want to insert,
delete or display the queue. According to the user response, the
appropriate function is called using switch. If the user enters an invalid
response, then that is printed. The code snippet for this is given below −
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
Page 7 of 8
: القسم: كلية الهندسة – الجامعة المستنصرية
الشعبة المرحلة االسم:
Page 8 of 8