Docstoc

stack2

Document Sample
stack2 Powered By Docstoc
					#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
  int num;
  public:
    stack *next;
void getdata()
{
  cout<<"Enter the number\n";
  cin>>num;
}
void putdata()
{
  cout<<"\n"<<num;
}
};
stack *top;
void push();
void pop();
void print();
main()
{clrscr();
while(1)
{
cout<<"\n1.PUSH\n2.POP\n3.PRINT\n4.EXIT\nEnter the choice:";
int n;
cin>>n;
switch(n)
{
case 1:push();break;
case 2:pop();break;
case 3:print();break;
case 4:exit(1);break;
default:cout<<"\nEnter the correct choice:";break;
}
}
}
void push()
{
if(top==NULL)
{
top=new stack;
top->getdata();
top->next=NULL;
}
else
{
stack *temp1=new stack;
temp1->getdata();
temp1->next=top;
top=temp1;
}
}
void pop()
{
if(top==NULL)
{
cout<<"\nNOT POSSIBLE\n";
return ;
}
else
{
stack *temp1=new stack;
temp1=top;
top=top->next;
delete(temp1);
}
}
void print()
{
stack *temp1=new stack;
temp1=top;
while(temp1!=NULL)
{
temp1->putdata();
temp1=temp1->next;
}
/*while(top!=NULL)
{
   top->putdata();
   top=top->next;
}
  */
}

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:0
posted:10/17/2012
language:
pages:2
Description: C programming, C programming