What is Queue ?
Queue is an abstract data structure, somewhat similar to Stacks.Unlike stacks, a queue is open at both its ends.
Sequence of Elements : FIFO First Come First Out
Pointers : 2 Pointers
Implementation of Queue Program
/* Implementation of Queue: */ #include<iostream.h> #include<stdlib.h> #include<conio.h> #define size 5 struct queue { int queue_box[size]; int front,rear; }Queue_call; int queueFull() { if(Queue_call.rear>=size-1) return 1; else return 0; } int queueEmpty() { if((Queue_call.front==-1)||(Queue_call.front>Queue_call.rear)) return 1; else return 0; } int insert_item(int item) { if(Queue_call.front==-1) Queue_call.front++; Queue_call.queue_box[++Queue_call.rear]=item; return Queue_call.rear; } int delete_item() { int item; item=Queue_call.queue_box[Queue_call.front]; Queue_call.front++; cout<<"\nThe delete_item item is "<<item; return Queue_call.front; } void display() { int i; for(i=Queue_call.front;i<=Queue_call.rear;i++) cout<<" "<<Queue_call.queue_box[i]; } void main() { int ch,item; clrscr(); Queue_call.front=-1; Queue_call.rear=-1; cout<<"\n Queue Implementation"; do { cout<<"\n---------------\n Main Menu"; cout<<"\n1:Insert \n2:Delete \n3:Display \n4:Clear \n5:Exit"; cout<<"\nSelect Operation:"; cin>>ch; switch(ch) { case 1: if(queueFull()) { cout<<"Cannot insert the element"; } else { cout<<"Enter the number to be inserted:"; cin>>item; insert_item(item); } break; case 2: if(queueEmpty()) { cout<<"\nQueue is Empty"; } else { delete_item(); } break; case 3: if(queueEmpty()) {cout<<"\nQueue Is Empty";} else {display();} break; case 4: clrscr(); break; case 5: cout<<"\nEnd of Program..."; } }while(ch!=5); getch(); }
Output
(Visited 96 times, 1 visits today)