#include<stdio.h>
#include<stdlib.h>

struct node
{
 int data;
 struct node *next;
};

void push(struct node **first,int n)
{
 struct node *p,*q;
 q=(struct node *)malloc(sizeof(struct node));
 if(*first==NULL)
   {
    *first=q;
    q->data=n;
    q->next=NULL;
   }
 else
   {
    p=*first;
    q->next=p;
    q->data=n;
    *first=q;
   }
}

int pop(struct node **first)
{
 int temp;
 struct node *p=*first;
 if(*first==NULL)
   {
    printf("\nstack is empty\n");
    return(0);
   }
 else
   {
    p=*first;
    *first=p->next;
    temp=p->data;
    free(p);
    return(temp);
   }
}

void display(struct node *p)
{
 if(p==NULL)
   {
    printf("\nstack is empty\n");
   }
 else
   {
    while(p!=NULL)
         {
          printf("\n%d",p->data);
          p=p->next;
         }
   }
}

void main()
{
 struct node *first=NULL;
 int ch,n,temp;
 do
  {
   printf("\nenter your choice:\n1.push\n2.pop\n");
   printf("3.display\n4.exit\n");
   scanf("%d",&ch);
   switch(ch)
     {
      case 1: printf("\nenter integar:");
              scanf("%d",&n);
              printf("\n%d",first);
              push(&first,n);
              printf("\n%d",first);
              break;

      case 2: temp=pop(&first);
              if(temp!=0)
                {
                 printf("\n%d has been popped\n",temp);
                }
              break;

      case 3: display(first);
              break;
     }
 }
 while(ch!=4);
}


