gpt4 book ai didi

C++ 链表值追溯改变

转载 作者:行者123 更新时间:2023-11-28 02:24:52 25 4
gpt4 key购买 nike

编辑:解决方案代码包含在最后。

我正在尝试实现一个链表类,该链表类使用作业中定义的节点类。下面的代码块按预期打印输出:

#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();
}

最佳答案

您的代码中有未定义的行为,因为您保存了指向局部变量的指针,这些变量一旦函数返回就超出范围。

我说的变量是insert函数里面的参数value,一旦insert函数返回,那个指针就不再是有效。

快速解决方案?不要存储指针,存储整数列表。或者可以使列表(和节点)成为模板化类并按值存储。

如果您真的想要一个可以包含任何内容的列表,请考虑使用例如Boost any .

关于C++ 链表值追溯改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31092130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com