ramytamer

Untitled

Apr 29th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define TYPE int
  5.  
  6. typedef struct Node{
  7.     TYPE data;
  8.     Node *back;
  9. }Node;
  10.  
  11. typedef struct Stack{
  12.     Node *top;
  13. }Stack;
  14.  
  15. void reset(Stack *s){ s->top = NULL; }
  16. int isEmpty(Stack *s) { return !s->top; }
  17.  
  18. void push(Stack *s,TYPE val) {
  19.     Node *node = (Node*) malloc(sizeof(Node));
  20.     node->data = val;
  21.     node->back = s->top;
  22.     s->top = node;
  23. }
  24.  
  25. TYPE pop(Stack *s) {
  26.     if(!isEmpty(s)){
  27.         Node *p = s->top;
  28.         TYPE x = p->data;
  29.         s->top = p->back;
  30.         free(p);
  31.         return x;
  32.     }
  33. }
  34.  
  35. void display(Stack s){ if(!isEmpty(&s)){ while(!isEmpty(&s)) printf(" {%d}\n",pop(&s) ); } }
  36.  
  37. int main(){
  38.     Stack s; reset(&s);
  39.     push(&s,5);
  40.     push(&s,6);
  41.     push(&s,7);
  42.     display(s);
  43.     display(s);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment