不知道创建一个链表

时间:2014-06-04 09:19:31

标签: c

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

以下是代码:

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;

}

2 个答案:

答案 0 :(得分:0)

您要将next指针值current附加到自身。因此,当您拨打book的下一个current时,它仍然是current。此外,您应该对代码进行分解,而不是像

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

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

答案 1 :(得分:0)

尝试使用&(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;

}
相关问题