Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
q/kdb+ 1.48 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct Node
  6. {
  7.     char *data;
  8.     struct Node *link;
  9. };
  10. struct Node *top;
  11.  
  12. void push(char *data)
  13. {
  14.     struct Node *temp;
  15.     temp = (struct Node *)malloc(sizeof(struct Node));
  16.     if (!temp)
  17.     {
  18.         printf("\nHeap Overflow");
  19.         abort();
  20.     }
  21.     temp->data = _strdup(data);
  22.     temp->link = top;
  23.     top = temp;
  24. }
  25.  
  26. int isEmpty()
  27. {
  28.     return top == NULL;
  29. }
  30.  
  31. char *peek()
  32. {
  33.  
  34.     if (!isEmpty(top))
  35.         return top->data;
  36.     else
  37.         abort();
  38. }
  39.  
  40.  
  41. void pop()
  42. {
  43.     struct Node *temp;
  44.  
  45.  
  46.     if (top == NULL)
  47.     {
  48.        //printf("\nStack Underflow");
  49.         abort();
  50.     }
  51.     else
  52.     {
  53.        // top assign into temp
  54.         temp = top;
  55.  
  56.        // assign second node to top
  57.         top = top->link;
  58.  
  59.        // destroy connection between first and second
  60.         temp->link = NULL;
  61.  
  62.        // release memory of top node
  63.         free(temp);
  64.     }
  65. }
  66.  
  67. void display() // remove at the beginning
  68. {
  69.     struct Node *temp;
  70.  
  71.    // check for stack underflow
  72.     if (top == NULL)
  73.     {
  74.        //printf("\nStack Underflow");
  75.         abort();
  76.     }
  77.     else
  78.     {
  79.         temp = top;
  80.         printf("Stack contains:\n");
  81.         while (temp != NULL)
  82.         {
  83.  
  84.            // print node data
  85.             printf("%s\n", temp->data);
  86.  
  87.            // assign temp link to temp
  88.             temp = temp->link;
  89.         }
  90.         printf("\n");
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement