首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在第二次显示嵌套列表的元素时丢失这些元素

在第二次显示嵌套列表的元素时丢失这些元素
EN

Stack Overflow用户
提问于 2019-06-02 22:23:42
回答 1查看 34关注 0票数 0

我正在尝试使用嵌套列表来构建一个“库”。一切正常,我可以使用我的排序添加函数添加元素,直到我注意到,如果我连续两次显示列表,第一次一切都很好,但在第二次中,添加到目录中的"book元素“不显示,就像它们不存在一样。

代码语言:javascript
运行
复制
struct book
{
    char *title;
    int number;
    char *country;
    struct book* new;
};

struct catalog
{
    char *name;
    struct catalog* next;
    struct book* firstbook;
};

void printList(struct catalog *head)
{
    struct catalog *temp = head;
    while(temp != NULL)
    {
        if(temp->firstbook == NULL)
        {
            printf("%s\n", temp->name);

        }
        while(temp->firstbook != NULL)
        {   printf("%s  ", temp->name);
            printf("%s  ", temp->firstbook->title);
            printf("%d  ", temp->firstbook->number);
            printf("%s\n", temp->firstbook->country);
            temp->firstbook = temp->firstbook->new;
        }
        temp = temp->next;
    }
}

struct book *newbook(char *booktitle, int number, char *country)
{
    struct book* newbook = (struct book*) malloc(sizeof(struct book));
    newbook->title = malloc(sizeof(strlen(booktitle)+1));
    newbook->country = malloc(sizeof(strlen(country)+1));
    newbook->title = booktitle;
    newbook->country = country;
    newbook->number = number;
    newbook->new = NULL;
}

struct catalog *findcatalog(struct catalog** head, char *catalogname)
{
    struct catalog* current;
    current = *head;
    while(current != NULL)
    {
        if(current->name == catalogname)
        {
            return current;
        }
        current = current->next;
    }
}

void sortedBookInsert(struct catalog** head, char *catalogname, char *booktitle, int number, char *country)
{
    struct catalog* searched;
    struct book* pom;
    struct book* ksiazka = newbook(booktitle, number, country);
    searched = findcatalog(head, catalogname);
    if(searched->firstbook == NULL || strcmp(searched->firstbook->title, ksiazka->title)>0)
    {
        ksiazka->new =searched->firstbook;
        searched->firstbook = ksiazka;
    }
    else
    { pom = searched->firstbook;
        while(pom->new!= NULL && strcmp(searched->firstbook->title, ksiazka->title)< 0)
        {
            pom = pom->new;
        }
        ksiazka->new = pom->new;
        pom->new = ksiazka;
    }
}

void sortedInsert(struct catalog** head,char *name)
{
    struct catalog* current;
    struct catalog* new_node = newcatalog(name);

    if (*head == NULL || strcmp((*head)->name, new_node->name) > 0)
    {
        new_node->next = *head;
        *head = new_node;
    }
    else
    {
        current = *head;
        while (current->next!=NULL && strcmp(current->next->name, new_node->name) < 0)
        {
            current = current->next;
        }
        new_node->next = current->next;
        current->next = new_node;
    }
}

int main() {

代码语言:javascript
运行
复制
struct catalog* head = NULL;
sortedInsert(&head, "Kappa");
sortedInsert(&head, "Aaaaaa");
sortedInsert(&head, "Sdafscx");
sortedInsert(&head, "Saxzxc");
sortedInsert(&head, "Asdas");
sortedInsert(&head, "Zzzzzzzz");
sortedInsert(&head, "Country");
sortedBookInsert(&head, "Country", "PKP", 11111, "Germany");
sortedBookInsert(&head, "Country", "Polacy", 11112, "Italy");
sortedBookInsert(&head, "Country", "Autobusy", 11234, "France");
sortedBookInsert(&head, "Country", "Polityka", 14111, "Russia");
printList(head);
printList(head);
return 0;

}

代码语言:javascript
运行
复制
When I print my list for a second time, elements from SortedBookInsert just... are not there. I am very confused.
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56415916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档