C++堆栈实现弹出函数不起作用

C++ stack implementation pop function not working

本文关键字:函数 不起作用 堆栈 实现 C++      更新时间:2023-10-16

我正在实现一个带有链表的堆栈以供审查。 我的弹出功能无法正常工作。 我创建了一个测试,其中的节点主要做与我的 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以释放内存。