Miti059

stack doubly hw

Oct 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 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 aN)
  10. {
  11.     node*N=(node*)malloc(sizeof(node));
  12.     N->a=aN;
  13.     N->next=NULL;
  14.     N->prev=NULL;
  15.  
  16.     if(head==NULL)
  17.     {
  18.         head=N;
  19.         tail=N;
  20.         return;
  21.     }
  22.     else
  23.     {
  24.         tail->next=N;
  25.         N->prev=tail;
  26.         N->next=NULL;
  27.         tail=N;
  28.         return;
  29.     }
  30.  
  31. }
  32.  
  33. int POP()
  34. {
  35.     int a=0;
  36.     node*list;
  37.     if(head==NULL)
  38.     {
  39.         printf("empty");
  40.     }
  41.     else
  42.     {
  43.         node* temp;
  44.         temp=tail;
  45.         a=tail->a;
  46.         tail->prev->next=NULL;
  47.         tail = tail->prev;
  48.         free(temp);
  49.  
  50.         return a;
  51.     }
  52. }
  53.  
  54. int main()
  55. {
  56.     int n,x,y,i,j,s=0;
  57.     printf("ENTER PUSH CASE:");
  58.     scanf("%d",&n);
  59.     for(i=0; i<n; i++)
  60.     {
  61.         scanf("%d",&x);
  62.         PUSH(x);
  63.     }
  64.    printf("ENTER POP CASE:");
  65.     scanf("%d",&y);
  66.     for(j=0; j<y; j++)
  67.     {
  68.         s=POP();
  69.         printf("%d ",s);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment