/* STACK OPERATION USING LINK LIST */
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
#include<process.h>
struct node
{
int item;
struct node *add;
}*h;
void push(struct node *a,int x)
{
if(a==NULL)
{
a=(node *)malloc(sizeof(node));
h=a;
a->item=x;
a->add=NULL;
return;
}
while(a->add!=NULL)
{
a=a->add;
}
a->add=(node *)malloc(sizeof(node));
if(a->add==NULL){cout<<"\n\aStack Overflow.";return;}
a=a->add;
a->item=x;
a->add=NULL;
}
int pop(struct node * a)
{
struct node *temp;
if(a->add==NULL)h=NULL;
if(a==NULL){cout<<"\n\aStack Underflow.";return 0;}
while(a->add!=NULL)
{
temp=a;
a=a->add;
}
temp->add=NULL;
int x=a->item;
a=NULL;
return x;
}
void show(struct node *a)
{
cout<<"List.:";
while(a!=NULL)
{
cout<<" "<<a->item;
a=a->add;
}
}
void main()
{
clrscr();
cout<<"\nSTACK OPERATION USING LINK LIST\n";
h=NULL;
while(1)
{
cout<<"\n1.PUSH\t2.POP\t3.EXIT\n";
char ch;
int n;
ch=getch();
switch(ch)
{
case '1':
cout<<"Enter no.:";
cin>>n;
push(h,n);
show(h);
break;
case '2':
cout<<"Poped.:"<<pop(h);
show(h);
break;
case '3':
exit(0);
}
}
}
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
#include<process.h>
struct node
{
int item;
struct node *add;
}*h;
void push(struct node *a,int x)
{
if(a==NULL)
{
a=(node *)malloc(sizeof(node));
h=a;
a->item=x;
a->add=NULL;
return;
}
while(a->add!=NULL)
{
a=a->add;
}
a->add=(node *)malloc(sizeof(node));
if(a->add==NULL){cout<<"\n\aStack Overflow.";return;}
a=a->add;
a->item=x;
a->add=NULL;
}
int pop(struct node * a)
{
struct node *temp;
if(a->add==NULL)h=NULL;
if(a==NULL){cout<<"\n\aStack Underflow.";return 0;}
while(a->add!=NULL)
{
temp=a;
a=a->add;
}
temp->add=NULL;
int x=a->item;
a=NULL;
return x;
}
void show(struct node *a)
{
cout<<"List.:";
while(a!=NULL)
{
cout<<" "<<a->item;
a=a->add;
}
}
void main()
{
clrscr();
cout<<"\nSTACK OPERATION USING LINK LIST\n";
h=NULL;
while(1)
{
cout<<"\n1.PUSH\t2.POP\t3.EXIT\n";
char ch;
int n;
ch=getch();
switch(ch)
{
case '1':
cout<<"Enter no.:";
cin>>n;
push(h,n);
show(h);
break;
case '2':
cout<<"Poped.:"<<pop(h);
show(h);
break;
case '3':
exit(0);
}
}
}
No comments:
Post a Comment