Matt23

stack

Mar 6th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. // not finnished
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct node {
  7.     int data;
  8.     struct node *next;
  9. } stack;
  10.  
  11. stack* init() {
  12.     stack* tmp = malloc(sizeof(stack));
  13.     tmp->data = NULL;
  14.     tmp->next = NULL;
  15.     return tmp;
  16. }
  17.  
  18. void destroy(stack **s) {
  19.     *s = NULL;
  20. }
  21.  
  22. int push(stack** s, int value) {
  23.     if (s == NULL) return 0;
  24.  
  25.     if((*s)->data == NULL) {
  26.         (*s)->data = value;
  27.     }
  28.     else {
  29.         stack* tmp = malloc(sizeof(stack));
  30.         tmp->data = value;
  31.         tmp->next = *s;
  32.         *s = tmp;
  33.     }
  34.  
  35.     return 1;
  36. }
  37.  
  38. int pop(stack** s) {
  39.     if (s == NULL) return NULL;
  40.     int value = (*s)->data;
  41.     *s = (*s)->next;
  42.  
  43.     return value;
  44. }
  45.  
  46. int main() {
  47.     stack* st1 = init();
  48.     push(&st1, 1);
  49.     push(&st1, 2);
  50.  
  51.     //push(st1, 3);
  52.     printf("%d\n", st1->data);
  53.     pop(&st1);
  54.     printf("%d\n", st1->data);
  55.     //destroy(st1);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment