Rakibul_Ahasan

Stack_Linked List_With_Function

Aug 21st, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int count=-1;
  5.  
  6. typedef struct node {
  7.     int val;
  8.     struct node * NEXT;
  9.     struct node * PREV;
  10. } node_t;
  11.  
  12. node_t * last;
  13.  
  14. void create() {
  15.     last = (struct node*) malloc(sizeof(node_t));
  16.     last = NULL;
  17.     count = 0;
  18. }
  19.  
  20. int isEmpty () {
  21.     if (last == NULL) return 1;
  22.     else return 0;
  23. }
  24.  
  25. int size() {
  26.     return count;
  27. }
  28.  
  29. void push (int num) {
  30.     node_t * temp;
  31.     temp = (struct node*) malloc(sizeof(node_t));
  32.     temp->val = num;
  33.     temp->NEXT = NULL;
  34.     temp->PREV = last;
  35.     if (last != NULL) {
  36.         last->NEXT = temp;
  37.         last = temp;
  38.     }
  39.     else last = temp;
  40.     count ++;
  41. }
  42.  
  43. int pop () {
  44.     int v;
  45.     v = last->val;
  46.     node_t * temp;
  47.     temp = (struct node*) malloc(sizeof(node_t));
  48.     temp = last;
  49.     last = last->PREV;
  50.     if(last!= NULL) last->NEXT = NULL;
  51.     free(temp);
  52.     count--;
  53.     return v;
  54. }
  55.  
  56. int top () {
  57.     return last->val;
  58. }
  59.  
  60. void display(node_t *temp) {
  61.     if (temp->PREV!=NULL) display(temp->PREV);
  62.     printf("%d ", temp->val);
  63. }
  64.  
  65. void menu () {
  66.      while (1) {
  67.         printf("Press 1 to create a new Stack.\n");
  68.         printf("Press 2 to push.\n");
  69.         printf("Press 3 to check if the Stack is empty.\n");
  70.         printf("Press 4 to pop.\n");
  71.         printf("Press 5 to show the top element.\n");
  72.         printf("Press 6 to view the size.\n");
  73.         printf("Press 7 to display.\n");
  74.         printf("Press 0 to Exit.\n");
  75.  
  76.         int n;
  77.         scanf("%d", &n);
  78.         if (n==0) break;
  79.  
  80.         else if (n==1) create();
  81.  
  82.         else if (n==2) {
  83.             printf("Enter the number: ");
  84.             int num;
  85.             scanf("%d", &num);
  86.             push(num);
  87.         }
  88.  
  89.         else if (n==3) {
  90.             if (isEmpty()) printf("The Stack is empty.\n");
  91.             else printf("The Stack is not empty.\n");
  92.         }
  93.  
  94.         else if (n==4) {
  95.             if (!isEmpty()) printf("Popped value = %d\n", pop());
  96.             else printf("The Stack is empty.\n");
  97.         }
  98.  
  99.         else if (n==5) {
  100.             if (!isEmpty()) printf("The top element = %d\n", top());
  101.             else printf("The Stack is empty.\n");
  102.         }
  103.  
  104.         else if (n==6) printf("Size of the Stack = %d\n", size());
  105.  
  106.         else if (n==7) {
  107.             if (!isEmpty()) {
  108.                 display(last);
  109.                 printf("\n");
  110.             }
  111.             else printf("The Stack is empty.\n");
  112.         }
  113.  
  114.         else {
  115.             printf("Invalid choice! Try again.\n");
  116.         }
  117.     }
  118. }
  119.  
  120. int main () {
  121.     menu();
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment