Program to perform push, pop, and traversal in stack

Monday, March 09, 2020

//program to perform push, pop, and traversal in stack
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int size=20;
void PUSH(int STACK[],int ELEMENT ,int &TOP)
{ if(TOP==size-1)
  { cout<<"\n Overflow";
  }
  else
  { TOP++;
STACK[TOP]=ELEMENT;
  }
}
void POP(int STACK[],int& TOP)
{ if(TOP==-1)
  { cout<<"\n Underflow";
  }
  else
  { cout<<"\n Element to be deleted"<<STACK[TOP];
TOP--;
  }
}
void DISPLAY(int STACK[],int TOP)
{ clrscr();
  cout<<STACK[TOP]<<"<--TOP";
  for(int P=TOP-1;P>=0;P--)
  cout<<"\n"<<STACK[P];
}
void main()
{ clrscr();
  int ch, element, top=-1,stack[20];
  l:
  //clrscr();
  cout<<"\n Press 1 to PUSH"
<<"\n Press 2 to POP"
<<"\n Press 3 to TRAVERSAL"
<<"\n Press 4 to EXIT\n";
  cout<<"\nEnter your choice:";
  cin>>ch;
  if(ch==4)
  { exit(0);}
  else if(ch==1)
  { cout<<"\n Enter the element to be inserted:";
cin>>element;
PUSH(stack,element,top);
goto l;
  }
  else if(ch==2)
  { POP(stack,top);
goto l;
  }
  else
  { DISPLAY(stack,top);
goto l;
  }
  getch();
}

You Might Also Like

0 Comments

Like us on Facebook

Flickr Images

Flickr Short Films