Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. File Edit Options Buffers Tools C Help                                                                        
  2. #include "stack.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. void stack_init(stack_s *stack) {
  7.   *stack = malloc(sizeof(stack_node_s));
  8.   (*stack)->next = NULL;
  9.   (*stack)->value = NULL;
  10. }
  11.  
  12. int stack_push(void *p, stack_s *stack) {
  13.   stack_s first = malloc(sizeof(stack_node_s));
  14.  
  15.   if (first == NULL) {
  16.     return -1;
  17.   } else {
  18.     first->value = p;
  19.     first->next = *stack;
  20.     *stack = first;
  21.     return 1;
  22.   }
  23. }
  24.  
  25. void *stack_pop(stack_s *stack) {
  26.   if (*stack != NULL) {
  27.     void *temp = malloc(sizeof(void));
  28.     *temp = (*stack)->value;
  29.     *stack = (*stack)->next;
  30.     return *temp;
  31.   } else {
  32.     return NULL;
  33.   }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement