Advertisement
Guest User

stack

a guest
Oct 20th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     int a;
  6.     struct node *next,*prev;
  7. }node;
  8. node*head=NULL,*tail=NULL;
  9. void PUSH(int a)
  10. {
  11.     node*p=(node*)malloc(sizeof(node));
  12.     p->a=a;
  13.     p->next=NULL;
  14.     p->prev=NULL;
  15.  
  16.     if(head==NULL)
  17.     {
  18.         head=p;
  19.         tail=p;
  20.         return;
  21.     }
  22.    tail->next=p;
  23.    p->prev=tail;
  24.    p->next=NULL;
  25.    tail=p;
  26.  
  27. }
  28.  
  29. int POP()
  30. { node*list;
  31.     if(head==NULL)
  32.     {
  33.         printf("empty");
  34.     }
  35.     else
  36.     {
  37.         int temp;
  38.         temp=tail->a;
  39.         list=tail;
  40.         list->prev->next=NULL;
  41.         tail=list->prev;
  42.         list->prev=NULL;
  43.         free(list);
  44.          return temp;
  45.  
  46.     }
  47. }
  48. int main()
  49. {
  50.    int n,x,y,i,s=0;
  51.    scanf("%d",&n);
  52.    for(i=0;i<n;i++)
  53.    {
  54.        scanf("%d",&x);
  55.        PUSH(x);
  56.    }
  57.    scanf("%d",&y);
  58.    for(i=0;i<y;i++)
  59.    {
  60.        s=POP();
  61.        printf("%d ",s);
  62.    }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement