Monday, 25 August 2014

Queue using Linked List

#include "stdafx.h"
#include<iostream>
using namespace std;
class Queue
{
private:
Queue *first,*last;
int data;
Queue *link;
public:
Queue()
{
link=NULL;
first=NULL;
last=NULL;
}
void insert_values(int value)
{
Queue *ptr=NULL;
ptr=new Queue;
if(ptr==NULL)
{
cout<<"\nQueue is Full\n";
}
if(first==NULL)
{
first=ptr;
first->data=value;
first->link=NULL;
last=first;
}
last->link=ptr;
last=ptr;
last->data=value;
last->link=NULL;
}
int remove_values()
{
if(first==NULL)
{
cout<<"Queue is Empty:\n";
return -1;
}
Queue*ptr=first;
first=first->link;
int value=ptr->data;
delete ptr;
return value;
}
~Queue()
{
Queue *ptr=first;
while(first!=NULL)
{
first=first->link;
delete ptr;
ptr=first;
}
}
};
int main()
{
Queue s1;
int choice,value;
do
{
cout<<"\n\n\t\t\t-----Main Menu-----\n\n\t\tPlease select your choice(1-3)\n\n1)Insert\n2)Remove\n3)Exit\n";
cin>>choice;
if(choice==1)
{
cout<<"\nPlease enter an Integer:\n";
cin>>value;
s1.insert_values(value);
}
else if(choice==2)
{
cout<<"\n\n\t\tRemoved value is  "<<s1.remove_values()<<"  ";
}
else if(choice==3)
{
exit(0);
}
else
{
cout<<"\nInvalid choice!!!Try again...\n";
}
}
while(choice!=3);
return 0;
}

No comments:

Post a Comment