C++堆栈实现弹出函数不起作用
C++ stack implementation pop function not working
我正在实现一个带有链表的堆栈以供审查。 我的弹出功能无法正常工作。 我创建了一个测试,其中的节点主要做与我的 linkedList 函数完全相同的事情,但我每次都遇到分段错误。 这是代码。
#include <iostream>
struct Node{
int data;
Node* next;
};
class Stack{
private:
Node head;
int size;
public:
Stack();
~Stack();
int getSize();
void push(int val);
int pop();
void printStack();
};
Stack::Stack(){
head.data = 0;
head.next = NULL;
}
Stack::~Stack(){
}
int Stack::getSize(){
return size;
}
void Stack::push(int val){
Node newNode;
newNode.data = val;
if(size == 0) newNode.next = NULL;
else newNode.next = head.next;
head.next = &newNode;
size++;
}
int Stack::pop(){
int returnVal = head.next->data;
head.next = head.next->next;
return returnVal;
}
}
int main(){
Stack s;
s.push(8);
s.push(30);
s.push(40);
int value = s.pop();
int value2 = s.pop(); //segmentation fault
std::cout<<value<<"n"<<value2<<"n";
/* This works correctly
Node head;
head.data = 0;
head.next = NULL;
Node n1;
n1.data = 5;
n1.next = NULL;
head.next = &n1;
Node n2;
n2.data = 8;
n2.next = head.next;
head.next = &n2;
Node n3;
n3.data = 30;
n3.next = head.next;
head.next = &n3;
int value = head.next->data;
std::cout << value << "n";
head.next = head.next->next;
value = head.next->data;
std::cout << value << "n";
*/
return 1;
}
问题在于您如何创建节点。在您的情况下,您创建一个局部变量,该变量仅存在于函数 push() 的范围内。你可以使用这样的东西。
void Stack::push(int val){
Node* newNode = new Node;
newNode->data = val;
/* ... */
}
编辑:添加了堆栈的一个版本(绝不是完整的)
#include <iostream>
struct Node{
int data;
Node* next;
};
class Stack {
private:
Node* head;
int size;
public:
Stack();
~Stack();
int getSize();
void push(int val);
int pop();
void printStack();
};
Stack::Stack() : head(0), size(0)
{
}
Stack::~Stack(){
}
int Stack::getSize(){
return size;
}
void Stack::push(int val){
Node* newNode = new Node;
newNode->data = val;
newNode->next = head;
head = newNode;
size++;
}
int Stack::pop(){
if(head != 0)
{
int val = head->data;
Node* tmp = head;
head = head->next;
tmp->next = NULL;
delete tmp;
size--;
return val;
}
else
{
return -1; // what happens if stack is empty ?
}
}
void Stack::push(int val){
Node newNode;
newNode
被声明为 push()
函数自动范围内的本地对象。
这意味着当push()
返回时,此对象将被自动销毁。这就是"自动范围"的含义。
push()
中的代码尝试将此对象插入堆栈,并假定此对象在push()
返回后将存在。当然,这不是真的,这最终会破坏内存,导致未定义的行为。
从根本上说,这不是对象生存期和范围在C++的工作方式。
我认为您的push()
和pop()
方法都有问题。 您可以尝试使用以下版本:
// create new node, point it to current head, and then assign it as new head
void Stack::push(int val){
Node* newNode = new Node;
newNode->data = val;
newNode->next = head; // OK even if head == NULL
head = newNode;
size++;
}
// retrieve value from head (if it exists), pop the head and return the value
int Stack::pop(){
if (head == NULL) return -1; // -1 indicates empty stack
int returnVal = head->data; // get popped value
Node* temp = &head;
head = head->next; // pop the top of the stack
delete temp;
size--;
return returnVal;
}
您的 push
代码中存在问题,实际上:
void Stack::push(int val){
Node newNode;
newNode.data = val;
if(size == 0) newNode.next = NULL;
else newNode.next = head.next;
head.next = &newNode;
size++;
}
当你写的时候
Node newNode;
您正在声明具有自动存储持续时间的Node
对象(您有时会听到这称为"在堆栈上")。此Node
仅在push
函数运行时存在,并且一旦函数返回,节点就不复存在。稍后尝试使用该节点会导致未定义的行为,这实际上可能是从彻底崩溃到回读垃圾数据的任何行为。
若要解决此问题,需要使用动态分配。使用 new Node
创建节点并存储指向该节点的指针。以这种方式创建的对象一直存在,直到某些东西显式销毁它们,这意味着它们将在push
调用完成后存活。然后,您需要调整pop
以释放内存。
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么我的数组双精度函数不起作用?
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ Arduino - 随机函数不起作用
- 为什么我的数组或函数不起作用?
- 从类中的另一个文件请求函数不起作用
- 双链表堆栈删除函数不起作用
- 将 cmake 代码段转换为函数 - 不起作用
- 为什么我的从base64解码的函数不起作用?
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 以下打印树函数不起作用为什么?
- 为什么内存集函数不起作用?
- 调用填充我的主体数组的函数不起作用
- 带有自定义数字的阶乘函数不起作用
- 如何QDialog raise()函数不起作用
- Lambda 表达式闭包函数不起作用
- C++:在没有 lvalue 的情况下调用时复制构造函数不起作用