我已经这样做了:
class Node {
//
int key;
Node* next;
public:
//
Node() : key( -1 ), next( NULL ) {}
Node( int i, Node* j ) : key( i ), next( j ) {}
//
~Node() { delete next; }
//
static void head_insertion( Node* & head, int i );
void print_list();
};
void Node::head_insertion( Node* & head, int i ) {
cout << "insert() 1 head = " << head << endl;
// append the current list to n
Node n(i, head);
cout << "address of n = " << &n << endl;
// make n the new head
head = &n;
//
cout << "insert() 2 head = " << head << endl;
}
头部插入不起作用:
insert() 1 head = 0x7fff56821518
address of n = 0x7fff56821518
insert() 2 head = 0x7fff56821518
Segmentation fault: 11
我有两个问题:
n
in中新创建的 Nodehead_insertion
与 指向的地址具有相同的地址head
。这是怎么回事?- 我写了析构函数,认为会递归调用列表中下一个元素的析构函数。这个对吗?