简单C ++链表 [英] Simple C++ Linked List

查看:173
本文介绍了简单C ++链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多以前的Java链接列表的经验,但我似乎很困惑自己这个简单的尝试在C + +。我在运行时得到了一个分段错误,从我的理解与分配一个空指针有关,但我是一个失去一个解决方案。

I have plenty of previous experience with linked lists in Java, but I seem to have confused myself with this simple attempt in C++. I am getting a segmentation fault at runtime, which from what I understand has to do with assigning a null pointer, but I am at a loss for a solution.

编辑:谢谢大家的非常有帮助的回应。代码现在正在工作,但试图在linkedList :: addNode结尾处使用

Thank you all for the very helpful responses. The code is now working, but trying to use

delete p;

分段错误。

at the end of linkedList::addNode results in a segmentation fault at runtime. Just curious if anyone knew why that is?

这是我更新的代码:

#include <iostream>
using namespace std;

class Node{
    public:
    	int data;
	Node * next;
	Node(int x){
		data = x;
		next = NULL;
		}
	Node(int x, Node * y){
		data = x; 
		next = y;
		}
    };


class linkedList{
Node *head;
public:
	linkedList(){
		head = NULL;
		}
	void addNode(int value){
		Node *p;
		if(head == NULL)
			head = new Node (value, NULL);
		else{
			p=head;
			while(p->next !=NULL)
				p=p->next;
			p->next = new Node (value, NULL);
			}
		}
	void print(){
		Node * p;
		p = head;
		while(p != NULL){
			cout << p->data << "\n";
			p = p->next;
			}
		}
};


int main(void){
linkedList test;
test.addNode(4);
test.addNode(76);
test.addNode(12);
test.print();
return(0);
}


推荐答案

$ c> linkedList :: addNode 方法,你有构造 if(head = NULL),这将结束 head ;你想要 == 运算符。

First, in linkedList::addNode method, you have the construction if (head = NULL), which will wind up assigning to head; you want the == operator.

第二,关于行:

head = &(Node (value, NULL));

由于有些不直观的原因,这将不起作用。您将获得对节点的引用,但该方法将在方法结束时立即超出范围,并且尝试引用该节点将导致分段错误。您需要使用 new 运算符(与其他类似行相同):

For somewhat unintuitive reasons, this won't work. You'll get a reference to a Node, but that node will go out of scope as soon as the method ends, and attempts to reference it will lead to a segmentation fault. You need to use the new operator (same with the other similar line):

head = new Node(value, NULL);

如果添加一个删除节点的方法,请确保 delete 节点,它将不会像在Java中一样自动进行垃圾回收。

If you add a method for removing a node, make sure to delete the node then—it won't get automatically garbage-collected like it will in Java.



边栏:想想会发生什么:当你做 Node(value,NULL)时,它声明如下:

Sidebar: Think of what happens like this: when you do Node(value, NULL), you're using a temporary variable that's declared like this:

Node hiddenTempNode(value, NULL);

这不会为除了堆栈之外的任何对象分配空间 - 这与分配空间非常相似在堆栈上将 int Node * 作为单独的变量。因此,一旦你离开该方法,对象就会消失,并且指向它的指针在使用时会做奇怪的事情。

This doesn't allocate space for an object anywhere except on the stack—it's very similar to allocating space for an int and a Node * on the stack as separate variables. As a result, as soon as you leave the method, the object disappears and the pointer to it will do weird things when used.


第三,要注意:你可能想在单参数构造函数中设置 next = NULL ,以确保它总是有一个值。类似的默认构​​造函数。

Third, beware: you may want to set next = NULL in your single-parameter constructor, to ensure that it always has a value. Similarly for your default constructor.

第四: linkedList :: print 方法循环,​​直到 p-> next NULL 并打印 p-& >;如果你想获得第一个和最后一个,那么 p-> next 的出现应该可以改为 p 项目。

Fourth: your linkedList::print method is looping until p->next is NULL and printing the value of p->next; those occurrences of p->next should probably be changed to just p if you want to get the first and last items.

这篇关于简单C ++链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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