Advertisement
Guest User

Double linked stack (push, pop)

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. struct node *next;
  6. int data;
  7. };
  8.  
  9. struct node *push(struct node *head, int num) {
  10. struct node *newNode = malloc(sizeof(*head));
  11. newNode->next = head;
  12. newNode->data = num;
  13. return newNode;
  14. }
  15.  
  16. int pop(struct node **headPtr) {
  17. struct node *top = *headPtr;
  18. int data = top->data;
  19. *headPtr = top->next;
  20. free(top);
  21. return data;
  22. }
  23.  
  24.  
  25. int main(int argc, char **argv) {
  26. struct node *head = NULL;
  27. int i;
  28. for (i = 1; i < argc; i++) {
  29. head = push(head, atoi(argv[i]));
  30. }
  31.  
  32. while (head) {
  33. int x = pop(&head);
  34. printf("%d ", x);
  35. }
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement