我收到了这段代码:
#include <stdio.h>
#include <stdlib.h>
struct AuthorRecord {
char *textTitle;
int NumberOfWords;
long Download;
struct AuthorRecord *next;
};
typedef struct AuthorRecord *AuthorRecordType;
typedef struct {
char *firstName;
char *lastName;
int idNumber;
AuthorRecordType text;
} AuthorType;
struct MemberNodeStruct {
AuthorType *anAuthor;
struct MemberNodeStruct *next;
};
typedef struct MemberNodeStruct *MemberNodeType;
AuthorRecordType createBook(){
/*Creates a new AuthorRecord and returns the pointer to it*/
AuthorRecordType book = (AuthorRecordType)malloc(sizeof(AuthorRecordType));
if(book==NULL) printf("cannot allocate memory for Book");
book->next=NULL;
return book;
}
AuthorType *createAuthor(){
/*Creates a new AuthorType and returns the pointer to it*/
AuthorType *p;
p=(AuthorType*)malloc(sizeof(AuthorType));
if(p==NULL) printf("cannot allocate memory for Author");
return p;
}
MemberNodeType createMember(){
/*Creates a new MemberNode and returns the pointer to it*/
MemberNodeType p;
p=(MemberNodeType)malloc(sizeof(MemberNodeType));
if(p==NULL) printf("cannot allocate memory for Member");
p->anAuthor=createAuthor();
p->next=NULL;
return p;
}
void writeAuthor(MemberNodeType p){
/*Writes the info of an Author where the pointer p points*/
scanf("%s%s%d", p->anAuthor->firstName, p->anAuthor->lastName, &p->anAuthor->idNumber);
return;
}
void writeBook(AuthorRecordType p){
/*Writes the info of a Book where the pointer p points*/
fgets (p->textTitle, 64, stdin);
scanf("%ld", &p->Download);
return;
}
void printAuthor(MemberNodeType p){
/*Prints the info of the Author where the pointer p points*/
printf("%s %s %d\n", p->anAuthor->firstName,p->anAuthor->lastName, p->anAuthor->idNumber);
return;
}
void printBook(AuthorRecordType p){
/*points the info of the Book where the pointer p points*/
printf("%s %ld\n", p->textTitle, p->Download);
return;
}
MemberNodeType writethelists(){
/*Fills the list with the given data*/
int NumberofAuthors, NumberofBooks=0 ,i, j;
MemberNodeType head=NULL, curr=NULL;
AuthorRecordType book;
printf("type number of authors \n");
scanf("%d", &NumberofAuthors);
for (i=0; i<NumberofAuthors; i++){
printf("getting into author loop \n");
if (head==NULL) {
printf("creating new member \n");
head=curr=createMember();
}
else{
printf("creating additional member \n");
curr=curr->next=createMember();
}
printf("write author info \n");
writeAuthor(curr);
if (curr->anAuthor->lastName < curr->anAuthor)
printf("type number of books \n");
scanf("%d", &NumberofBooks);
for (j=0; j<NumberofBooks; j++){
printf("books loop");
if (j==0){
printf("creating new book for author\n");
curr->anAuthor->text = book = createBook();
}
else{
printf("creating additional book\n");
book=book->next=createBook();
}
printf("write book info \n");
writeBook(book);
}
}
return head;
}
它应该把作者和他们的书带到那些结构中。困扰我的是按照他们的名字对作者进行排序。我已经编写了一些代码,我认为我现在可以在插入代码时对它们进行排序。 在插入它们之后对它们进行排序的好方法是什么?
编辑: 我知道代码不完整。我现在正在编写函数,我将稍后在main()中使用
答案 0 :(得分:0)
有一类称为online algoritms的排序算法。它们最适合您的情况,因为您可能必须在保持排序的同时向列表中添加元素。如果您是本主题的初学者,我建议从插入排序开始,因为它非常简单。
如果您希望获得性能提升,可以使用更有效的排序算法对初始列表进行排序(如quicksort),并使用在线排序算法将其排序为更多元素。
答案 1 :(得分:0)
您要求我们快速解决此特定问题,下次您可能再次使用大型结构层次结构,再次要求我们对其进行操作。
这里有一些可能更有价值的东西:尝试逐步增加工作。首先创建一个main-function和一个简短的结构列表,每个结构只包含一个整数。然后编写一个对该链表进行排序的函数。
这样你就不必担心有关作者,书籍和字符串以及分配和打印的所有内容。当该功能然后时,您可以将其更改为作者等。