gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:32:56 27 4
gpt4 key购买 nike

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

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com