IMPLEMENTATION OF STACK WITH POINTER OBJECTS OF CLASS.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct Product
{
int id;
char name[20];
Product *next;
};
class Stack
{
Product *top;
public:
Stack()
{
top=NULL;
}
void Push();
void Peep();
void Find();
void Pop();
};
void Stack::Push()
{
Product *ptr = new Product;
cout<<"==========================\n";
cout<<"Enter Product Details.....\n";
cout<<"==========================\n";
cout<<"Product Id : ";
cin>>ptr->id;
cout<<"Product name : ";
gets(ptr->name);
ptr->next=NULL;
if(top==NULL)
{
top=ptr;
cout<<"\nProduct Added Successfully....\n";
}
else
{
ptr->next=top;
top=ptr;
cout<<"\nProduct Added Successfully....\n";
}
}
void Stack::Peep()
{
Product *ptr=top;
while(ptr!=NULL)
{
cout<<"==========================\n";
cout<<"PRODUCT INFORMATION\n";
cout<<"==========================\n";
cout<<"Product ID : "<<ptr->id<<endl;
cout<<"Product Name : "<<ptr->name<<endl;
cout<<"==========================\n\n";
ptr=ptr->next;
}
}
void Stack::Find()
{
Product *ptr=top;
int s,pos=0,flag=0;
if(ptr==NULL)
{
cout<<"Stack is Empty....\nNoting to Search...\n";
}
else
{
cout<<"==========================\n";
cout<<"SEARCHING PRODUCT\n";
cout<<"==========================\n";
cout<<"Enter Product id to search: ";
cin>>s;
while(ptr!=NULL)
{
if(s==ptr->id)
{
pos++;
cout<<"Product ID found at pos..."<<pos<<" from top."<<endl;
flag++;
cout<<"==========================\n";
cout<<"PRODUCT INFORMATION\n";
cout<<"==========================\n";
cout<<"Product ID : "<<ptr->id<<endl;
cout<<"Product Name : "<<ptr->name<<endl;
cout<<"==========================\n\n";
break;
}
else
{
pos++;
ptr=ptr->next;
}
}
if(flag==0)
cout<<"\nNo Such Data Found....\n";
}
}
void Stack::Pop()
{
Product *ptr;
ptr=top;
top=top->next;
if(ptr==NULL)
{
cout<<"Stack is Empty....\nNoting to Delete...\n";
}
else
{
cout<<"==========================\n";
cout<<"DELETED PRODUCT DETAILS\n";
cout<<"==========================\n";
cout<<"Product ID : "<<ptr->id<<endl;
cout<<"Product Name : "<<ptr->name<<endl;
cout<<"==========================\n\n";
delete ptr;
}
}
void main()
{
Stack q;
int n;
do
{
clrscr();
cout<<"=====================\n";
cout<<" STACK OPERATIONS\n";
cout<<"=====================\n";
cout<<"0. Exit................"<<endl;
cout<<"1. Add................."<<endl;
cout<<"2. Display............."<<endl;
cout<<"3. Search.............."<<endl;
cout<<"4. Delete.............."<<endl;
cout<<"Enter your choice: ";
cin>>n;
clrscr();
switch(n)
{
case 1: q.Push(); break;
case 2: q.Peep(); break;
case 3: q.Find(); break;
case 4: q.Pop(); break;
}
getch();
}while(n);
}
#include<conio.h>
#include<stdio.h>
struct Product
{
int id;
char name[20];
Product *next;
};
class Stack
{
Product *top;
public:
Stack()
{
top=NULL;
}
void Push();
void Peep();
void Find();
void Pop();
};
void Stack::Push()
{
Product *ptr = new Product;
cout<<"==========================\n";
cout<<"Enter Product Details.....\n";
cout<<"==========================\n";
cout<<"Product Id : ";
cin>>ptr->id;
cout<<"Product name : ";
gets(ptr->name);
ptr->next=NULL;
if(top==NULL)
{
top=ptr;
cout<<"\nProduct Added Successfully....\n";
}
else
{
ptr->next=top;
top=ptr;
cout<<"\nProduct Added Successfully....\n";
}
}
void Stack::Peep()
{
Product *ptr=top;
while(ptr!=NULL)
{
cout<<"==========================\n";
cout<<"PRODUCT INFORMATION\n";
cout<<"==========================\n";
cout<<"Product ID : "<<ptr->id<<endl;
cout<<"Product Name : "<<ptr->name<<endl;
cout<<"==========================\n\n";
ptr=ptr->next;
}
}
void Stack::Find()
{
Product *ptr=top;
int s,pos=0,flag=0;
if(ptr==NULL)
{
cout<<"Stack is Empty....\nNoting to Search...\n";
}
else
{
cout<<"==========================\n";
cout<<"SEARCHING PRODUCT\n";
cout<<"==========================\n";
cout<<"Enter Product id to search: ";
cin>>s;
while(ptr!=NULL)
{
if(s==ptr->id)
{
pos++;
cout<<"Product ID found at pos..."<<pos<<" from top."<<endl;
flag++;
cout<<"==========================\n";
cout<<"PRODUCT INFORMATION\n";
cout<<"==========================\n";
cout<<"Product ID : "<<ptr->id<<endl;
cout<<"Product Name : "<<ptr->name<<endl;
cout<<"==========================\n\n";
break;
}
else
{
pos++;
ptr=ptr->next;
}
}
if(flag==0)
cout<<"\nNo Such Data Found....\n";
}
}
void Stack::Pop()
{
Product *ptr;
ptr=top;
top=top->next;
if(ptr==NULL)
{
cout<<"Stack is Empty....\nNoting to Delete...\n";
}
else
{
cout<<"==========================\n";
cout<<"DELETED PRODUCT DETAILS\n";
cout<<"==========================\n";
cout<<"Product ID : "<<ptr->id<<endl;
cout<<"Product Name : "<<ptr->name<<endl;
cout<<"==========================\n\n";
delete ptr;
}
}
void main()
{
Stack q;
int n;
do
{
clrscr();
cout<<"=====================\n";
cout<<" STACK OPERATIONS\n";
cout<<"=====================\n";
cout<<"0. Exit................"<<endl;
cout<<"1. Add................."<<endl;
cout<<"2. Display............."<<endl;
cout<<"3. Search.............."<<endl;
cout<<"4. Delete.............."<<endl;
cout<<"Enter your choice: ";
cin>>n;
clrscr();
switch(n)
{
case 1: q.Push(); break;
case 2: q.Peep(); break;
case 3: q.Find(); break;
case 4: q.Pop(); break;
}
getch();
}while(n);
}
No comments
Post your comments