Advertisement
joker546645

Adam_działczy

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