编译失败[错误] ld返回1退出状态

时间:2014-08-15 16:17:13

标签: c data-structures linked-list

当我运行它时,该程序(应该实现一个链表)终止,说id返回1退出状态。 我认为问题在于链接。我做错了还是错过了什么?

#include<stdio.h>
#include<stdlib.h>   

struct link{
    int data;
    struct link *next;
};




struct link** create(int value, struct link** head);
void print(struct link** head);  



struct link** create(int value,struct link** head)
{
    struct link* newnode = (struct link*) malloc(sizeof(struct link));
    struct link* temp=*head;

    if(head==NULL)
    {
        newnode->data=value;
        newnode->next=NULL;
        *head=newnode;
    }
    else{

        while((temp->next)!=NULL)
        {
            temp=temp->next;
        }

        newnode->data=value;
        temp->next=newnode;
        newnode->next=NULL;
    }

    print(head);
    return head;
}




void print(struct link** head)
{
    struct link* temp=*head;
    while(temp!=NULL)
    {
        printf("%d------>",temp->data);
        temp=temp->next;
    }
}




int main()
{
    struct link** head=NULL;
    int choice,value;

    do{
        printf("the no. wanted to enter into the list:");
        scanf("%d", &value);
        create(value,head);

        printf("want to enter more press y or Y");

        fflush(stdin);
        scanf("%c", &choice);
    }while(choice=='y' || choice=='Y');

    getch();
}

2 个答案:

答案 0 :(得分:1)

真的?

while(choice=='y' or choice=='Y');
                  ^^

以下是正确的:

while(choice=='y' || choice=='Y');
                  ^^

注意:or可以在C ++中使用(如:abelenky所述)

答案 1 :(得分:0)

更改为

at main

struct link *head=NULL;

char choice;

create(value, &head);

创建

if(*head==NULL) // or if(temp==NULL)