Friday, 28 October 2016
Queue
- Enqueue >> Insert/Add
- Dequeue >> Remove
FIFO Rules, That is First in First out
------------
------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************** | |
QUEUE C++ | |
AUTHOR URI:https://mostafa.itnishi.com | |
******************************************************/ | |
#include<iostream> | |
#include<conio.h> | |
#include<stdlib.h> | |
#define SIZE 5 | |
using namespace std; | |
int q[SIZE],front=0,rear=0; | |
int main() | |
{ | |
int ch; | |
void enqueue(); | |
void dequeue(); | |
void display(); | |
while(1) | |
{ | |
cout<<"\n 1. add element"; | |
cout<<"\n 2. remove element"; | |
cout<<"\n 3.display"; | |
cout<<"\n 4.exit"; | |
cout<<"\n enter your choice:"; | |
cin>>ch; | |
switch(ch) | |
{ | |
case 1: | |
enqueue(); | |
break; | |
case 2: | |
dequeue(); | |
break; | |
case 3: | |
display(); | |
break; | |
case 4: | |
exit(0); | |
default: | |
cout<<"\n invalid choice"; | |
} | |
} | |
} | |
void enqueue() | |
{ | |
int no; | |
if (rear==SIZE && front==0) | |
cout<<"queue is full"; | |
else | |
{ | |
cout<<"enter the num:"; | |
cin>>no; | |
q[rear]=no; | |
} | |
rear++; | |
} | |
void dequeue() | |
{ | |
int no,i; | |
if (front==rear) | |
cout<<"queue is empty"; | |
else | |
{ | |
no=q[front]; | |
front++; | |
cout<<"\n"<<no<<" -removed from the queue\n"; | |
} | |
} | |
void display() | |
{ | |
int i,temp=front; | |
if (front==rear) | |
cout<<"the queue is empty"; | |
else | |
{ | |
cout<<"\n element in the queue:"; | |
for(i=temp;i<rear;i++) | |
{ | |
cout<<q[i]<<" "; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************** | |
QUEUE C++ | |
AUTHOR URI:https://mostafa.itnishi.com | |
******************************************************/ | |
#include<iostream> | |
#define maxi 1000 | |
using namespace std; | |
class qqueue{ | |
int a[maxi]; | |
int f,r; | |
public: | |
void initialize(); | |
void enqueue(int val); | |
void dequeue(); | |
void display(); | |
}; | |
void qqueue::initialize(){ | |
cout<<"Empty Queue is created :D"; | |
f=r=-1; | |
} | |
void qqueue::enqueue(int val){ | |
if(r==maxi-1){ | |
cout<<"\nQueue Overflow.."; | |
} | |
else if(f==-1 && r==-1){ | |
f=r=1; | |
a[r]=val; | |
} | |
else{ | |
r++; | |
a[r]=val; | |
} | |
} | |
void qqueue::dequeue(){ | |
if(f==-1 && r==-1){ | |
cout<<"\nQueue is already empty.."; | |
} | |
else if(f==r){ | |
cout<<"\nThe dequeued element is.."<<a[f]; | |
f=r=-1; | |
} | |
else{ | |
cout<<"\nThe dequeued element is.."<<a[f]; | |
f++; | |
} | |
} | |
void qqueue::display(){ | |
int x; | |
if(f==-1 && r==-1){ | |
cout<<"\nThe Queue is empty.."; | |
return; | |
} | |
cout<<"\nThe Contents of the Queue is.."; | |
for(x=f;x<=r;x++){ | |
cout<<a[x]<<" "; | |
} | |
} | |
int main() | |
{ | |
qqueue q; | |
q.initialize(); | |
int ch,val; | |
cout<<"\nChoice: \n1)Enqueue \n2)Dequeue \n3)Display \n4)Exit"; | |
while(1){ | |
cout<<"\nEnter your choice.."; | |
cin>>ch; | |
if(ch==1){ | |
cout<<"\nEnter the element to be enqueued.."; | |
cin>>val; | |
q.enqueue(val); | |
} | |
else if(ch==2){ | |
q.dequeue(); | |
} | |
else if(ch==3){ | |
q.display(); | |
} | |
else{ | |
break; | |
} | |
} | |
return 0; | |
} |