c - 如何在链表的末尾添加一个节点?
问题描述
首先,在你们告诉我之前,我检查了本网站上的其他问题并按照说明进行操作。我的程序仍然有错误。
这是我制作的代码,但它根本没有开始
#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 个问题。当我运行它时,它会在我插入列表的第一个值时立即停止。我如何使它工作?
解决方案
您的程序具有返回 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;
}
推荐阅读
- java - LoggedErrorException:发现数据绑定错误
- powershell - 如何通过 Invoke-Command 运行 CMD
- javascript - 我可以将我的导入语句提取到不同的文件吗?
- razor - 如何在不覆盖样式类的情况下向 Sitecore MVC 链接字段添加类属性
- python-3.x - 如何使用 Python3 将目录中的多个 CSV 文件转换为管道分隔的文本文件
- python - 基本的 Google API Oauth2 身份验证设置无法提供对象?
- llvm - LLVM StructType isPacked()
- azure - 使用 ARM 模板创建 Azure 函数时处理连接字符串和主键的最佳方式
- c - 警告:ISO C90 禁止混合声明和代码 [-Wdeclaration-after-statement]
- visual-studio - 固定 Fortran 延续行中无法识别的标记“&”