【问题标题】:dont know to create a linked list不知道创建一个链表
【发布时间】:2014-06-04 09:19:31
【问题描述】:

我正在尝试制作链表,但我无法做到这一点。当我尝试打印它时,它仍然只打印第一个节点。我知道这意味着第一个没有参考下一个。 如果有人帮助我,我会很高兴。

代码如下:

struct book {
char *author;
char *title;
int year;
struct book *next;
};

struct book *insert_books() {
char c,x;
int i=0;    
struct book *first=(struct book*)malloc(sizeof(struct book));

struct book *current=(struct book*)malloc(sizeof(struct book));

struct book *new=(struct book*)malloc(sizeof(struct book));

first->author=(char*)malloc(30*sizeof(char));

first->title=(char*)malloc(50*sizeof(char));

first->next=NULL;

i=0;
scanf("%d\n",&first->year);

c='a';

while(c!='\n') {
    c=getchar();
    first->author[i]=c; 
    i++;
        }

first->author[i]='\0';
i=0;
c='a';

while(c!='\n') {
    c=getchar();
    first->title[i]=c; 
    i++;
        }

first->author[i]='\0';
current=first;
printf("\nWanna continue?(y/n)");
scanf("%c",&x);     

while(x=='y') {
    new->author=(char*)malloc(30*sizeof(char));
    new->title=(char*)malloc(50*sizeof(char));
    new->next=NULL;
    i=0;
    scanf("%d\n",&new->year);
    c='a';

    while(c!='\n') {
        c=getchar();
        new->author[i]=c; 
        i++;
        }

    new->author[i]='\0';
    i=0;
    c='a';

    while(c!='\n') {
        c=getchar();
        new->title[i]=c; 
        i++;
        }

    new->author[i]='\0';
    current->next=new;
    current=current->next;
    printf("\nWanna continue?(y/n)");
    scanf("%c",&x); 

            } 
return first;

}

【问题讨论】:

  • 所有这些 malloc() 而不是一个 free() 伤害了我。
  • 我无法释放这个链表
  • 当你在列表中插入一个节点时,你应该只创建一个struct bookmalloc,即保存新书的结构体。所有其他指针要么指向现有节点的实例,要么应该是NULL。我经常看到链表;我想,人们被教导不要在不分配内存的情况下访问指针,但在这种情况下,这是完全错误的。
  • 另外,如果将所有输入内容与链表的实际逻辑分开,代码会更清晰。
  • 所以我必须只分配新的,即使函数必须返回对第一个节点的引用?所以第一个不必分配?对于代码中的输入内容造成的混乱,我们深表歉意。

标签: c


【解决方案1】:

您将currentnext 指针值附加到自身。所以当你调用current 的下一个book 时,它仍然是current。此外,你应该分解你的代码,而不是像

这样的冗余过程
while(c!='\n') {
    c=getchar();
    first->author[i]=c; 
    i++;
        }

最后,我认为由于您的复制/粘贴,您忘记将 first->author[i]='\0'; 更改为 first->title[i]='\0';

【讨论】:

  • 而且我认为在尝试实现链表之前,您需要了解很多东西。
  • 你能给我一些建议吗?如何在循环中正确附加下一个指针以创建链表?我已经在另一个函数中为链表实现了相同的代码,这是正确的。感谢您的提醒。是的,这是由于我的复制/粘贴。
【解决方案2】:

尝试使用 &(first->year) 代替 &first->year 并在 if 块中再次为 new 分配内存。

struct book *insert_books() {
    char c,x;
    int i=0;    
    struct book *first=(struct book*)malloc(sizeof(struct book));

struct book *current=(struct book*)malloc(sizeof(struct book));

struct book *new=(struct book*)malloc(sizeof(struct book));

first->author=(char*)malloc(30*sizeof(char));

first->title=(char*)malloc(50*sizeof(char));

first->next=NULL;

i=0;
scanf("%d\n",&(first->year));

c='a';

while(c!='\n') {
    c=getchar();
    first->author[i]=c; 
    i++;
        }

first->author[i]='\0';
i=0;
c='a';

while(c!='\n') {
    c=getchar();
    first->title[i]=c; 
    i++;
        }

first->author[i]='\0';
current=first;
printf("\nWanna continue?(y/n)");
scanf("%c",&x);     

while(x=='y') {
            new=(struct book*)malloc(sizeof(struct book));
    new->author=(char*)malloc(30*sizeof(char));
    new->title=(char*)malloc(50*sizeof(char));
    new->next=NULL;
    i=0;
    scanf("%d\n",&new->year);
    c='a';

    while(c!='\n') {
        c=getchar();
        new->author[i]=c; 
        i++;
        }

    new->author[i]='\0';
    i=0;
    c='a';

    while(c!='\n') {
        c=getchar();
        new->title[i]=c; 
        i++;
        }

    new->author[i]='\0';
    current->next=new;
    current=current->next;
    printf("\nWanna continue?(y/n)");
    scanf("%c",&x); 

            } 
return first;

}

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多