Last active
August 29, 2015 13:56
-
-
Save xXFracXx/8921242 to your computer and use it in GitHub Desktop.
D) Queues [Array]
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 using array | |
#include<iostream.h> | |
#include<stdio.h> | |
#include<conio.h> | |
#include<stdlib.h> | |
#include<ctype.h> | |
class queue | |
{ | |
public: | |
int q[5],front,rear,x,result; | |
void enterque(); | |
void deleteque(); | |
void display(); | |
queue() | |
{ | |
front=0; | |
rear=0; | |
} | |
}; | |
void queue::enterque() | |
{ | |
if(rear>=5) | |
cout<<"\n QUEUE OVERFLOW \n"; | |
else | |
{ | |
cout<<"\nEnter the number to be inserted: "; | |
cin>>x; | |
rear++; | |
q[rear]=x; | |
cout<<"\nNumber pushed in the queue:"<<q[rear]; | |
} | |
} | |
void queue::deleteque() | |
{ | |
if(rear==0) | |
cout<<"Queue underflow"; | |
else | |
{ | |
if(front==rear) | |
{ | |
front=0; | |
rear=0; | |
} | |
else | |
front++; | |
} | |
cout<<"\nDeleted element is:"; | |
result=q[front]; | |
cout<<result; | |
} | |
void queue::display() | |
{ | |
if(rear==0) | |
cout<<"\nQueue underflow\n"; | |
else | |
cout<<"\nContents of queue is:"; | |
{ | |
for(int i=front+1;i<=rear;i++) | |
cout<<q[i]<<"\t"; | |
} | |
} | |
int main() | |
{ | |
int c; | |
queue que; | |
do | |
{ | |
cout<<"\n 1. Insertion\n 2. Deletion\n 3. Display\n 4. Exit "; | |
cout<<"\nEnter your choice:"; | |
cin>>c; | |
switch(c) | |
{ | |
case 1: | |
que.enterque(); | |
break; | |
case 2: | |
que.deleteque(); | |
break; | |
case 3: | |
que.display(); | |
break; | |
case 4: | |
exit(0); | |
default: | |
cout<<"\nInvalid choice!!\n"; | |
} | |
}while(c<4); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment