IMPLEMENTATION OF QUEUE IN C++ WITH POINTER OBJECTS
#include<iostream.h>
#include<conio.h>
struct Product
{
int pid;
int rate;
Product *next;
void getInfo()
{
cout<<"Enter Product ID : ";
cin>>pid;
cout<<"Enter Product Rate : ";
cin>>rate;
next=NULL;
}
void showInfo()
{
cout<<pid<<" : "<<rate<<" --> "<<next<<endl;
}
}*temp,*front,*rear;
void Push()
{
temp=new Product;
temp->getInfo();
if(front==NULL)
{
temp->next=NULL;
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
cout<<"\n....Data Added Successfully....\n";
}
void Peep()
{
temp=front;
while(temp!=NULL)
{
temp->showInfo();
temp=temp->next;
}
cout<<"<------Queue displayed----->\n";
}
void Pop()
{
if(front==NULL)
cout<<"\n......Queue is empty.....\n";
else
{
temp=front;
front=front->next;
cout<<"\nThe Following node is deleted...\n";
temp->showInfo();
delete temp;
}
cout<<"\n....Now the Queue remains.....\n";
Peep();
}
void menu()
{
cout<<"===============================\n";
cout<<"\t0. Exit\n";
cout<<"\t1. Push to Queue (Add)\n";
cout<<"\t2. Peep Queue (Display)\n";
cout<<"\t3. Pop from Queue (Delete)\n";
cout<<"\tChoose : ";
}
void project()
{
front=NULL;
int ch;
do
{
clrscr();
menu();
cin>>ch;
switch(ch)
{
case 1: Push(); break;
case 2: Peep(); break;
case 3: Pop(); break;
}
getch();
}while(ch);
}
void main()
{
project();
}
#include<conio.h>
struct Product
{
int pid;
int rate;
Product *next;
void getInfo()
{
cout<<"Enter Product ID : ";
cin>>pid;
cout<<"Enter Product Rate : ";
cin>>rate;
next=NULL;
}
void showInfo()
{
cout<<pid<<" : "<<rate<<" --> "<<next<<endl;
}
}*temp,*front,*rear;
void Push()
{
temp=new Product;
temp->getInfo();
if(front==NULL)
{
temp->next=NULL;
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
cout<<"\n....Data Added Successfully....\n";
}
void Peep()
{
temp=front;
while(temp!=NULL)
{
temp->showInfo();
temp=temp->next;
}
cout<<"<------Queue displayed----->\n";
}
void Pop()
{
if(front==NULL)
cout<<"\n......Queue is empty.....\n";
else
{
temp=front;
front=front->next;
cout<<"\nThe Following node is deleted...\n";
temp->showInfo();
delete temp;
}
cout<<"\n....Now the Queue remains.....\n";
Peep();
}
void menu()
{
cout<<"===============================\n";
cout<<"\t0. Exit\n";
cout<<"\t1. Push to Queue (Add)\n";
cout<<"\t2. Peep Queue (Display)\n";
cout<<"\t3. Pop from Queue (Delete)\n";
cout<<"\tChoose : ";
}
void project()
{
front=NULL;
int ch;
do
{
clrscr();
menu();
cin>>ch;
switch(ch)
{
case 1: Push(); break;
case 2: Peep(); break;
case 3: Pop(); break;
}
getch();
}while(ch);
}
void main()
{
project();
}
No comments
Post your comments