gpt4 book ai didi

C 用链表排序不能正确排序

转载 作者:行者123 更新时间:2023-11-30 14:54:53 25 4
gpt4 key购买 nike

我试图在上一个函数中对链接列表中的数据进行排序,但由于某种原因,它不是按所选标准排序,而是简单地将最新输入的帖子数据放在顶部并对数据进行排序,但将其分配给发错帖子了。我不知道为什么,我也不知道我在做什么。我知道代码不漂亮,我还没有机会清理它。所有其他功能均正常工作。我已经提供了运行示例。我对这个级别的编程非常陌生。有什么想法吗?

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

4

Add a Post Author: Bob Date of Post(YYYYMMDD): 20121205 Number of Likes: 56 Number of Comments: 23 Enter size of post: 222

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

4

Add a Post Author: Karen Date of Post(YYYYMMDD): 20170513 Number of Likes: 57 Number of Comments: 60 Enter size of post: 2222

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

7

Sort posts by: 1. Likes 2. Size 3. Date 4. Number of Comments 1

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

1

Author: Karen Date: 20170513 Likes: 56 Number of Comments: 60 Size: 2222

Author: Bob Date: 20121205 Likes: 57 Number of Comments: 23 Size: 222

    struct post {
char author[50];
int date;
int likes;
int comments;
int length;
struct post *next;
};

typedef struct post Post;

int menu();
void printList(Post *List);
void searchList(Post *List);
int numPosts(Post *List);
Post *addToList(Post *List);
void deleteAPost(Post **List);
void deleteAllPosts(Post **List);
void sort(Post **List);

int main(void) {

int menuSelection = 0;
Post *LIST = NULL;

menuSelection = menu();

while(menuSelection >= 1) {
switch(menuSelection) {
case 1 : printList(LIST);
break;
case 2 : searchList(LIST);
break;
case 3 : printf("Number of stored posts: %d", numPosts(LIST));
break;
case 4 : LIST = addToList(LIST);
break;
case 5 : deleteAPost(&LIST);
break;
case 6 : deleteAllPosts(&LIST);
break;
case 7 : sort(&LIST);
break;
case 8 : printf("Program terminated. Have a lovely day!\n"); exit(0);
default: printf("Invalid!");
}
menuSelection = menu();
}

if(LIST) free(LIST);
return 0;
}


int menu(void) {

int menuSelection = 0;

printf("\n***Welcome! Please make a selection to continue***\n");
printf("1-Display the stored posts\n");
printf("2-Display the first post with a given attribute value\n");
printf("3-Display the current total number of stored posts\n");
printf("4-Store the data of a new post\n");
printf("5-Delete a post either by name or by author\n");
printf("6-Delete all the stored posts\n");
printf("7-Sort the post based on one of the following post attributes: number of likes, number of comments, date, or post size\n");
printf("8-Exit\n");
printf("\n");
scanf("%d", &menuSelection);
printf("\n");

return menuSelection;
}
void sort(Post **List){
int choice = 0;
printf("Sort posts by:\n1. Likes\n2. Size\n3. Date\n 4. Number of Comments\n");
scanf(" %d",&choice);

if (choice == 1){
if((*List)== NULL || (*List)->next == NULL) {
return;
}

Post *t1 = (*List)->next;

while(t1 != NULL) {
int like = t1->likes;
int found = 0;
Post *t2 = *List;

while(t2 != t1) {
if(t2->likes > t1->likes && found == 0) {
like = t2->likes;
t2->likes = t1->likes;
found = 1;
t2 = t2->next;
} else {
if(found == 1) {
int temp = like;
like = t2->likes;
t2->likes = temp;
}
t2 = t2->next;
}
}
t2->likes = like;
t1 = t1->next;
}
}

最佳答案

您的排序算法修改了节点:它交换了 likes成员,但不包括结构的其余部分。正如您在输出中看到的那样,节点已损坏。

您还应该交换其他成员(next 指针除外),或者使用不修改对象而仅更改其顺序(即仅更改 next 指针)的算法。

您应该发布结构定义以及读取、排序和写入数据的主函数,以便堆栈溢出的读者可以全面了解您的问题并找到错误和解决方案。

关于C 用链表排序不能正确排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46435460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com