monoteen

//error

Nov 19th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef int element;
  6.  
  7. typedef struct stackNode {
  8.     element data;
  9.     struct stackNode *link;
  10. }stackNode;
  11.  
  12. stackNode* top;
  13.  
  14. void push(element item)
  15. {
  16.     stackNode* temp = (stackNode *)malloc(sizeof(stackNode));
  17.     temp->data = item;
  18.     temp->link = top;
  19.     top = temp;
  20. }
  21.  
  22. element pop()
  23. {
  24.     element item;
  25.     stackNode* temp = top;
  26.  
  27.     if (top == NULL) {
  28.         printf("\n\n Stack is Empty\n");
  29.         return 0;
  30.     }
  31.     else {
  32.         item = temp->data;
  33.         top = temp->link;
  34.         free(temp);
  35.         return item;
  36.     }
  37. }
  38.  
  39. element peek()
  40. {
  41.     element item;
  42.     if (top == NULL) {
  43.         printf("\n\n Stack is Empty\n");
  44.         return 0;
  45.     }
  46.     else {
  47.         item = top->data;
  48.         return item;
  49.     }
  50. }
  51.  
  52. void del()
  53. {
  54.     stackNode* temp;
  55.     if (top == NULL) {
  56.         printf("\n\n Stack is Empty\n");
  57.     }
  58.     else {
  59.         temp = top;
  60.         top = top->link;
  61.         free(temp);
  62.     }
  63. }
  64.  
  65. void printStack()
  66. {
  67.     stackNode* p = top;
  68.     printf("\n STACK [ ");
  69.     while (p) {
  70.         printf("%d ", p->data);
  71.         p = p->link;
  72.     }
  73.     printf("] ");
  74. }
  75.  
  76. int main(void)
  77. {
  78.     element item;
  79.     top = NULL;
  80.     printStack();
  81.     push(1);
  82.     printStack();
  83.     push(2);
  84.     printStack();
  85.     push(3);
  86.     printStack();
  87.  
  88.     item = peek();
  89.     printStack();
  90.     printf("peek top => %d", item);
  91.  
  92.     del();
  93.     printStack();
  94.  
  95.     item = pop;
  96.     printStack();
  97.     printf("\t pop top => %d", item);
  98.  
  99.     item = pop();
  100.     printStack();
  101.     printf("\t pop top => %d", item);
  102.  
  103.     pop();
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment