Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. typedef struct stack stack;
  2. typedef struct node node;
  3.  
  4. struct node {
  5. char sequence;
  6. node *next;
  7. };
  8.  
  9. struct stack {
  10. node *top;
  11. };
  12.  
  13. stack *create_stack() {
  14. stack *new_stack = (stack*) malloc(sizeof(stack));
  15. new_stack -> top = NULL;
  16. return new_stack;
  17. }
  18.  
  19. void push(stack *stack, char sequence) {
  20. node *new_top = (node*) malloc(sizeof(node));
  21. new_top -> sequence = sequence;
  22. new_top -> next = stack -> top;
  23. stack -> top = new_top;
  24. return;
  25. }
  26.  
  27. int pop(stack *stack) {
  28. if(stack -> top == NULL) {
  29. //printf("Stack is underflow.\n");
  30. return -1; // error code
  31. }
  32. else {
  33. //printf("%s\n", stack -> top -> sequence);
  34. stack -> top = stack -> top -> next;
  35. return 0; // success code
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement