gpt4 book ai didi

c - 用 C 中的结构对链表进行排序

转载 作者:行者123 更新时间:2023-12-02 03:25:37 25 4
gpt4 key购买 nike

我尝试使用我在一个程序中学到的冒泡排序方法,该程序用于对已插入链表的书籍进行排序。我试过交换 2 个节点,但这导致 nextptr 也被交换。我启动了这个名为 authsort 的新功能,它没有引用书籍结构中的成员。我需要改变什么才能使这种工作正常进行?

#include<stdlib.h>
#include <stdio.h>
#include<ctype.h>
#include<string.h>

struct book
{
char author[30];
char title[50];
char hold2[50];
char titleins[50];
char genre[15];
int rating;
int comma;
int titleshift;
int number;
};
struct listnode
{
struct book data;
struct listnode *nextptr;
};
typedef struct listnode LISTNODE;
typedef LISTNODE *LISTNODEPTR;



void insert(LISTNODEPTR *sptr, struct listnode data);
void authsort(LISTNODEPTR sptr, struct listnode data);
void freenodes(LISTNODEPTR currentptr);
int add_2 (count, i, j, index, tag);
void print_list(LISTNODEPTR, struct listnode book);
int main ()
{
LISTNODEPTR startptr = NULL;
struct listnode listnode;
struct book book;
int choice = 0;
char item;
int count = 0;
int index = 0;
int j = 0;
char * pch2;
char string[40];
char string2[40];
char * pch;
int i = 0;
while( choice != 2)
{
printf("What Would You Like To Do?\n");
printf("1.Add Books\n2.exit\n3:sort by author\n");
scanf("%d", &choice);
getchar();




switch(choice)
{
case 1:
printf("Enter your book title:");
gets(listnode.data.title);
for(i=0;i<100;i++)
{
listnode.data.title[i] = tolower(listnode.data.title[i]);
}
printf("Enter the author:");
if(listnode.data.title[0] == 't')
{
if(listnode.data.title[1] == 'h')
{
if(listnode.data.title[2] == 'e')
{
if(listnode.data.title[3] == ' ')
{
book.titleshift = 4;
pch = strtok(listnode.data.title, " ");
strcpy(string, pch);
pch2 = strtok(NULL, "");
book.comma = strlen(pch2);
strcpy(string2, pch2);
strncat(string2, string, 20);
strncpy(listnode.data.title, string2, 50);

}
}
}
}
if(listnode.data.title[0] == 'a')
{
if(listnode.data.title[1] == ' ')
{
book.titleshift = 2;
pch = strtok(listnode.data.title, " ");
strcpy(string, pch);
pch2 = strtok(NULL, "");
book.comma = strlen(pch2);
strcpy(string2, pch2);
strncat(string2, string, 20);
strncpy(listnode.data.title, string2, 50);
}
}
gets(listnode.data.author);
for(i=0;i<100;i++)
{
listnode.data.author[i] = tolower(listnode.data.author[i]);
}
printf("Enter the genre:");
gets(listnode.data.genre);

for(i=0;i<100;i++)
{
listnode.data.genre[i] = tolower(listnode.data.genre[i]);
}
printf("Enter the quality:");
scanf("%d", &listnode.data.rating);
printf("Enter the number of pages:");
scanf("%d", &listnode.data.number); // after scanning in from user, we insert and print the list
insert(&startptr, listnode);
print_list(startptr, listnode);
break;
case 2:
freenodes(startptr);
break;
case 3:
authsort(startptr, listnode);
print_list(startptr, listnode);
break;
}
}
return 0;
}


/*********************************FUNCTION*********************************/
void print_list(LISTNODEPTR currentptr, struct listnode book)
{

if (!currentptr)
printf("List is empty.\n\n");
else
{
printf("your list:\n");
while (currentptr)

{
if(currentptr->data.comma > 1)
{

printf("%-15s %-20s %-15s %-15d %-15d\n", currentptr->data.author, currentptr->data.title, currentptr->data.genre, currentptr->data.rating, currentptr->data.number);
currentptr = currentptr -> nextptr; // this will continue printing as long as there in a nextptr, or book

}

else if(currentptr->data.comma == 0)
{
printf("%-15s %-20s %-15s %-15d %-15d\n", currentptr->data.author, currentptr->data.title, currentptr->data.genre, currentptr->data.rating, currentptr->data.number);
currentptr = currentptr -> nextptr; // this will continue printing as long as there in a nextptr, or book

}
}

if(!currentptr)
{
printf("end of list\n\n");

}
}
}

/*********************FUNCTION******************/
void insert(LISTNODEPTR *sptr, struct listnode data)
{
LISTNODEPTR newptr, previousptr, currentptr;
newptr = malloc(sizeof(LISTNODE));
int i = 0;

if (newptr)
{



strcpy(newptr -> data.title,data.data.title);

//if(book.titleshift == 0)
//{

//strcpy(newptr -> title,book.title);
//}
// here the books are being copied into the current node
strcpy(newptr -> data.author, data.data.author);
strcpy(newptr -> data.genre, data.data.genre);
newptr -> data.rating= data.data.rating;
newptr -> data.number= data.data.number;
newptr -> nextptr = NULL;
previousptr = NULL;
currentptr = *sptr;

while (currentptr != NULL && strcmp(data.data.title, currentptr -> data.title)>0)
// this compares the titles to insert by author
{
previousptr = currentptr;
currentptr = currentptr -> nextptr;
}
if (previousptr == NULL)
{
newptr -> nextptr = *sptr;
*sptr = newptr;
}
else
{
previousptr -> nextptr = newptr;
newptr -> nextptr = currentptr;
}
}
else
{
printf("Not inserted.\n");
}
}

/*******************************FUNCTION***********************************/

/**************************FUNCTION*********************/
void freenodes(LISTNODEPTR currentptr)
{
LISTNODEPTR tempptr;
while(currentptr!=NULL)
{
tempptr=currentptr->nextptr;
free(currentptr);
currentptr=tempptr;
}
}

void authsort(LISTNODEPTR sptr, struct listnode data)
{
LISTNODEPTR nextptr;
LISTNODEPTR previousptr;
LISTNODEPTR currentptr;
LISTNODEPTR tempptr;
LISTNODEPTR trail;
int count = 0;
int j = 0;
while(sptr != NULL)
{
count++;
sptr = sptr -> nextptr;
}
for(j=0;j<count;j++)
{

currentptr = sptr;
while(currentptr->nextptr != NULL)
{


if(strcmp(currentptr ->data.author, currentptr->nextptr.data.author)>0)
{
tempptr = currentptr ->nextptr;
currentptr ->nextptr = currentptr ->nextptr->nextptr;
currentptr ->nextptr = currentptr;


if(currentptr == sptr)
sptr = trail = tempptr;
else
trail->nextptr = tempptr;
currentptr = tempptr;
}

trail = currentptr;
currentptr = currentptr->nextptr;
}

}



}

最佳答案

看,您的最终目标应该是拥有一个已排序的链表。现在您正在尝试对两个节点进行排序,但您甚至可以交换它们的数据!

这不涉及指针重定位,并且使您的程序更易于分析和理解。我希望这能提示您找到正确的解决方案!

快乐编码:)

关于c - 用 C 中的结构对链表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30521158/

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