IMPLEMENTATION OF STACK 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;
}
}*top,*start,*temp;

void Push()
{
temp=new Product;
temp->getInfo();
if(start==NULL)
{
temp->next=NULL;
start=top=temp;
}
else
{
temp->next=top;
top=temp;
}
cout<<"\n....Data Added Successfully....\n";
}

void Peep()
{
while(temp!=NULL)
{
temp->showInfo();
temp=temp->next;
}
cout<<"<------Stack displayed----->\n";
temp=top;
}

void Pop()
{
if(top==NULL)
cout<<"\n......Stack is empty.....\n";
else
{
top=top->next;
cout<<"\nThe Following node is deleted...\n";
temp->showInfo();
delete temp;
temp=top;
}
cout<<"\n....Now the Stack remains.....\n";
Peep();
}

void menu()
{
cout<<"===============================\n";
cout<<"\t0. Exit\n";
cout<<"\t1. Push to Stack (Add)\n";
cout<<"\t2. Peep Stack (Display)\n";
cout<<"\t3. Pop from Stack (Delete)\n";
cout<<"\tChoose : ";
}

void project()
{
start=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

Powered by Blogger.