编辑:最后包含解决方案代码。
我正在尝试实现一个链接列表类,它使用赋值中定义的节点类。下面的代码块按预期打印输出:
#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
但是,当我在main:
中将最后一个语句添加到程序流程的末尾时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();
}
答案 0 :(得分:2)
你的代码中有未定义的行为,因为你保存了指向局部变量的指针,一旦函数返回就会超出范围的变量。
我所讨论的变量是value
函数中的参数insert
,一旦insert
函数返回指针不再有效。
快速解决方案?不要存储指针,存储整数列表。或者可以使列表(和节点)成为模板化类,并按值存储 。
如果您真的想要一个可以包含任何内容的列表,请考虑使用例如Boost any