[英]Singly Linked List Doesn't work when i am using Node(key,data) rather than Node.key and Node.data in C++
[英]Why there is a dummy node when I am printing a singly linked list?
我的代码为什么打印一个额外的节点(垃圾值)? 我的代码有问题吗? 让我来解决这个问题。
void push(node **head_ref,int value) //function to insert a new node on front of the list
{
node *new_node=(node*)malloc(sizeof(node));
new_node->data=value;
new_node->next=*head_ref;
*head_ref=new_node;
}
void printll(node *head) //function to print the list
{
node *temp = head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
实际输出:
45 88 24 34 77 0
预期产量:
45 88 24 34 77
完整代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cassert>
using namespace std;
struct node
{
int data;
node *next;
};
void push(node **head_ref,int value)
{
node *new_node=(node*)malloc(sizeof(node));
new_node->data=value;
new_node->next=*head_ref;
*head_ref=new_node;
}
void printll(node *head)
{
node *temp = head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
int main()
{
node *head= (node*)malloc(sizeof(node));
push(&head,77);
push(&head,34);
push(&head,24);
push(&head,88);
push(&head,45);
printll(head);
printf("\n");
return 0;
}
当您使用malloc
分配内存时,不会以任何方式初始化内存,它的内容是不确定的 。 这意味着,当您分配第一个节点(不需要的虚拟额外节点)时,它的next
指针不为null,并且取消引用该不确定的指针会导致未定义的行为 。
最简单的解决方案? 考虑到您的代码比C比C ++更接近C,因此根本不分配内存,而只是创建一个指针并将其初始化为NULL
:
node *head = NULL;
在C ++中执行此操作的一种正确方法是根本不使用malloc
,而使用C ++运算符new
并向初始化它的node
结构添加构造函数:
struct node
{
node() : data(0), next(nullptr) {}
node(int d, node* n) : data(d), next(n) {}
int data;
node* next;
};
void push(node** head_ref, int value)
{
*head_ref = new node(value, *head_ref);
}
...
int main()
{
node* head = nullptr;
...
}
现在,您可以创建一个新节点,它的初始value
0
, next
指针为空指针。 如上所示,您还可以创建和初始化具有特定value
和next
的新节点。
[如果您的编译器不支持C ++ 11 nullptr
值,则将nullptr
替换为0
]
代替这个定义
node *head= (node*)malloc(sizeof(node));
你应该简单地写
node *head = NULL;
要么
node *head = nullptr; // C++
否则,您的程序将具有未定义的行为,因为未初始化头分配的节点。
另外,如果它是C ++程序,则应使用运算符new
而不是C函数malloc
。 例如功能push
看起来像
void push( node * &head_ref, int value )
{
head_ref = new node { value, head_ref };
}
叫像
push( head, 77 );
考虑到您还必须编写一个函数,该函数将为列表释放所有分配的内存。
您的设计中有一个虚拟节点(头部本身)。 因此,打印功能需要跳过该虚拟节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.