stack functions using a c++ program

#include
#include
#include
//#include
template
class stack
{
struct node
{
object data;
node *next;
} *TOS;
public:
stack ()
{
TOS=NULL;
}
void create ();
void push ();
void pop ();
void display ();
};
template
void stack :: push ()
{
object item;
node *newnode;
char ch;
new node;
cout<<"\n Enter the item to be inserted:\t"; cin>>item;
newnode = new node;
newnode->data=item;
newnode->next=NULL;
if (TOS==NULL)
TOS = newnode;
else
{
new node->next=TOS;
TOS=new node;
}
}
template
void stack ::pop()
{
object item;
node*nodeptr;
if(TOS==NULL)
cout<<"\n stack is empty"; else { nodeptr=TOS; TOS=TOS->next;
cout<<"\n deleted item is:\t"<data;
delete nodeptr;
}
}
template
void stack ::display()
{
object item;
node*nodeptr;
if(TOS==NULL)
cout<<"\n stack is empty\n"; else { cout<<"\n item in the stack are\n"; nodeptr=TOS; while(nodeptr) { cout<<"\t"<data;
nodeptr=nodeptr->next;
}
}
}
void main()
{
stack stack;
int opt;
char ch;
clrscr();
do
{
cout<<"\n\n\t\t Stack operation"; cout<<"\n 1.push"; cout<<"\n 2.pop"; cout<<"\n 3.display"; cout<<"\n 4.exit"; cout<<"\n enter your choice:\t"; cin>>opt;
switch (opt)
{
case 1:stack.push();
break;
case 2:stack.pop();
break;
case 3:stack.display();
break;
case 4:
exit(0);
default:cout<<"\n invalid choice";
}
}
while(opt!=4);
}

Comments