c - 如何在链表末尾添加节点?

标签 c list struct nodes

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

这是我编写的代码,但它根本没有启动

#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;

    }
}

当我构建它时,没有任何问题。当我运行它时,一旦我插入列表的第一个值,它就会停止。 我该如何让它发挥作用?

最佳答案

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

这个 NULL 正在被分配给列表。

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

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

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

制作以下模组 --

#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;
}

关于c - 如何在链表末尾添加节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610948/

相关文章:

c - 如何在 Windows 上用 C 语言将这种阻塞 I/O 模式转换为重叠 I/O 模式?

python - 从文件中写入和读取列表

java - 在Python中迭代字典、列表

c - 如何在指针数组中传递列表

c++ - 在另一个构造函数中调用对象数组的构造函数

c - 用C模拟数据库

c++ - 如何计算与服务器的 SYN/ESTABLISHED 连接?

C 中的条件运算符

c - 将结构传递给函数的正确方法是什么?

转换返回浮点类型的函数的返回值