Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- typedef struct node_s
- {
- int data;
- struct node_s *next, *prev; // previous
- } node_s;
- node_s *init(int a);
- void print_list(node_s *lst);
- void add(node_s *lst, int a);
- void destruct(node_s *lst);
- void sort_list(node_s *lst)
- {
- node_s *p1 = lst, *p2 = lst, *p3;
- int temp;
- do
- {
- p2 = lst;
- do
- {
- p3 = p2->next;
- if (p2->data > p3->data)
- {
- temp = p2->data;
- p2->data = p3->data;
- p3->data = temp;
- }
- p2 = p2->next;
- } while (p2 != lst);
- p1 = p1->next;
- printf("\n");
- } while (p1 != lst);
- }
- int main(void)
- {
- int data;
- node_s *temp;
- printf("Введите элементы:");
- scanf("%d", &data);
- node_s *lst = init(data);
- for (int i = 0; i < 5; i++)
- {
- scanf("%d", &data);
- add(lst, data);
- }
- printf("ДО:\n");
- print_list(lst);
- puts("\n\n");
- sort_list(lst);
- puts("\n\n");
- printf("Отсортирован:\n");
- print_list(lst);
- destruct(lst);
- return 0;
- }
- void add(node_s *lst, int a)
- {
- node_s *temp = malloc(sizeof(node_s));
- temp->data = a;
- temp->next = lst->next;
- temp->prev = lst;
- node_s *lst_next = lst->next;
- lst->next = temp;
- if (lst_next)
- lst_next->prev = temp;
- // temp->ptr = lst->ptr;
- // lst->ptr = temp;
- }
- node_s *init(int a)
- {
- node_s *lst = (node_s *)malloc(sizeof(node_s));
- lst->data = a;
- lst->next = lst;
- lst->prev = lst;
- return lst;
- }
- void print_list(node_s *lst)
- {
- node_s *p = lst;
- if (!lst)
- return;
- do
- {
- printf("%d ", p->data);
- p = p->next;
- } while (p != lst);
- }
- void destruct(node_s *lst)
- {
- node_s *temp = lst;
- node_s *p = lst;
- if (!lst)
- return;
- do
- {
- temp = temp->next;
- free(p);
- p = temp;
- } while (p != lst);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement