Advertisement
kebria

Implementing Stack in C. Using Linked List

Jun 23rd, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. /* Implementing Stack in C. Using Linked List */
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5.  
  6. struct Node
  7. {
  8.     int data;
  9.     struct Node* next;
  10. }*top = NULL;
  11.  
  12. void push(int value)
  13. {
  14.     struct Node* newNode;
  15.     newNode = (struct Node*)malloc(sizeof(struct Node));
  16.     newNode->data = value;
  17.     if (top == NULL)
  18.         newNode->next = NULL;
  19.     else
  20.         newNode->next = top;
  21.  
  22.     top = newNode;
  23.     printf("\nInsertion is Successful!!!\n");
  24. }
  25.  
  26. void pop()
  27. {
  28.     if (top == NULL)
  29.         printf("\nStack is Empty!!!\n");
  30.     else
  31.     {
  32.         struct Node* temp = top;
  33.         printf("\n to beDeleted element: %d", temp->data);
  34.         top = temp->next;
  35.         free(temp);
  36.     }
  37. }
  38. void print()
  39. {
  40.     if (top == NULL)
  41.         printf("\nStack is Empty!!!\n");
  42.     else {
  43.         struct Node* temp = top;
  44.         while (temp->next != NULL) {
  45.             printf("%d--->", temp->data);
  46.             temp = temp->next;
  47.         }
  48.         printf("%d--->NULL", temp->data);
  49.     }
  50. }
  51.  
  52. int main()
  53. {
  54.     int choice, value;
  55.     printf("\n:: Stack using Linked List ::\n");
  56.     while (1)
  57.     {
  58.         printf("\n****** MENU ******\n");
  59.         printf("1. Push\n2. Pop\n3. Print\n4. Exit\n");
  60.         printf("Enter your choice: ");
  61.         scanf("%d", &choice);
  62.         switch (choice)
  63.         {
  64.         case 1: printf("Enter the value to be insert: ");
  65.             scanf("%d", &value);
  66.             push(value);
  67.             break;
  68.         case 2: pop(); break;
  69.         case 3: print(); break;
  70.         case 4: exit(0);
  71.         default: printf("\nWrong selection!!! Please try again!!!\n");
  72.         }
  73.     }
  74.     return 0;
  75. }
  76.  
  77.  
  78. /*
  79. :: Stack using Linked List ::
  80.  
  81. ****** MENU ******
  82. 1. Push
  83. 2. Pop
  84. 3. Print
  85. 4. Exit
  86. Enter your choice: 1
  87. Enter the value to be insert: 10
  88.  
  89. Insertion is Successful!!!
  90.  
  91. ****** MENU ******
  92. 1. Push
  93. 2. Pop
  94. 3. Print
  95. 4. Exit
  96. Enter your choice: 1
  97. Enter the value to be insert: 20
  98.  
  99. Insertion is Successful!!!
  100.  
  101. ****** MENU ******
  102. 1. Push
  103. 2. Pop
  104. 3. Print
  105. 4. Exit
  106. Enter your choice: 2
  107.  
  108.  to beDeleted element: 20
  109. ****** MENU ******
  110. 1. Push
  111. 2. Pop
  112. 3. Print
  113. 4. Exit
  114. Enter your choice: 3
  115. 10--->NULL
  116. ****** MENU ******
  117. 1. Push
  118. 2. Pop
  119. 3. Print
  120. 4. Exit
  121. Enter your choice: 4
  122. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement