Advertisement
joker546645

final_stack_list

Mar 7th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3.  
  4. struct stack_n
  5. {
  6.     int number;
  7.     struct stack_n *next;
  8. };
  9.  
  10. struct list
  11. {
  12.     struct stack_n *stacks;
  13.     struct list *next;
  14.     struct list *prev;
  15. };
  16.  
  17. int index ( struct list *ls )
  18. {
  19.     int i = 0;
  20.     while (ls != NULL && ls->stacks != NULL)
  21.     {
  22.         ls = ls->next;
  23.         i++;
  24.     }
  25.     return i;
  26. }
  27.  
  28. struct list *find ( int ind, struct list **ls )
  29. {
  30.     int max_ind = index ( *ls ) - 1;
  31.     if (ind > max_ind)
  32.     {
  33.         return NULL;
  34.     }
  35.     int to_back = max_ind - ind;
  36.     while (to_back > 0)
  37.     {
  38.         *ls = (*ls)->next;
  39.         to_back--;
  40.     }
  41.     struct list *tmp = *ls;
  42.     while ((*ls)->prev != NULL)
  43.         *ls = (*ls)->prev;
  44.     return tmp;
  45. }
  46.  
  47. int add (int ind, struct stack_n **top, struct list **ls )
  48. {
  49.     int max_ind = index ( *ls );
  50.     if (ind > max_ind)
  51.     {
  52.         return 0;
  53.     }
  54.    
  55.     struct list *p;
  56.     p = *ls;
  57.     *ls = (struct list *)malloc ( sizeof ( struct list ) );
  58.     (*ls)->stacks = *top;
  59.     (*ls)->next = p;
  60.     (*ls)->prev = NULL;
  61.     p->prev = *ls;
  62.  
  63.     if (ind != 0 && max_ind != ind)
  64.     {
  65.         struct list *tmp_p = find ( ind, ls );
  66.         struct list *tmp_m = find ( ind - 1, ls );
  67.         p->prev = NULL;
  68.         tmp_m->prev = *ls;
  69.         tmp_p->next = *ls;
  70.         (*ls)->next = tmp_m;
  71.         (*ls)->prev = tmp_p;
  72.  
  73.     }
  74.     if (ind == 0 && max_ind != 0)
  75.     {
  76.         struct list *tmp = find ( 0, ls );
  77.         (*ls)->prev = tmp;
  78.         (*ls)->next = NULL;
  79.         p->prev = NULL;
  80.         tmp->next = *ls;
  81.     }
  82.     while ((*ls)->prev != NULL)
  83.         *ls = (*ls)->prev;
  84.     return 1;
  85. }
  86.  
  87. bool sub ( int ind, struct list **ls )
  88. {
  89.     int max_ind = index ( *ls ) - 1;
  90.     if (ind > max_ind)
  91.     {
  92.         return false;
  93.     }
  94.     struct list *p;
  95.     int to_back = max_ind - ind;
  96.     while (to_back > 0)
  97.     {
  98.         *ls = (*ls)->next;
  99.         to_back--;
  100.     }
  101.     p = *ls;
  102.     if (ind == 0 && max_ind == 0)
  103.     {
  104.         (*ls)->stacks = NULL;
  105.         (*ls)->next = NULL;
  106.         (*ls)->prev = NULL;
  107.         return true;
  108.     }
  109.     if (ind == 0)
  110.     {
  111.         (*ls)->prev->next = NULL;
  112.     }
  113.     else if (ind == max_ind)
  114.     {
  115.         *ls = (*ls)->next;
  116.         (*ls)->prev = NULL;
  117.     }
  118.     else
  119.     {
  120.         (*ls)->next->prev = (*ls)->prev;
  121.         (*ls)->prev->next = (*ls)->next;
  122.     }
  123.     while ((*ls)->prev != NULL)
  124.         *ls = (*ls)->prev;
  125.     free ( p );
  126.  
  127.     return true;
  128. }
  129.  
  130. void push ( int element, struct stack_n **top )
  131. {
  132.     struct stack_n *p;
  133.  
  134.     p = *top;
  135.     *top = (struct stack_n *)malloc ( sizeof ( struct stack_n ) );
  136.     (*top)->number = element;
  137.     (*top)->next = p;
  138. }
  139.  
  140. int pop ( struct stack_n **top )
  141. {
  142.     int element = INT_MAX;
  143.     struct stack_n *p;
  144.  
  145.     if (*top != NULL)
  146.     {
  147.         p = *top;
  148.         element = (*top)->number;
  149.         *top = (*top)->next;
  150.         free ( p );
  151.     }
  152.     return element;
  153. }
  154.  
  155. void print ( struct list *ls )
  156. {
  157.     int max_ind = index ( ls );
  158.     if (max_ind == 0)
  159.     {
  160.         printf ( "Empty!\n" );
  161.     }
  162.     else
  163.     {
  164.         int p;
  165.         struct list *tmp_list;
  166.         tmp_list = ls;
  167.         while (max_ind--)
  168.         {
  169.             struct stack_n *tmp_stack;
  170.             tmp_stack = ls->stacks;
  171.             printf ( "L.%d --> ", max_ind );
  172.             while (ls->stacks != NULL) {
  173.                 p = ls->stacks->number;
  174.                 printf ( "%d ", p );
  175.                 ls->stacks = ls->stacks->next;
  176.             }
  177.             ls->stacks = tmp_stack;
  178.             printf ( "\n" );
  179.             ls = ls->next;
  180.         }
  181.         ls = tmp_list;
  182.         printf ( "----------------------------------------------||\n\n" );
  183.     }
  184. }
  185.  
  186. int main ( ) {
  187.  
  188.     struct stack_n *top = NULL;
  189.  
  190.     push ( 1, &top );
  191.     push ( 2, &top );
  192.     push ( 3, &top );
  193.  
  194.  
  195.     struct stack_n *top2 = NULL;
  196.  
  197.     push ( 4, &top2 );
  198.     push ( 5, &top2 );
  199.     push ( 6, &top2 );
  200.  
  201.  
  202.     struct list *ls = ( struct list * )malloc ( sizeof ( struct list ) );
  203.     ls->stacks = NULL;
  204.     ls->next = NULL;
  205.     ls->prev = NULL;
  206.     add ( 0, &top, &ls );
  207.     add ( 1, &top2, &ls );
  208.  
  209.     struct stack_n *top4 = NULL;
  210.  
  211.     push ( 7, &top4 );
  212.     push ( 8, &top4 );
  213.     push ( 9, &top4 );
  214.     add ( 1, &top4, &ls );
  215.     struct list *top3 = find ( 1, &ls );
  216.     print ( ls );
  217.     sub ( 0, &ls );
  218.     print ( ls );
  219.     int s = index ( ls );
  220.     print ( top3 );
  221.     push ( 55, &(ls->stacks) );
  222.     print ( ls );
  223.    
  224.     pop ( &(ls->stacks) );
  225.     pop ( &(ls->stacks) );
  226.     print ( ls );
  227.     sub ( 1, &ls );
  228.     sub ( 0, &ls );
  229.     print ( ls );
  230.     free ( ls );
  231.  
  232.     return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement