为什么第一次push后head还是null?
why after the first push head is still null?
我正在尝试创建单链表。第一次推送后,head
仍然是null。为什么第一次推送后头部没有更新?
using namespace std;
typedef struct node {
int data; // will store information
node *next; // the reference to the next node
};
void push(node*,int);
void print(node*);
int main()
{
node* head = NULL; //empty linked list
push(head, 2);
if (head == NULL) {
cout << "vrvrvr";
}
push(head, 3);
push(head, 5);
push(head, 2);
//print(head);
getchar();
return 0;
}
void push(node* x, int y){
node *temp = new node();
if (x == NULL) { // check linked list is empty
temp->next = x;
temp->data = y;
x = temp;
}
else {
node *temp1 = new node();
temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
delete temp1; // 'temp' node will be the last node
}
}
void print(node* x){
node *temp1 = new node();
temp1 = x;
while (temp1->next != NULL) {
cout << temp1->data << endl;
temp1 = temp1->next;
}
}
push
的主要问题是函数中对 x
所做的更改是函数局部的。它不会更改 main
中 head
的值。
您可以通过将参数类型更改为 node*&
.
来解决这个问题
void push(node*& x, int y) {
...
}
我看到的其他问题都在块中:
else {
node *temp1 = new node();
temp1 = x;
// Problem 1:
// After this, the memory returned by the previous line is lost.
// It is a memory leak.
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
delete temp1; // 'temp' node will be the last node
// Problem 2:
// You are deleting a node from the linked list.
// The linked list now has a dangling pointer.
}
您可以使用以下方法更正这些问题:
node *temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
}
改进建议
从 node
的定义中删除 typedef
。它在您发布的代码中悬空 typedef
。此外,您可以在 C++ 中使用不带 typedef
的 node
。
struct node {
int data;
node *next;
};
向node
添加构造函数。
struct node {
node(int d) : data(d), next(nullptr) {}
int data;
node *next;
};
这将简化您在 push
中的代码。
void push(node*& x, int y){
node *temp = new node(y);
if (x == NULL) { // check linked list is empty
x = temp;
}
else {
node *temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
}
}
我正在尝试创建单链表。第一次推送后,head
仍然是null。为什么第一次推送后头部没有更新?
using namespace std;
typedef struct node {
int data; // will store information
node *next; // the reference to the next node
};
void push(node*,int);
void print(node*);
int main()
{
node* head = NULL; //empty linked list
push(head, 2);
if (head == NULL) {
cout << "vrvrvr";
}
push(head, 3);
push(head, 5);
push(head, 2);
//print(head);
getchar();
return 0;
}
void push(node* x, int y){
node *temp = new node();
if (x == NULL) { // check linked list is empty
temp->next = x;
temp->data = y;
x = temp;
}
else {
node *temp1 = new node();
temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
delete temp1; // 'temp' node will be the last node
}
}
void print(node* x){
node *temp1 = new node();
temp1 = x;
while (temp1->next != NULL) {
cout << temp1->data << endl;
temp1 = temp1->next;
}
}
push
的主要问题是函数中对 x
所做的更改是函数局部的。它不会更改 main
中 head
的值。
您可以通过将参数类型更改为 node*&
.
void push(node*& x, int y) {
...
}
我看到的其他问题都在块中:
else {
node *temp1 = new node();
temp1 = x;
// Problem 1:
// After this, the memory returned by the previous line is lost.
// It is a memory leak.
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
delete temp1; // 'temp' node will be the last node
// Problem 2:
// You are deleting a node from the linked list.
// The linked list now has a dangling pointer.
}
您可以使用以下方法更正这些问题:
node *temp1 = x;
while (temp1->next != NULL) { // go to the last node
temp1 = temp1->next;
}
temp1->next = temp;
temp->data = y;
temp->next = NULL;
}
改进建议
从
node
的定义中删除typedef
。它在您发布的代码中悬空typedef
。此外,您可以在 C++ 中使用不带typedef
的node
。struct node { int data; node *next; };
向
node
添加构造函数。struct node { node(int d) : data(d), next(nullptr) {} int data; node *next; };
这将简化您在
push
中的代码。void push(node*& x, int y){ node *temp = new node(y); if (x == NULL) { // check linked list is empty x = temp; } else { node *temp1 = x; while (temp1->next != NULL) { // go to the last node temp1 = temp1->next; } temp1->next = temp; } }