编辑:结尾包含解决方案代码
我正在尝试实现一个链表类,该类使用赋值中定义的节点类。以下代码块按预期打印输出:
#include <iostream>
using namespace std;
// Node class as provided
class node {
void *info;
node *next;
public:
node (void *v) {info = v; next = 0; }
void put_next (node *n) {next = n;}
node *get_next ( ) {return next;}
void *get_info ( ) {return info;}
};
// Linked list class
class list {
//Start of the linked list
node *start;
public:
list (int v) {
start = new node (&v);
}
void insert (int value, int place=-1) {
node *temp = new node (&value);
if (place == 0) {
temp->put_next(start);
start = temp;
} else {
node *before = start;
for (int i = 1; before->get_next() != 0; i++) {
if (i == place) {
break;
}
before = before->get_next();
}
temp->put_next(before->get_next());
before->put_next(temp);
}
}
void remove(int place) {
if (place == 0) {
start = start->get_next();
} else {
node *curr = start;
for (int i = 1; curr != 0; i ++) {
if (i == place) {
curr->put_next(curr->get_next()->get_next());
break;
}
curr = curr->get_next();
}
}
}
void display() {
for (node *current = start; current != 0; current = current->get_next()) {
cout << *(static_cast<int*>(current->get_info())) << endl;
}
}
};
int main() {
list *tst = new list(10);
tst->display();
cout << "Prepending 9" << endl;
tst->insert(9,0);
tst->display();
cout << "Inserting 8" << endl;
tst->insert(8,1);
tst->display();
cout << "Prepending 7" << endl;
tst->insert(7,0);
tst->display();
tst->remove(0);
cout << "Removed the first element:" << endl;
tst->display();
cout << endl;
// cout << "Prepending 6" << endl;
// tst->insert(6,0);
// tst->display();
}
创建此输出:
10
Prepending 9
9
10
Inserting 8
9
8
10
Prepending 7
7
9
8
10
Removed the first element:
9
8
10
然而,当我把最后一句话添加到程序流的末尾时,主要是:
tst->insert(6,0);
我的输出更改为:
10
Prepending 9
9
10
Inserting 8
8
8
10
Prepending 7
7
7
7
10
Removed the first element:
134515798
134515798
10
我错过了什么?在稍后的执行中添加一个值如何改变在我到达程序流中的那个点之前发生的输出?
我使用ideone.com作为我的IDE/来运行程序,我以前从未遇到过问题,但这就是问题所在吗?
解决方案
#include <iostream>
using namespace std;
// Provided node class
class node {
void *info;
node *next;
public:
node (void *v) {info = v; next = 0; }
void put_next (node *n) {next = n;}
node *get_next ( ) {return next;}
void *get_info ( ) {return info;}
};
// List class template
template <class T>
class list {
node *start;
public:
list (T v) {
start = new node (&v);
}
// Insert method
void insert (T *value, int place=-1) {
node *temp = new node (value);
// If we're putting it at the beginning, then change the reference to start
if (place == 0) {
temp->put_next(start);
start = temp;
}
// We're inserting it somewhere other than the beginning, handle appropriately
else {
node *before = start;
// Loop to find preceeding node
for (int i = 1; before->get_next() != 0; i++) {
if (i == place) {
break;
}
before = before->get_next();
}
// Insert after preceeding node, and point at subsequent node
temp->put_next(before->get_next());
before->put_next(temp);
}
}
// Remove function
void remove(int place) {
// If we're removing hte beginning, then change start pointer
if (place == 0) {
start = start->get_next();
}
// Find node to remove
else {
node *curr = start;
for (int i = 1; curr != 0; i ++) {
if (i == place) {
// Cut target node out of list
curr->put_next(curr->get_next()->get_next());
break;
}
curr = curr->get_next();
}
}
}
// Print nodes
void display() {
for (node *current = start; current != 0; current = current->get_next()) {
cout << *(static_cast<T*>(current->get_info())) << endl;
}
cout << endl;
}
};
int main() {
int nine = 9;
int eight = 8;
int seven = 7;
int six = 6;
int five = 5;
cout << "Create list holding '10'" << endl;
list<int> *tst = new list<int>(10);
cout << "Prepending 9" << endl;
tst->insert(&nine,0);
cout << "Inserting 8 at 2nd place" << endl;
tst->insert(&eight,1);
cout << "Appending 7" << endl;
tst->insert(&seven);
cout << "Prepending 6" << endl;
tst->insert(&six,0);
cout << "Inserting 5 at 3rd place" << endl;
tst->insert(&five,2);
cout << "Show completed list:" << endl;
tst->display();
cout << "Removing the first element:" << endl;
tst->remove(0);
tst->display();
cout << "Removing the last element:" << endl;
tst->remove(4);
tst->display();
cout << "Removing the second element:" << endl;
tst->remove(1);
tst->display();
}
您的代码中有未定义的行为,因为您保存了指向局部变量的指针,这些变量在函数返回后会超出范围。
我所说的变量是insert
函数中的参数value
,一旦insert
函数返回指针就不再有效。
快速解决方案?不要存储指针,而是存储整数列表。或者,可以将列表(和节点)作为模板类,并按值存储。
如果你真的想要一个可以包含任何内容的列表,可以考虑使用例如Boost any。