Program to create linked list implementation of stack

Monday, March 09, 2020

//program to create linked list implementation of stack
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{ int info;
  node * next;
}*top,*ptr,*temp;
void PUSH(int INF)
{ ptr=new node;
  ptr->info=INF;
  ptr->next=NULL;
  if(ptr==NULL)
  { cout<<"\n Node is not created";
exit(0);
  }
  if(top==NULL)
top=ptr;
  else
  { ptr->next=top;
top=ptr;
  }
}
void POP()
{ if(top==NULL)
  { cout<<"\n Stack underflow";
exit(0);
  }
  else
  { temp=top;
top=top->next;
delete temp;
  }
}
void DISPLAY(node *np)
{ if(np==NULL)
cout<<"\n Stack empty";
  else
  { while(np!=NULL)
{ cout<<np->info<<"-->>";
np=np->next;
}
cout<<"NULL";
  }
}
void main()
{ clrscr();
  int p,inf;
  top=NULL;
  l:
  cout<<"\n Press 1 for insertion"
<<"\n Press 2 for deletion"
<<"\n Press 3 for exit";
  cin>>p;
  if(p==3)
  { cout<<"\n thank you";
exit(0);
  }
  else if(p==1)
  { cout<<"\n Enter element:";
cin>>inf;
PUSH(inf);
DISPLAY(top);
goto l;
  }
  else
  { POP();
DISPLAY(top);
goto l;
  }
  getch();
}

You Might Also Like

0 Comments

Like us on Facebook

Flickr Images

Flickr Short Films