
Queue is an abstract data type and a common data structure similar to stack . Queue is an ordered list in which insertions (Push) , deletion (Pop) operations are done at different ends . The end at which insertion is performed is known as rear end . The end at which deletion (Pop) operation is performed is known as front end .
Queue follows "First in First Out Principle" which implies that the element which is inserted first is deleted first from the queue . Queue can be implemented using arrays and linkedlists .
for example : Let 1,2,3,4,5 be the five elements inserted and the first element inserted is 1 and next element to be inserted is 2 and soon . To delete an element from the Queue , the first element to be deleted is 1 which follows First In First Out principle .
Also check : Implementation of Stacks using c++
In my previous post I discussed about implementation of stacks with Push , pop and top operations . In the similar way we perform Push , pop operations in Queue at two different ends .
If the pop operation is performed the first element is removed the vacancy is created . To fill the vacancy the elements in the queue are rearranged after an element is removed from the queue . "Queue Underflow " occurs when the pop operation is performed in an empty Queue .
Download the program source code . The above program is implemented in c++ using object oriented programming .
Also check : Search algorithm for Binary search Tree
If I have missed anything please get it to me through comments . Also share your logic and idea with us through comments . If you feel there can be a better way and logic to improve the program please comment below .
Queue follows "First in First Out Principle" which implies that the element which is inserted first is deleted first from the queue . Queue can be implemented using arrays and linkedlists .
for example : Let 1,2,3,4,5 be the five elements inserted and the first element inserted is 1 and next element to be inserted is 2 and soon . To delete an element from the Queue , the first element to be deleted is 1 which follows First In First Out principle .
image credits : wikipedia.org 
Applications of Queue :
The main application of Queue of Processors in the operating system . The first program or instruction that is to be passed must be executed first which follows Queue Principle .In my previous post I discussed about implementation of stacks with Push , pop and top operations . In the similar way we perform Push , pop operations in Queue at two different ends .
Push :
Push an element into the queue or Insert an element into the queue from the rear end . If the queue is full i.e we cannot insert any element into the queue and this situation is known as "Queue Overflow".Pop :
Delete an element or to pop an element form the queue at the front end . This operation is performed at the front end of the queue .If the pop operation is performed the first element is removed the vacancy is created . To fill the vacancy the elements in the queue are rearranged after an element is removed from the queue . "Queue Underflow " occurs when the pop operation is performed in an empty Queue .
Program for Queue and implementation using c++
/* Queue implementation using c++ */ #include<iostream> using namespace std; class queue { public : int *a,f,r,size; queue() { f=0; r=1; cout<<"\nEnter size of the Queue\n"; cin>>size; a = new int [size]; } int isempty(); int isfull(); int push(); int pop(); int display(); }; int queue :: isempty() { if(r==1) return 1; return 0; } int queue :: isfull() { if(r==(size1)) return 1; return 0; } int queue :: push() { if(isfull()) { cout<<"\n Queue Overflow\n"; return 0; } else { cout<<"\nEnter an Element\n"; cin>>a[++r]; cout<<"Inserted successfully\n"; } } int queue :: pop() { if(isempty()) { cout<<"\nQueue Underflow\n"; return 0; } for(int i=0;i<=r;i++) a[i]=a[i+1]; r; cout<<"Element deleted successfully\n"; } int queue :: display() { if(isempty()) { cout<<"\nQueue is empty  No element to display\n"; return 0; } else { for(int i=0;i<=r;i++) cout<<""<<a[i]<<" "; } } int main() { int i=0,s; queue q; while(i!=1) { cout<<"\n*********M E N U*********\n"; cout<<"\n1.Push\n2.Pop\n3.Display\n4.Exit\n"; cout<<"Enter option\n"; cin>>s; switch(s) { case 1: q.push(); break; case 2: q.pop(); break; case 3: q.display(); break; case 4: i=1; break; default : cout<<"\nEnter correct option\n"; break; } } return 0; }
Output of the program :
Download the program source code . The above program is implemented in c++ using object oriented programming .
Also check : Search algorithm for Binary search Tree
If I have missed anything please get it to me through comments . Also share your logic and idea with us through comments . If you feel there can be a better way and logic to improve the program please comment below .
public:
ReplyDeleteint *a,f,r,size;
Please, change to private:
private:
int *a,f,r,size;
public:
queue()...