我对c编程没有太多经验。我不明白这段代码中的错误是什么。在将此代码放到网上之前,我已经尝试了 5 次。请帮忙。我在这里实现了一个双向链表,其中包含两个将节点添加到列表的函数和一个显示整个列表的函数。编译成功后,如果我尝试添加一个节点,那么程序会意外结束。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node* next;
struct node* prev;
};
void display_list(struct node* ptr);
void add_node(struct node* ptr)
{
if(ptr->next==NULL)
{
ptr=(struct node*)malloc(sizeof(struct node));
(ptr->next)->next=NULL;
(ptr->next)->prev=ptr;
}
else
{ //traverse the list
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
(ptr->next)=(struct node*)malloc(sizeof(struct node));
(ptr->next)->next=NULL;
(ptr->next)->prev=ptr;
}
printf("nEnter data : ");
scanf("%d",((ptr->next)->data));
display_list(ptr);
}
void display_list(struct node* ptr)
{
if(ptr->next==NULL)
{
printf("%dn",ptr->data);
}
else
{
//traverse the list and display each node
while(ptr->next!=NULL)
{
printf("%d--->>>---",ptr->data);
ptr=ptr->next;
}
//display last node
printf("%d",ptr->data);
}
}
int main()
{
int choice;
struct node* start=NULL;
again:
printf("n1) Add node");
printf("n2) Display list");
scanf("%d",&choice);
if(choice==1)
add_node(start);
else if(choice==2)
display_list(start);
else
goto again;
return 0;
}
在你的add_node
函数中,你有一个if
语句来检查ptr->next
是否NULL
,但你从来没有真正检查ptr
本身是否NULL
。
在你的main
函数中,你可以看到第一次调用add_node
,参数确实是NULL
的,因此第一次在该函数中,ptr
是NULL
的,一旦你的代码尝试检查ptr->next
,你就会遇到问题。
既然你在评论中问得很好,我将告诉你我对代码重组的意思。
现在,您对add_node
的实现将struct node *
作为参数。问题是当你有这样的东西时:
struct node* ptr = NULL;
add_node(ptr);
即使您修改add_node
以正确处理NULL
参数,ptr
本身的值也不会在返回add_node
后更改。一种方法是让add_node
取struct node **
。像这样:
void add_node(struct node ** head) {
struct node * ptr = *head;
// use ptr like you had before in the old implementation
*head = ptr; // updating head.
// If ptr has changed, this will update head
// If it hasn't, then no harm
}
这样,如果你有类似的东西
struct node *foo;
add_node(&foo);
然后,add_node
末尾的*head = ptr
行将使用正确的值更新变量,并且这次foo
将在add_node
返回时更新。
如果您在此处替换行:
struct node* start=NULL;
使用创建根节点的代码:
struct node* start = (struct node*)malloc(sizeof(struct node));
if (start)
{
start->prev = NULL;
start->next = NULL;
}
您将传递一个有效的节点,然后可以"添加到"。
终于得到了导致代码的这个问题的答案。我对真正问题所在的区域添加了评论:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
/////////////////////////////////////////////////////////////////////////////////////
struct node
{
int data;
struct node* next;
struct node* prev;
};
//////////////////////////////////////////////////////////////////////////////////////
***//now add node function also returns a pointer of type node***
struct node* insert_node(struct node* start,int item)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=item;
//no availabel heap
if(new_node==NULL)
{
printf("nNo availabel memory!");
exit(0);
}
//if there is no node in the linked list
if(start==NULL)
{
start=new_node;
start->next=NULL;
start->prev=NULL;
}
//if there is one node in the linked list
else
{
new_node->next=start;
new_node->prev=NULL;
start=new_node; //start now points to new node
}
return start;
}
void display_list(struct node* ptr) //display function has also been modified
{
struct node* temp=ptr;
while(temp)
{
printf("%d-->>--",temp->data);
temp=temp->next;
}
}
////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int i;
struct node* start=NULL;
for(i=0;i<10;i++)
{
start=insert_node(start,i+1); //function must return a pointer
//because the function uses a copy of the pointer and does not modify the
//original pointer in the main function
}
display_list(start);
return 0;
}
您正在取消引用无效指针
ptr=(struct node*)malloc(sizeof(struct node));
(ptr->next)->next=NULL;
(ptr->next)->prev=ptr;
您创建包含垃圾的 PTR,然后访问其中的成员ptr->next->next