Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TYPE int
- typedef struct Node{
- TYPE data;
- Node *back;
- }Node;
- typedef struct Stack{
- Node *top;
- }Stack;
- void reset(Stack *s){ s->top = NULL; }
- int isEmpty(Stack *s) { return !s->top; }
- void push(Stack *s,TYPE val) {
- Node *node = (Node*) malloc(sizeof(Node));
- node->data = val;
- node->back = s->top;
- s->top = node;
- }
- TYPE pop(Stack *s) {
- if(!isEmpty(s)){
- Node *p = s->top;
- TYPE x = p->data;
- s->top = p->back;
- free(p);
- return x;
- }
- }
- void display(Stack s){ if(!isEmpty(&s)){ while(!isEmpty(&s)) printf(" {%d}\n",pop(&s) ); } }
- int main(){
- Stack s; reset(&s);
- push(&s,5);
- push(&s,6);
- push(&s,7);
- display(s);
- display(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment