C++ PROGRAM FOR IMPLEMENTATION OF STACK USING POINTERS


#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};
void stack::push()
{
node *newlink;
newlink=new node;
cout<<"enter the pointer element to push"<<endl;
cin>>newlink->data;
newlink->next=top;
top=newlink;
cout<<"push operation is succcessfull";
}
void stack::pop()
{
if(top==NULL)
cout<<"stack is empty";
else
{
node *t;
t=top;
top=top->next;
cout<<"poped the elemets"<<t->data;
delete t;
cout<<"\n pop operation is successfull";
}
}
void stack::display()
{
node *curt=top;
if(top==NULL)
cout<<"stack empty";
else
{
cout<<"the elements in the stack is\n";
//cout<<curt->data;
//getch();
while(curt!= NULL)
{
cout<<curt->data;
curt = curt->next;
}
}
}
void main()
{
stack s1;
int ch=0;
clrscr();
cout<<"stack operation using pointer";
cout<<"\n 1.PUSH \n 2.POP \n 3.DISPLAY \n4.EXIT\n";
while(ch!=4)
{
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
s1.push();
break;
case 2:
s1.pop();
break;
case 3:
s1.display();
break;
case 4:
cout<<"program terminated";
break;
}
}
getch();
}


OUTPUT:
STACK OPERATION USING POINTER
 1.PUSH
 2.POP
 3.DISPLAY
4.EXIT
enter the choice:1
enter the pointer element to push
20
push operation is succcessfullenter the choice:1
enter the pointer element to push
30
push operation is succcessfullenter the choice:2
poped the elemets30
 pop operation is successfullenter the choice:3
the elements in the stack is
20enter the choice:















No comments:

Post a Comment