首页 > 解决方案 > 如何在链表的末尾添加一个节点?

问题描述

首先,在你们告诉我之前,我检查了本网站上的其他问题并按照说明进行操作。我的程序仍然有错误。

这是我制作的代码,但它根本没有开始

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

typedef struct node {
    int num;
    struct node *next;
}Tnode;

Tnode *head;

typedef Tnode *Tlist;

void insert(int);

void printlist();

Tnode *makenode(int x);

Tlist MakeList ();

void InsertAtEnd (Tlist list,int x);

void infoPrint (int info);

void PrintList(Tlist list);

int main(int argc, char** argv) {
    int n,i,x;
    Tlist list=MakeList();
    printf("Creiamo una lista; quanti elementi vuoi inserire ? ");//translation "how many elements in the list ?"

    scanf("%d",&n);

    for(i=0;i<n;i++){
        printf("\n Inserisci valore da inserire ");//translation "insert the element" 
        scanf("%d",&x);
        InsertAtEnd(list,x);
        PrintList(list);
    }
    return (EXIT_SUCCESS);
}

Tlist MakeList (){
return NULL;
}

void insert(int x){
    Tnode *temp= makenode(x);
    temp->next=head;
    head=temp;
    }

Tnode *makenode(int x){
    Tnode *new=malloc(sizeof(Tnode));
    if (new==NULL)
        return NULL;
    new->num=x;
    new->next=NULL;
    printf(".");
    return new;
}

void infoPrint (int info) {
    printf (" %d ", info);
}

void PrintList(Tlist list){
    Tnode *node=list;
    while(node!=NULL){
        infoPrint(node->num);
        node=node->next;
    }
}

void InsertAtEnd (Tlist head,int x){
    Tnode *newNode,*tmp;
    newNode=makenode(x);
    tmp=head;

    while(tmp->next!=NULL){
        tmp=tmp->next;
        tmp->next=newNode;

    }
}

当我构建它时,有 0 个问题。当我运行它时,它会在我插入列表的第一个值时立即停止。我如何使它工作?

标签: cliststructnodes

解决方案


您的程序具有返回 NULL 的 MakeList 函数。

这个 NULL 被分配给列表。

然后,您将列表发送到 insertAtEnd 函数。

在评估 时while statement,它会转储核心(分段错误)并且程序退出。

这就是您的程序突然退出的原因。

制作以下模组——

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

typedef struct node {
    int num;
    struct node *next;
}Tnode;

Tnode *head;

typedef Tnode *Tlist;

void insert(int);

void printlist();

Tnode *makenode(int x);

Tlist MakeList ();

Tlist InsertAtEnd (Tlist list,int x); // changed prototype

void infoPrint (int info);

void PrintList(Tlist list);

int main(int argc, char** argv) {
    int n,i,x;
    Tlist list=MakeList();
    printf("Creiamo una lista; quanti elementi vuoi inserire ? ");//translation "how many elements in the list ?"

    scanf("%d",&n);

    for(i=0;i<n;i++){
        printf("\n Inserisci valore da inserire ");//translation "insert the element"
        scanf("%d",&x);
        list = InsertAtEnd(list,x); // changed call to function...
        PrintList(list);
    }
    return (EXIT_SUCCESS);
}

Tlist MakeList (){
return NULL;
}

void insert(int x){
    Tnode *temp= makenode(x);
    temp->next=head;
    head=temp;
    }

Tnode *makenode(int x){
    Tnode *new=malloc(sizeof(Tnode));
    if (new==NULL)
        return NULL;
    new->num=x;
    new->next=NULL;
    printf(".");
    return new;
}

void infoPrint (int info) {
    printf (" %d ", info);
}

void PrintList(Tlist list){
    Tnode *node=list;
    while(node!=NULL){
        infoPrint(node->num);
        node=node->next;
    }
}

Tlist InsertAtEnd (Tlist head,int x){
    Tnode *newNode,*tmp;
    newNode=makenode(x);
    tmp=head;

    if (tmp == NULL)
        head = newNode;
    else
    {
            while(tmp->next!=NULL){
                    tmp=tmp->next;
            }
            tmp->next = newNode;
    }
    return head;
}


推荐阅读