Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
当我运行该程序时(应该实现一个链表)终止,并说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();
}

最佳答案

改成

在主要

struct link *head=NULL;

char choice;

create(value, &head);


在创建

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

07-27 15:51