如何在链接列表的末尾添加节点?

问题描述 投票:0回答:2

首先,在您告诉我之前,我检查了此站点上的其他问题,然后按照说明进行操作。我的程序仍然存在错误。

这是我编写的代码,但是根本没有开始

#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个问题。当我运行它时,只要插入列表的fisrt值,它就会停止。我如何使其工作?

c list struct nodes
2个回答
1
投票

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

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


0
投票

功能InsertAtEnd出了问题。

您不关心第一个元素的大小写。为此,需要修改功能定义。您可以将双指针指向head到此函数。

此外,tmp->next=newNode;的分配应在for循环之后。

您应避免创建指针类型TList,将指针传递到Tnode会更整洁和容易。>

void InsertAtEnd (Tnode ** 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;

    }
}

// in main call it with
InsertAtEnd(&list,x);

© www.soinside.com 2019 - 2024. All rights reserved.