Отсортировать список по возрастанию, а затем сформировать новый список - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Дан список с элементами целого типа. Отсортировать его по возрастанию, а затем сформировать новый список, состоящий из нечетных элементов исходного списка . Вывести на экран исходный список до сортировки и после сортировки, а так же полученный список порядковых номеров. помогите наладите функцию сортировки )
Листинг программы
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. typedef struct Item
  4. {
  5. struct Item* prev;
  6. struct Item* next;
  7. int data;
  8. } Item;
  9. //функции работы со строкой
  10. Item* CreateList();
  11. Item* GetList();
  12. int AddSymbol(Item*, int);
  13. void PrintList(Item*);
  14. void DeleteList(Item*);
  15. int getInt(int*);
  16. int main() {
  17. char ex = 'Y';
  18. int i = 0;
  19. int buf = 0;
  20. Item* I;
  21. Item* I1;
  22. Item* Temp;
  23. Item* Temp2;
  24. Item* min;
  25. while (ex == 'Y' || ex == 'y') {
  26. puts("Enter list");
  27. I = GetList();
  28. I1 = CreateList();
  29. Temp = I;
  30. Temp2 = I;
  31. i = 0;
  32. PrintList(I);
  33. while (Temp->next)
  34. {
  35. if (Temp->next->data % 2)
  36. AddSymbol(I1, i);
  37. i++;
  38. Temp = Temp->next;
  39. }
  40. Temp = I;
  41. while (Temp->next)
  42. {
  43. Temp2 = Temp;
  44. min = Temp;
  45. while (Temp2->next)
  46. {
  47. if (Temp2->next->data < min->next->data)
  48. min = Temp2;
  49. Temp2 = Temp2->next;
  50. }
  51. if (min != Temp)
  52. {
  53. buf = min->next->data;
  54. min->next->data = Temp->next->data;
  55. Temp->next->data = buf;
  56. }
  57. Temp = Temp->next;
  58. }
  59. PrintList(I);
  60. PrintList(I1);
  61. DeleteList(I);
  62. DeleteList(I1);
  63. puts("If you want to continue, enter Y, else enter N");
  64. ex = 'i';
  65. while (ex != 'Y' && ex != 'y' && ex != 'N' && ex != 'n')
  66. scanf("%c", &ex);
  67. }
  68. return 0;
  69. }
  70. //////////функци работы со списком/////////
  71. Item* CreateList() {
  72. Item* S = (Item*)calloc(1, sizeof(Item));
  73. S->data = 0;
  74. S->next = NULL;
  75. S->prev = NULL;
  76. return S;
  77. }
  78. Item* GetList() {
  79. int Buf = 0;
  80. int res = 0;
  81. int i;
  82. Item* List = CreateList();
  83. while (res != -1) {
  84. res = getInt(&Buf);
  85. if (res > 0)
  86. AddSymbol(List, Buf);
  87. }
  88. return List;
  89. }
  90. int AddSymbol(Item* String, int Buf) {
  91. if (String) {
  92. while (String->next) {
  93. String = String->next;
  94. }
  95. String->next = (Item*)calloc(1, sizeof(Item));
  96. String->next->prev = String;
  97. String->next->next = NULL;
  98. String->next->data = Buf;
  99. return 0;
  100. }
  101. else {
  102. return 1;
  103. }
  104. }
  105. void PrintList(Item* S) {
  106. if (S) {
  107. S = S->next;
  108. while (S) {
  109. printf("%d ", S->data);
  110. S = S->next;
  111. }
  112. printf("\n");
  113. }
  114. }
  115. void DeleteList(Item* S) {
  116. if (S&&S->next) {
  117. while (S->next) {
  118. S = S->next;
  119. }
  120. S = S->prev;
  121. while (S->prev) {
  122. free(S->next);
  123. S = S->prev;
  124. }
  125. free(S->next);
  126. }
  127. }
  128. int getInt(int* d)
  129. {
  130. int n;
  131. do
  132. {
  133. n = scanf("%d", d);
  134. if (n == 0)
  135. {
  136. puts("Incorrect input.");
  137. scanf("%*c");
  138. }
  139. } while (n == 0);
  140. return n;
  141. }

Решение задачи: «Отсортировать список по возрастанию, а затем сформировать новый список»

textual
Листинг программы
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4.  
  5. struct item {
  6.     struct item* next;
  7.     struct item* prev;
  8.     int data;
  9. }; 
  10.  
  11. typedef struct list {
  12.     struct item* head;
  13.     struct item* tail;
  14. } list_t;
  15.  
  16. void list_init(list_t* lst);
  17. int  list_add(list_t* lst, int data);
  18. void list_clear(list_t* lst);
  19. void list_swap(list_t* lst, struct item* p1, struct item* p2);
  20.  
  21. //сортировка выбором
  22. void list_sort(list_t* lst){
  23.     struct item* p, *k, *i;
  24.     for(p = lst->head; p != NULL; p = p->next){
  25.         k = p;
  26.         for(i = p->next; i != NULL; i = i->next){
  27.             if(i->data < k->data)
  28.                 k = i;
  29.         }
  30.         if(k != p){
  31.             list_swap(lst, k, p);
  32.             p = k;
  33.         }
  34.     }
  35. }
  36.  
  37. int main(void){
  38.     int    i;
  39.     const struct item* p;
  40.     list_t lst;
  41.     list_init(&lst);
  42.    
  43.     for(i = 0; i < 20; ++i)
  44.         list_add(&lst, rand() % 10);
  45.  
  46.     for(p = lst.head; p != NULL; p = p->next)
  47.         printf("%d ", p->data);
  48.     putchar('\n');
  49.     for(p = lst.tail; p != NULL; p = p->prev)
  50.         printf("%d ", p->data);
  51.     puts("\n\n");
  52.    
  53.     list_sort(&lst);
  54.  
  55.     //вывод после сортировки
  56.     for(p = lst.head; p != NULL; p = p->next)
  57.         printf("%d ", p->data);
  58.     putchar('\n');
  59.     for(p = lst.tail; p != NULL; p = p->prev)
  60.         printf("%d ", p->data);
  61.     putchar('\n');
  62.     list_clear(&lst);
  63.     return 0;
  64. }
  65.  
  66. //инициализация
  67. void list_init(list_t* lst){
  68.     lst->head = lst->tail = NULL;
  69. }
  70.  
  71. //добавление в конец списка
  72. int list_add(list_t* lst, int data){
  73.     struct item* p = (struct item*)malloc(sizeof(struct item));
  74.     if(p != NULL){
  75.         p->data = data;
  76.         p->next = p->prev = NULL;
  77.         if(lst->head == NULL)
  78.             lst->head = lst->tail = p;
  79.         else {
  80.             p->prev   = lst->tail;
  81.             lst->tail->next = p;
  82.             lst->tail = p;
  83.         }
  84.     }
  85.     return (p != NULL);
  86. }
  87.  
  88. //удаление всех
  89. void list_clear(list_t* lst){
  90.     struct item* t;
  91.     while(lst->head != NULL){
  92.         t = lst->head;
  93.         lst->head = lst->head->next;
  94.         free(t);
  95.     }
  96.     lst->tail = NULL;
  97. }
  98.  
  99. // обмен
  100. void list_swap(list_t* lst, struct item* p1, struct item* p2){
  101.     struct item* r1, *r2, *t1, *t2;
  102.     r1 = p1->prev;
  103.     r2 = p2->prev;
  104.  
  105.     if(r1 != NULL) r1->next = p2;
  106.     p2->prev = r1;
  107.  
  108.     if(r2 != NULL) r2->next = p1;
  109.     p1->prev = r2;
  110.  
  111.     t1 = p1->next;
  112.     t2 = p2->next;
  113.                
  114.     p1->next = p2->next;
  115.     if(t2 != NULL) t2->prev = p1;
  116.  
  117.     p2->next = t1;
  118.     if(t1 != NULL) t1->prev = p2;
  119.  
  120.     if(lst->head == p1)
  121.         lst->head = p2;
  122.     else if(lst->head == p2)
  123.         lst->head = p1;
  124.  
  125.     if(lst->tail == p1)
  126.         lst->tail = p2;
  127.     else if(lst->tail == p2)
  128.         lst->tail = p1;
  129. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 3.429 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы