shridharshetti

LinkedList 3rd sem ISE

Oct 18th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. };
  8. struct node *push(struct node *head,int data)
  9. {
  10.     struct node *nw=NULL;
  11.     nw=(struct node *)malloc(sizeof(struct node));
  12.     nw->data=data;
  13.     nw->next=NULL;
  14.     if(head==NULL)
  15.     {
  16.         head=nw;
  17.     }
  18.     else
  19.     {
  20.         nw->next=head;
  21.         head=nw;
  22.     }
  23.     return(head);
  24. }
  25. struct node *pop(struct node *head)
  26. {
  27.     struct node *cur;
  28.     if(head==NULL)
  29.         printf("Empty\n");
  30.     else
  31.     {
  32.         cur=head;
  33.         printf("%d deleted\n",cur->data);
  34.         head=head->next;
  35.         free(cur);
  36.     }
  37.     return head;
  38. }
  39. void main()
  40. {
  41.     int ch,n;
  42. struct node *head=NULL;
  43. do
  44. {
  45.     printf("1 to push \n2 to pop\n");
  46.     scanf("%d",&ch);
  47.     switch(ch)
  48.     {
  49.         case 1 :    printf("Enter data\n");
  50.                     scanf("%d",&n);
  51.                     head=push(head,n);
  52.                     break;
  53.         case 2 :    head=pop(head);
  54.                     break;
  55.         case 3 :    break;
  56.  
  57.     }
  58. }while(ch!=3);
  59. }
Add Comment
Please, Sign In to add comment