为什么第一个推头仍然是空的? [英] Why after the first push head is still null?

查看:93
本文介绍了为什么第一个推头仍然是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include 

using namespace std;

typedef struct node {
	int data;
	node *next;
};

void push(node*,int);
void print(node*);

int main()
{
	node* head = NULL;
	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) {
		temp->next = x;
		temp->data = y;
		x = temp;
		
	}
	else {
		node *temp1 = new node();
		temp1 = x;
		while (temp1->next != NULL) {
			temp1 = temp1->next;
		}
		temp1->next = temp;
		temp->data = y;
		temp->next = NULL;
		delete temp1;
	}
}

void print(node* x){
	node *temp1 = new node();
	temp1 = x;
	while (temp1->next != NULL) {
		cout << temp1->data << endl;
		temp1 = temp1->next;
	}
}

推荐答案

推送功能的代码有三个问题:

There are three problems in the code of your push function:
void push(node* x, int y){
	node *temp = new node();
	if (x == NULL) {
		temp->next = x;
		temp->data = y;
		x = temp; // this will not do what you want!!! Note that
                      // x is being passed by value!
		
	}
	else {
		node *temp1 = new node(); // why allocate a second node? Just need a pointer
		temp1 = x; // you lose that freshly allocated node; => memory leak
		while (temp1->next != NULL) {
			temp1 = temp1->next;
		}
		temp1->next = temp;
		temp->data = y;
		temp->next = NULL;
		delete temp1; // you are not deleting the temporary node from above
                     // but the second last node of your list!!!

	}
}





要解决第一个问题,需要将指针传递给指针(只要你在C中工作;在C ++中就可以使用参考参数)。这将如下所示:



To solve the first problem you need to pass a pointer to a pointer (as long as you are working in C; in C++ you could use a reference parameter). This will look like this:

void push (node** ppAnchor, int value)
{
    ...





为了消除第二个和第三个问题,我建议您使用现有列表上的指针运行,并使该指针始终指向可以附加新节点的位置;像这样:





To eliminate the second and third problem I would suggest that you run with a pointer over your existing list and make that pointer always point to the place where a new node can be attached; like this:

void push (node** ppAnchor, int value)
{
    node** pLink = ppAnchor;
    while (*pLink != NULL)
        pLink = &(*pLink)->next;





现在pLink指向列表中最后一个节点的 next 成员或列表的锚点如果是空的因此,它指向您要附加新节点的位置。其余的很简单:





Now pLink is pointing the next member of the last node in the list or to the anchor of the list if it is empty. So it points exactly to the location where you want to attach your new node. The rest is simple:

    node* pNode = new node;
    pNode->data = data;
    pNode->next = NULL;
    *pLink = pNode;
}





在您的主要内容中,您可以调用以下函数:



In your main you call the function like this:

int main()
{
	node* head = NULL;
	push (&head, 2);





最后一句话:我会调用该函数追加而不是 push ,因为它将新元素附加到结尾列表。



在列表的开头附加新元素比这更容易,并且不需要while循环。我把它作为练习留给你。



现在,如果你的列表变得很大,那么while循环可能会出现性能问题。因此,将指针存储到列表的最后一个元素也是很聪明的。为此,我将定义一个名为 List 的新结构,其中包含两个指针。也可以尝试将其作为练习。



A last remark: I would call that function append instead of push, as it attaches the new element to the end of the list.

Attaching the new element at the begin of the list is even easier than that and doesn't need the while-loop. I leave that as an exercise to you.

Now, that while-loop may get a performance problem if your lists grow to a large size. Therefore, it would be clever to also store a pointer to the last element of the list. To do that, I would define a new structure called List that contains both pointers. Try that also as an exercise.


这篇关于为什么第一个推头仍然是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆