Advertisement
immuntasir

Stack - Linked List

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