【问题标题】:Sorting structs alphabetically按字母顺序对结构进行排序
【发布时间】:2014-06-01 18:45:51
【问题描述】:

我得到了这个代码:

#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() 中简单地使用它们

【问题讨论】:

  • 提供的代码不完整。请编辑代码以包含“main()”函数(程序开始的地方),并提出上述函数的方式。
  • AuthorRecordType 和 MemberNodeType 是指针但 AuthorType 不是?
  • 当我看到head=curr=createMember() 在一个应该写入auf 作者列表的函数中时,我有点紧张,即。以只读模式对列表进行操作。这种函数中的指针应该是指向现有节点的指针或NULL。只有在列表中插入新节点时才应创建节点。我建议您在担心排序之前先做好准备。
  • @eznme MembernodeType 和 AuthorRecordType 有一个下一节,这意味着它们需要有一个指向它们的指针,但 AuthorType 只能是每个 MemberNode 中的一个。
  • @user3697377 是的,但是如果您在 1 个月后重新阅读您的代码,您会混淆它们,因为它们的名称非常相似。我建议您要么始终将它们全部设为指针,要么将它们命名为 PAuthor 作为指针,将 StructAuthor 命名为结构。

标签: c sorting linked-list


【解决方案1】:

有一类排序算法称为online algoritms。它们最适合您的情况,因为您可能必须在列表中添加元素同时保持排序。如果您是该主题的初学者,我建议您从插入排序开始,因为它非常简单。

如果您想获得性能提升,您可以使用更高效的排序算法对初始列表进行排序(如快速排序),并使用在线排序算法在更多元素出现时保持排序。

【讨论】:

    【解决方案2】:

    您要求我们快速解决此特定问题,下次您可能会带着大量的结构层次结构再次出现,再次要求我们对其进行处理。

    这可能更有价值:尝试渐进式地工作。首先制作一个主函数和一个短链表,每个结构都只包含一个整数。然后编写一个对该链表进行排序的函数。

    这样您就不必担心有关作者、书籍、字符串以及分配和打印的所有内容。当该功能起作用时然后您可以将其更改为作者等。

    【讨论】:

      最近更新 更多