Jan
23
#include<stdio.h>
#include<conio.h>
struct stack
{
int no;
struct stack *next;
}*top=NULL;
typedef struct stack st;
void push();
void pop();
void traverse();
void main(){
int choice;
char ch;
clrscr();
do
{
printf("\n1 push");
printf("\n2 pop");
printf("\n3 traverse");
printf("\n4 exit");
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: traverse();
break;
case 4: exit(0);
}
}while(choice !=4);
}
void push()
{
st *p;
p=(st*) malloc(sizeof(st));
printf("\n Enter the element to be inserted");
scanf("%d",&p->no);
p->next=top;
top=p;
}
void pop(){
  st* p;
  p=top;
  if(top==NULL)
  printf("Stack is already empty");
  else
  {
  top=top->next;
  printf("\n The deleted element is %d",p->no);
  free (p);
  }
  }
  void traverse()
  {
  st *p;
  p=top;
  while(p!=NULL)
  {
  printf("\nno = %d",p->no);
  p=p->next;
  }
  printf("\nno = %d",p->no);
  }