Code:
#include<iostream>
//#include<stdlib.h>
using namespace std;
class node{
int value;
node *nxt;
public:
void setvalue(int v){ value = v; }
int getvalue(){ return value; }
void setnxt(node* n){ nxt = n; }
node* getnxt(){ return nxt; }
};
class myQueue{
node* front;
node* rear;
int noE;
public:
myQueue(){
front = NULL; rear = NULL; noE=0;
}
void enqueue(int d){
node* newnode = new node();
newnode->setvalue(d);
newnode->setnxt(NULL);
if(rear == NULL )
front = newnode;
else
rear->setnxt(newnode);
rear = newnode;
noE++;
}
int dequeue() {
int x= front->getvalue();
node *p = front;
if(front == rear)
front=rear=NULL;
else
front = front->getnxt();
delete p;
noE--;
return x;
}
int isEmpty(){
return noE == 0 ;
}
int sz(){
return noE;
}
int frontvalue(){
return front->getvalue();
}
};
main(){
myQueue q1;
int option=0;
int value;
while(1){
system("cls");
cout<<"\n\n\t ===== Queue operations =====";
cout<<"\n\n\t1. Enqueue value";
cout<<"\n\n\t2. Dequeue value";
cout<<"\n\n\t3. Display Front value";
cout<<"\n\n\t4. No. of values in the Queue";
cout<<"\n\n\t5. Exit";
cout<<"\n\n\tEnter your choice: ";
cin>>option;
switch(option){
case 1:
cout<<"\n\n\tEnter value: ";
cin>>value;
q1.enqueue(value);
cout<<"\n\n\tValue enqueue successfully\n\n\t";
system("pause");
break; //case 1 for enqueue
case 2:
if(q1.isEmpty()){
cout<<"\n\n\tQueue is empty.\n\n\t";
}
else{
cout<<"\n\n\tDequeue value: "<<q1.dequeue();
cout<<"\n\n\t";
}
system("pause");
break; //case 2 for pop
case 3:
if(q1.isEmpty()){
cout<<"\n\n\tQueue is empty.\n\n\t";
}
else{
cout<<"\n\n\tFront value: "<<q1.frontvalue();
cout<<"\n\n\t";
}
system("pause");
break; //case 2 for pop
case 4:
if(q1.isEmpty()){
cout<<"\n\n\tQueue is empty.\n\n\t";
}
else{
cout<<"\n\n\tNo. of values in the Queue: "<<q1.sz();
cout<<"\n\n\t";
}
system("pause");
break;
case 5:
exit(0);
break;
}///switch
}///while
system("pause");
}///main
Outpuy:
0 Comments