#include <iostream>
#include <conio>
class node
{
public:
int info;
node* next;
node(int val,node* ptr=0)
{
info=val;
next=ptr;
}
};
class stack
{
node* top;
public:
stack()
{
top=0;
}
bool isOverflow();
bool isUnderflow();
void push(int val);
void pop();
void display();
};
bool stack::isUnderflow()
{
return (top==0);
}
void stack::push(int val)
{
if(top==0)
{
top=new node(val);
cout<<"Element successfully pushed";
}
else
{
top=new node(val,top);
cout<<"Element successfully pushed";
}
}
void stack::pop()
{
if(isUnderflow())
cout<<"Stack Underflow...Nothing can be popped";
else if(top==0)
{
cout<<"Element popped is:"<<top->info;
delete top;
top=0;
}
else
{
cout<<"Element popped is:"<<top->info;
node* temp=top;
top=top->next;
delete temp;
}
}
void stack::display()
{
if(isUnderflow())
cout<<"Stack Empty...";
else
{
node* temp=top;
cout<<"Stack is as follows"<<endl;
while(temp!=0)
{
cout<<temp->info<<endl;
temp=temp->next;
}
}
}
void main()
{
int choice,v;
char ch;
stack obj;
do
{
cout<<"n ******** Welcome to Stack Menu ********";
cout<<"n ---------------------------------------";
cout<<"n Enter your choice:";
cout<<"nt1.Push";
cout<<"nt2.Pop";
cout<<"nt3.Display"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"Enter the element to be added"<<endl;
cin>>v;
obj.push(v);
break;
}
case 2:obj.pop();
break;
case 3:obj.display();
break;
default:cout<<"nWrong choice";
}
cout<<"nnDo you want to continue? (Y/N)"<<endl;
cin>>ch;
}while(ch=='Y'||ch=='y');
getch();
}

CODE Data Structures

  • 1.
    #include <iostream> #include <conio> classnode { public: int info; node* next; node(int val,node* ptr=0) { info=val; next=ptr; } }; class stack { node* top; public: stack() { top=0; } bool isOverflow(); bool isUnderflow(); void push(int val); void pop(); void display(); }; bool stack::isUnderflow() { return (top==0); } void stack::push(int val) { if(top==0) { top=new node(val); cout<<"Element successfully pushed"; } else { top=new node(val,top); cout<<"Element successfully pushed"; } } void stack::pop() { if(isUnderflow()) cout<<"Stack Underflow...Nothing can be popped"; else if(top==0) { cout<<"Element popped is:"<<top->info; delete top; top=0; }
  • 2.
    else { cout<<"Element popped is:"<<top->info; node*temp=top; top=top->next; delete temp; } } void stack::display() { if(isUnderflow()) cout<<"Stack Empty..."; else { node* temp=top; cout<<"Stack is as follows"<<endl; while(temp!=0) { cout<<temp->info<<endl; temp=temp->next; } } } void main() { int choice,v; char ch; stack obj; do { cout<<"n ******** Welcome to Stack Menu ********"; cout<<"n ---------------------------------------"; cout<<"n Enter your choice:"; cout<<"nt1.Push"; cout<<"nt2.Pop"; cout<<"nt3.Display"<<endl; cin>>choice; switch(choice) { case 1: { cout<<"Enter the element to be added"<<endl; cin>>v; obj.push(v); break; } case 2:obj.pop(); break; case 3:obj.display(); break; default:cout<<"nWrong choice"; } cout<<"nnDo you want to continue? (Y/N)"<<endl; cin>>ch;
  • 3.