Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- struct stack_n
- {
- int number;
- struct stack_n *next;
- };
- struct list
- {
- struct stack_n *stacks;
- struct list *next;
- struct list *prev;
- };
- int index ( struct list *ls )
- {
- int i = 0;
- while (ls != NULL && ls->stacks != NULL)
- {
- ls = ls->next;
- i++;
- }
- return i;
- }
- struct list *find ( int ind, struct list **ls )
- {
- int max_ind = index ( *ls ) - 1;
- if (ind > max_ind)
- {
- return NULL;
- }
- int to_back = max_ind - ind;
- while (to_back > 0)
- {
- *ls = (*ls)->next;
- to_back--;
- }
- struct list *tmp = *ls;
- while ((*ls)->prev != NULL)
- *ls = (*ls)->prev;
- return tmp;
- }
- int add (int ind, struct stack_n **top, struct list **ls )
- {
- int max_ind = index ( *ls );
- if (ind > max_ind)
- {
- return 0;
- }
- struct list *p;
- p = *ls;
- *ls = (struct list *)malloc ( sizeof ( struct list ) );
- (*ls)->stacks = *top;
- (*ls)->next = p;
- (*ls)->prev = NULL;
- p->prev = *ls;
- if (ind != 0 && max_ind != ind)
- {
- struct list *tmp_p = find ( ind, ls );
- struct list *tmp_m = find ( ind - 1, ls );
- p->prev = NULL;
- tmp_m->prev = *ls;
- tmp_p->next = *ls;
- (*ls)->next = tmp_m;
- (*ls)->prev = tmp_p;
- }
- if (ind == 0 && max_ind != 0)
- {
- struct list *tmp = find ( 0, ls );
- (*ls)->prev = tmp;
- (*ls)->next = NULL;
- p->prev = NULL;
- tmp->next = *ls;
- }
- while ((*ls)->prev != NULL)
- *ls = (*ls)->prev;
- return 1;
- }
- bool sub ( int ind, struct list **ls )
- {
- int max_ind = index ( *ls ) - 1;
- if (ind > max_ind)
- {
- return false;
- }
- struct list *p;
- int to_back = max_ind - ind;
- while (to_back > 0)
- {
- *ls = (*ls)->next;
- to_back--;
- }
- p = *ls;
- if (ind == 0 && max_ind == 0)
- {
- (*ls)->stacks = NULL;
- (*ls)->next = NULL;
- (*ls)->prev = NULL;
- return true;
- }
- if (ind == 0)
- {
- (*ls)->prev->next = NULL;
- }
- else if (ind == max_ind)
- {
- *ls = (*ls)->next;
- (*ls)->prev = NULL;
- }
- else
- {
- (*ls)->next->prev = (*ls)->prev;
- (*ls)->prev->next = (*ls)->next;
- }
- while ((*ls)->prev != NULL)
- *ls = (*ls)->prev;
- free ( p );
- return true;
- }
- void push ( int element, struct stack_n **top )
- {
- struct stack_n *p;
- p = *top;
- *top = (struct stack_n *)malloc ( sizeof ( struct stack_n ) );
- (*top)->number = element;
- (*top)->next = p;
- }
- int pop ( struct stack_n **top )
- {
- int element = INT_MAX;
- struct stack_n *p;
- if (*top != NULL)
- {
- p = *top;
- element = (*top)->number;
- *top = (*top)->next;
- free ( p );
- }
- return element;
- }
- void print ( struct list *ls )
- {
- int max_ind = index ( ls );
- if (max_ind == 0)
- {
- printf ( "Empty!\n" );
- }
- else
- {
- int p;
- struct list *tmp_list;
- tmp_list = ls;
- while (max_ind--)
- {
- struct stack_n *tmp_stack;
- tmp_stack = ls->stacks;
- printf ( "L.%d --> ", max_ind );
- while (ls->stacks != NULL) {
- p = ls->stacks->number;
- printf ( "%d ", p );
- ls->stacks = ls->stacks->next;
- }
- ls->stacks = tmp_stack;
- printf ( "\n" );
- ls = ls->next;
- }
- ls = tmp_list;
- printf ( "----------------------------------------------||\n\n" );
- }
- }
- int main ( ) {
- struct stack_n *top = NULL;
- push ( 1, &top );
- push ( 2, &top );
- push ( 3, &top );
- struct stack_n *top2 = NULL;
- push ( 4, &top2 );
- push ( 5, &top2 );
- push ( 6, &top2 );
- struct list *ls = ( struct list * )malloc ( sizeof ( struct list ) );
- ls->stacks = NULL;
- ls->next = NULL;
- ls->prev = NULL;
- add ( 0, &top, &ls );
- add ( 1, &top2, &ls );
- struct stack_n *top4 = NULL;
- push ( 7, &top4 );
- push ( 8, &top4 );
- push ( 9, &top4 );
- add ( 1, &top4, &ls );
- struct list *top3 = find ( 1, &ls );
- print ( ls );
- sub ( 0, &ls );
- print ( ls );
- int s = index ( ls );
- print ( top3 );
- push ( 55, &(ls->stacks) );
- print ( ls );
- pop ( &(ls->stacks) );
- pop ( &(ls->stacks) );
- print ( ls );
- sub ( 1, &ls );
- sub ( 0, &ls );
- print ( ls );
- free ( ls );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement