/*
Stack performs following operation
1 . Insertion (push).
2 . Deletion (pop)
*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class Stack
{
public :
int val;
node *start,*temp,*cur;
Stack()
{
start=NULL;
}
void push()
{
cout<<"\nEnter value of element :";
cin>>val;
temp=new node[1];//temp=new node;
temp->data=val;
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void pop()
{
if(start==NULL)
cout<<"\nStack is empty\n";
else
{
temp=start;
start=start->next;
delete temp;
cout<<"\nElement deleted successfully\n";
}
}
void display()
{
if(start==NULL)
cout<<"\nStack is empty\n";
else
{
temp=start;
while(temp!=NULL)
{
cout<<temp->data<<",";
temp=temp->next;
}
}
}
};
void main()
{
Stack s;
int n;
char ch;
do{
cout<<"\nMain Menu\n";
cout<<"1.Push\t2.Pop\t3.Display\t4.Exit\nEnter yuor choice :";
n=getch();
switch(n)
{
case '1':s.push();
cout<<"\nElement inserted successfully\n";
break;
case '2':s.pop();
break;
case '3':s.display();
break;
case '4':exit(0);
}
cout<<"\nDo u want to continue(y/n) : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output
Stack performs following operation
1 . Insertion (push).
2 . Deletion (pop)
*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class Stack
{
public :
int val;
node *start,*temp,*cur;
Stack()
{
start=NULL;
}
void push()
{
cout<<"\nEnter value of element :";
cin>>val;
temp=new node[1];//temp=new node;
temp->data=val;
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void pop()
{
if(start==NULL)
cout<<"\nStack is empty\n";
else
{
temp=start;
start=start->next;
delete temp;
cout<<"\nElement deleted successfully\n";
}
}
void display()
{
if(start==NULL)
cout<<"\nStack is empty\n";
else
{
temp=start;
while(temp!=NULL)
{
cout<<temp->data<<",";
temp=temp->next;
}
}
}
};
void main()
{
Stack s;
int n;
char ch;
do{
cout<<"\nMain Menu\n";
cout<<"1.Push\t2.Pop\t3.Display\t4.Exit\nEnter yuor choice :";
n=getch();
switch(n)
{
case '1':s.push();
cout<<"\nElement inserted successfully\n";
break;
case '2':s.pop();
break;
case '3':s.display();
break;
case '4':exit(0);
}
cout<<"\nDo u want to continue(y/n) : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output
No comments:
Post a Comment