链表在c中的实现,运行时错误
编译代码时没有错误,但在两次输入后程序在运行时崩溃。也许有一些逻辑错误我看不出来。我试图在链表的尾部插入节点,同时只保持头部位置链表在c中的实现,运行时错误,c,pointers,data-structures,linked-list,singly-linked-list,C,Pointers,Data Structures,Linked List,Singly Linked List,编译代码时没有错误,但在两次输入后程序在运行时崩溃。也许有一些逻辑错误我看不出来。我试图在链表的尾部插入节点,同时只保持头部位置 #include<stdio.h> #include<stdlib.h> struct Node{ int data; struct Node* next; }; struct Node *head; //print the element of the lists void print(){ printf("\
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node *head;
//print the element of the lists
void print(){
printf("\nThe list from head to tail is as follows \n");
struct Node* temp = head;
while(temp!=NULL){
printf("\n %d ",(*temp).data);
temp = (*temp).next;
}
}
//insert a node at the tail of the linked list
void insert_at_tail(int data){
struct Node* temp = head;
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data=data;
new_node->next=NULL;
if(temp==NULL){
head=new_node;
}
else{
while(temp!=NULL){temp=temp->next;}
(*temp).next=new_node;
}
}
int main(){
head = NULL;
int i,data;
for(i=0;i<5;i++){
scanf("%d",&data);
insert_at_tail(data);
}
print();
return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*头部;
//打印列表中的元素
作废打印(){
printf(“\n从头到尾的列表如下\n”);
结构节点*温度=头部;
while(temp!=NULL){
printf(“\n%d”,(*临时数据);
温度=(*温度)。下一步;
}
}
//在链表的尾部插入一个节点
在尾端插入无效尾端(整型数据){
结构节点*温度=头部;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建_节点->数据=数据;
新建节点->下一步=空;
if(temp==NULL){
head=新的_节点;
}
否则{
而(temp!=NULL){temp=temp->next;}
(*temp.next=新的_节点;
}
}
int main(){
head=NULL;
int i,数据;
对于(i=0;i
也许有一些逻辑错误
对!
在这里:
您将循环,直到temp
实际为NULL
,然后请求其next
成员,因此您正在请求NULL
的next
,因此您在自找麻烦(程序崩溃)
尝试这样做:
while(temp->next != NULL) { temp=temp->next; }
循环,直到temp
指向列表的最后一个节点。更改后,代码应该可以正常工作
附言:不
也许有一些逻辑错误
对!
在这里:
您将循环,直到temp
实际为NULL
,然后请求其next
成员,因此您正在请求NULL
的next
,因此您在自找麻烦(程序崩溃)
尝试这样做:
while(temp->next != NULL) { temp=temp->next; }
循环,直到temp
指向列表的最后一个节点。更改后,代码应该可以正常工作
PS:No!while(temp!=NULL){temp=temp->next;}temp=new_node;这很有效。@EshanPandey我认为你应该遵循我的解决方案,因为我测试了它,效果很好。你的方法没有连接到节点。这并不是说我不喜欢你的解决方案,只是我提出了我自己的解决方案。显然,你的回答很有帮助。你指出了我做错了什么,这对我有帮助。@gsamaras谢谢。顺便说一句,我仍然很满意与印刷品打交道函数。是的,因为您不链接节点。您的每个节点的next
成员使用您的解决方案指向NULL
,而使用我的解决方案,每个节点指向其下一个节点。这就是为什么您无法打印列表@EshanPandey。是的,我认为您是对的。我的方法没有连接节点,因此整个list没有打印。只有第1个元素。但我不完全理解。当(temp!=NULL){temp=temp->next;}temp=new_node;这很有效。@EshanPandey我认为你应该遵循我的解决方案,因为我测试了它,效果很好。你的方法没有连接到节点。这并不是说我不喜欢你的解决方案,只是我提出了我自己的解决方案。显然,你的回答很有帮助。你指出了我做错了什么,这对我有帮助。@gsamaras谢谢。顺便说一句,我仍然很满意与印刷品打交道函数。是的,因为您不链接节点。您的每个节点的next
成员使用您的解决方案指向NULL
,而使用我的解决方案,每个节点指向其下一个节点。这就是为什么您无法打印列表@EshanPandey。是的,我认为您是对的。我的方法没有连接节点,因此整个lis没有打印。只有第一个元素。但我不完全理解。你能帮助(解释)以便于阅读和理解吗::1)通过一个空行分离代码块(for、if、else、while、do…while、switch、case、default)2)遵循公理:每行只有一条语句,最多每个语句一个变量声明。调用scanf()
时,始终检查返回值(而不是参数值)以确保操作成功。调用任何堆分配函数(malloc、calloc、realloc)时,1)始终检查(!=NULL)确保操作成功的返回值。2)返回的类型为void*
,因此可以将其分配给任何指针。强制转换只会使代码变得杂乱无章,使其更难理解、调试等,以便于可读性和理解::1)单独的代码块(for、if、else、while、do…while、switch、case、default)通过一个空行2)遵循公理:每行只有一条语句,每个语句(最多)一个变量声明。调用scanf()
时,始终检查返回值(而不是参数值)为确保操作成功。调用任何堆分配函数(malloc、calloc、realloc)时,1)始终检查(!=NULL)用于确保操作成功的返回值。2)返回的类型为void*
,因此可以将其分配给任何指针。强制转换只会使代码变得混乱,使其更难理解、调试等。