Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node{
  5.     /*
  6.                  next
  7.                   |
  8.         ********  v
  9.         * data *------>
  10.         ********
  11.  
  12.         next: pointer to the next NODE
  13.         data: the data (as an integer in this case)
  14.         all the structure is called NODE
  15.     */
  16.     struct node *next;
  17.     int data;
  18. }NODE;
  19.  
  20. typedef struct stack{
  21.    /*
  22.  
  23.          
  24.                  pointer to NODE
  25.                    |
  26.         ********   v
  27.         * size *------>
  28.         ********
  29.         size: The size of the stack
  30.         all the structure is called stack
  31.    */
  32.     int size; /*This doesn't work*/
  33.     NODE *top;
  34. }STACK;
  35.  
  36. STACK* create_stack(){
  37.     /*
  38.         Here I create a pointer to the stack structure.
  39.         And I inicialize the values.
  40.         Also return NULL in case that is not more memory
  41.         available otherwise return a stack with defaults
  42.         values
  43.     */
  44.     STACK *s = (STACK*) malloc(sizeof(STACK));
  45.     if(s == NULL) /* Not more memory available */
  46.         return s; /* return NULL */
  47.     /* In other case set up the default values */
  48.     s->size = 0; /* No elements in the stack */
  49.     s->top = NULL; /* The top doesn't have any NODE */
  50.     return s; /* Return the stack with default values */
  51. }
  52.  
  53. int empty(STACK *s){
  54.     /*
  55.         Returns 1 (or True) if the stack is empty
  56.         Returns 0 (or False) if the stack is NOT empty.
  57.     */
  58.  
  59.     /*If there is no STACK or don't have any NODE*/
  60.     if(s == NULL || s->top == NULL)
  61.         return 1; /* returns True */
  62.     return 0; /* returns False */
  63. }
  64.  
  65. void push(STACK *s, int data){
  66.     /*
  67.         This function insert a new NODE in the top of the stack
  68.         Example 1: Empty STACK
  69.            Pointer to the top of the stack
  70.                    |                
  71.         *********  v
  72.         * STACK *---->NULL
  73.         *********
  74.  
  75.         Step 1: Create a new node
  76.            Pointer to the next NODE
  77.                     |
  78.         **********  v
  79.         * NODE_1 *---->
  80.         **********
  81.  
  82.         Step 2: Put it on the top
  83.         *********    
  84.         * STACK *---->NULL
  85.         *********    
  86.  
  87.         **********
  88.         * NODE_1 *---->NULL
  89.         **********
  90.  
  91.         *********     **********
  92.         * STACK *---->* NODE_1 *---->NULL
  93.         *********     **********
  94.  
  95.         Example 2: Stack N NODES
  96.            Pointer to the top of the stack
  97.                    |                
  98.         *********  v  **********             **********
  99.         * STACK *---->* NODE_N *---->...---->* NODE_1 *---->NULL
  100.         *********     **********             **********
  101.  
  102.         Step 1: Create a new node
  103.            Pointer to the next NODE
  104.                         |
  105.         **************  v
  106.         * NODE_(N+1) *---->
  107.         **************
  108.  
  109.         Step 2: Put it on the top
  110.         *********  v  **********             **********
  111.         * STACK *---->* NODE_N *---->...---->* NODE_1 *---->NULL
  112.         *********     **********             **********
  113.  
  114.         **************     **********
  115.         * NODE_(N+1) *---->* NODE_N *
  116.         **************     **********
  117.  
  118.         *********     **************     **********             **********
  119.         * STACK *---->* NODE_(N+1) *---->* NODE_N *---->...---->* NODE_1 *---->NULL
  120.         *********     **************     **********             **********
  121.  
  122.     */
  123.  
  124.     /* If the stack doesn't exists then stop */
  125.     if(s == NULL)
  126.         return;
  127.     /* Create a NODE and keep the pointer in p */
  128.     NODE *p = (NODE*)malloc(sizeof(NODE));
  129.     if(p == NULL) /* If p is NULL then stop (no more memory available) */
  130.         return;
  131.     /* Put the data in the node */
  132.     p->data = data;
  133.     /* The next element of p is top */
  134.     p->next = s->top;
  135.     /* The new top is now p */
  136.     s->top = p;
  137. }
  138.  
  139. void pop(STACK *s){
  140.     /* If the stack doesn't exists then stop*/
  141.     if(s == NULL)
  142.         return;
  143.     /*
  144.         *********     **********     **********
  145.         * STACK *---->* NODE_2 *---->* NODE_1 *---->NULL
  146.         *********     **********     **********
  147.         Step 1: Create a pointer P that points to the top of the STACK
  148.         **********     **********
  149.         * NODE_2 *---->* NODE_1 *
  150.         **********     **********
  151.             ^
  152.             |
  153.             |
  154.           *****
  155.           * P *
  156.           *****
  157.         Step 2: The next element of P is the new top.
  158.         *********     **********
  159.         * STACK *---->* NODE_1 *---->NULL
  160.         *********     **********
  161.         Now NODE_2 is not anymore in the stack but it stills in P
  162.         Just free P (aka NODE_2)
  163.        
  164.     */
  165.  
  166.  
  167.     /* If the stack is NOT empty */
  168.     if(!empty(s)){
  169.         NODE *p; /* Create a pointer to a NODE */
  170.         p = s->top; /* The top of the STACK is in p */
  171.         s->top = p->next; /* The top of the STACK is now the next element of p */
  172.         free(p); /* Delete the top of the stack */
  173.     }
  174. }
  175.  
  176. int top(STACK *s){
  177.     /*
  178.         If STACK is empty just return 0
  179.         otherwise returns the value of the top of STACK
  180.     */
  181.     if(!empty(s))
  182.         return 0;
  183.     return s->top->data;
  184. }
  185.  
  186. void print_stack(STACK *s){
  187.     /* Just print the STACK in a nice way */
  188.     NODE *p;
  189.     if(s == NULL){
  190.         printf("NULL\n");
  191.         return;
  192.     }
  193.  
  194.     if(empty(s)){
  195.         printf("[]\n");
  196.         return;
  197.     }
  198.     else{
  199.         printf("[%d", s->top->data);
  200.         for(p = s->top->next; p != NULL; p = p->next)
  201.             printf(" %d", p->data);
  202.         printf("]\n");
  203.     }
  204. }
  205.  
  206. void destroy_stack(STACK **s){
  207.     /* Destroy the stack */
  208.     if(s == NULL || *s == NULL)
  209.         return;
  210.    
  211.     while(!empty(*s))
  212.         pop(*s);
  213.     free(*s);
  214.     *s = NULL;
  215. }
  216.  
  217. int main(){
  218.  
  219.     STACK *s = create_stack();
  220.     int i;
  221.    
  222.     printf("PUSH TIME\n");
  223.     for(i = 0; i < 10; i++){
  224.         print_stack(s);
  225.         push(s, i);
  226.     }
  227.  
  228.     printf("POP TIME\n");
  229.     while( !empty(s) ){
  230.         print_stack(s);
  231.         pop(s);
  232.     }
  233.  
  234.     print_stack(s);
  235.  
  236.     destroy_stack(&s);
  237.     print_stack(s);
  238.     return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement