Отсортировать список по возрастанию, а затем сформировать новый список - C (СИ)
Формулировка задачи:
Дан список с элементами целого типа. Отсортировать его по возрастанию, а затем сформировать новый список, состоящий из нечетных элементов исходного списка . Вывести на экран исходный список до сортировки и после сортировки, а так же полученный список порядковых номеров.
помогите наладите функцию сортировки )
Листинг программы
- #include "stdio.h"
- #include "malloc.h"
- typedef struct Item
- {
- struct Item* prev;
- struct Item* next;
- int data;
- } Item;
- //функции работы со строкой
- Item* CreateList();
- Item* GetList();
- int AddSymbol(Item*, int);
- void PrintList(Item*);
- void DeleteList(Item*);
- int getInt(int*);
- int main() {
- char ex = 'Y';
- int i = 0;
- int buf = 0;
- Item* I;
- Item* I1;
- Item* Temp;
- Item* Temp2;
- Item* min;
- while (ex == 'Y' || ex == 'y') {
- puts("Enter list");
- I = GetList();
- I1 = CreateList();
- Temp = I;
- Temp2 = I;
- i = 0;
- PrintList(I);
- while (Temp->next)
- {
- if (Temp->next->data % 2)
- AddSymbol(I1, i);
- i++;
- Temp = Temp->next;
- }
- Temp = I;
- while (Temp->next)
- {
- Temp2 = Temp;
- min = Temp;
- while (Temp2->next)
- {
- if (Temp2->next->data < min->next->data)
- min = Temp2;
- Temp2 = Temp2->next;
- }
- if (min != Temp)
- {
- buf = min->next->data;
- min->next->data = Temp->next->data;
- Temp->next->data = buf;
- }
- Temp = Temp->next;
- }
- PrintList(I);
- PrintList(I1);
- DeleteList(I);
- DeleteList(I1);
- puts("If you want to continue, enter Y, else enter N");
- ex = 'i';
- while (ex != 'Y' && ex != 'y' && ex != 'N' && ex != 'n')
- scanf("%c", &ex);
- }
- return 0;
- }
- //////////функци работы со списком/////////
- Item* CreateList() {
- Item* S = (Item*)calloc(1, sizeof(Item));
- S->data = 0;
- S->next = NULL;
- S->prev = NULL;
- return S;
- }
- Item* GetList() {
- int Buf = 0;
- int res = 0;
- int i;
- Item* List = CreateList();
- while (res != -1) {
- res = getInt(&Buf);
- if (res > 0)
- AddSymbol(List, Buf);
- }
- return List;
- }
- int AddSymbol(Item* String, int Buf) {
- if (String) {
- while (String->next) {
- String = String->next;
- }
- String->next = (Item*)calloc(1, sizeof(Item));
- String->next->prev = String;
- String->next->next = NULL;
- String->next->data = Buf;
- return 0;
- }
- else {
- return 1;
- }
- }
- void PrintList(Item* S) {
- if (S) {
- S = S->next;
- while (S) {
- printf("%d ", S->data);
- S = S->next;
- }
- printf("\n");
- }
- }
- void DeleteList(Item* S) {
- if (S&&S->next) {
- while (S->next) {
- S = S->next;
- }
- S = S->prev;
- while (S->prev) {
- free(S->next);
- S = S->prev;
- }
- free(S->next);
- }
- }
- int getInt(int* d)
- {
- int n;
- do
- {
- n = scanf("%d", d);
- if (n == 0)
- {
- puts("Incorrect input.");
- scanf("%*c");
- }
- } while (n == 0);
- return n;
- }
Решение задачи: «Отсортировать список по возрастанию, а затем сформировать новый список»
textual
Листинг программы
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- struct item {
- struct item* next;
- struct item* prev;
- int data;
- };
- typedef struct list {
- struct item* head;
- struct item* tail;
- } list_t;
- void list_init(list_t* lst);
- int list_add(list_t* lst, int data);
- void list_clear(list_t* lst);
- void list_swap(list_t* lst, struct item* p1, struct item* p2);
- //сортировка выбором
- void list_sort(list_t* lst){
- struct item* p, *k, *i;
- for(p = lst->head; p != NULL; p = p->next){
- k = p;
- for(i = p->next; i != NULL; i = i->next){
- if(i->data < k->data)
- k = i;
- }
- if(k != p){
- list_swap(lst, k, p);
- p = k;
- }
- }
- }
- int main(void){
- int i;
- const struct item* p;
- list_t lst;
- list_init(&lst);
- for(i = 0; i < 20; ++i)
- list_add(&lst, rand() % 10);
- for(p = lst.head; p != NULL; p = p->next)
- printf("%d ", p->data);
- putchar('\n');
- for(p = lst.tail; p != NULL; p = p->prev)
- printf("%d ", p->data);
- puts("\n\n");
- list_sort(&lst);
- //вывод после сортировки
- for(p = lst.head; p != NULL; p = p->next)
- printf("%d ", p->data);
- putchar('\n');
- for(p = lst.tail; p != NULL; p = p->prev)
- printf("%d ", p->data);
- putchar('\n');
- list_clear(&lst);
- return 0;
- }
- //инициализация
- void list_init(list_t* lst){
- lst->head = lst->tail = NULL;
- }
- //добавление в конец списка
- int list_add(list_t* lst, int data){
- struct item* p = (struct item*)malloc(sizeof(struct item));
- if(p != NULL){
- p->data = data;
- p->next = p->prev = NULL;
- if(lst->head == NULL)
- lst->head = lst->tail = p;
- else {
- p->prev = lst->tail;
- lst->tail->next = p;
- lst->tail = p;
- }
- }
- return (p != NULL);
- }
- //удаление всех
- void list_clear(list_t* lst){
- struct item* t;
- while(lst->head != NULL){
- t = lst->head;
- lst->head = lst->head->next;
- free(t);
- }
- lst->tail = NULL;
- }
- // обмен
- void list_swap(list_t* lst, struct item* p1, struct item* p2){
- struct item* r1, *r2, *t1, *t2;
- r1 = p1->prev;
- r2 = p2->prev;
- if(r1 != NULL) r1->next = p2;
- p2->prev = r1;
- if(r2 != NULL) r2->next = p1;
- p1->prev = r2;
- t1 = p1->next;
- t2 = p2->next;
- p1->next = p2->next;
- if(t2 != NULL) t2->prev = p1;
- p2->next = t1;
- if(t1 != NULL) t1->prev = p2;
- if(lst->head == p1)
- lst->head = p2;
- else if(lst->head == p2)
- lst->head = p1;
- if(lst->tail == p1)
- lst->tail = p2;
- else if(lst->tail == p2)
- lst->tail = p1;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д