[英]C++ stack implementation pop function not working
我正在实现一个带有链接列表的堆栈以供审查。 我的弹出功能无法正常工作。 我使用 main 中的节点创建了一个测试,它执行的操作与我的 linksList 函数所做的完全相同,但是我每次都会遇到分段错误。 这是代码。
#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;
}
问题是你如何创建 Node.js 文件。 在您的情况下,您创建了一个局部变量,该变量仅存在于函数 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
创建new Node
并存储指向它的指针。 以这种方式创建的对象会一直存在,直到某些东西明确地销毁它们为止,这意味着它们将在push
调用完成后push
。 然后,您需要调整pop
以释放内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.