使用链表进行堆栈

发布于 2024-12-29 14:37:38 字数 1213 浏览 2 评论 0原文

我的代码中出现“分段错误”。怎么了?提前致谢。 ps这是一个使用链表的堆栈。

#include <iostream>
//stack using linked list
class LinkedList {
 public:
  LinkedList() : head(0), tail(0) {}
  ~LinkedList() {
    while (!empty()) pop();
    delete head;
  }
  void pop() {
    node* temp;
    temp = head;
    for ( ; temp->next_ != tail; temp = temp->next_) {
      tail = temp;   
    }
    delete temp;
    tail->next_ = 0;
  } //removes, but does not return, the top element
  int top() {
    return tail->value_;
  } //returns, but does not remove, the top element
  bool empty() {
    return head == 0;
  }
  void push(const int& value) {
    node* element = new node(value);
    if (empty()) {
      head = tail = element;
    } else {
      tail->next_ = element;
      tail = element;
    }
  } //place a new top element
 private:
  class node {
   public:
    node(const int& input) : value_(input), next_(0) {};
    int value_; //store value
    node* next_; //link to the next element
  };
  node* head;
  node* tail;
};
int main() {
  LinkedList list;
  list.push(1);
  list.push(2);
  list.push(3);
  list.pop();
  std::cout << list.top() << std::endl;
  return 0;
}

I got "segmentation error" in my code. what is wrong? thanks in advance. p.s it's a stack using linked list.

#include <iostream>
//stack using linked list
class LinkedList {
 public:
  LinkedList() : head(0), tail(0) {}
  ~LinkedList() {
    while (!empty()) pop();
    delete head;
  }
  void pop() {
    node* temp;
    temp = head;
    for ( ; temp->next_ != tail; temp = temp->next_) {
      tail = temp;   
    }
    delete temp;
    tail->next_ = 0;
  } //removes, but does not return, the top element
  int top() {
    return tail->value_;
  } //returns, but does not remove, the top element
  bool empty() {
    return head == 0;
  }
  void push(const int& value) {
    node* element = new node(value);
    if (empty()) {
      head = tail = element;
    } else {
      tail->next_ = element;
      tail = element;
    }
  } //place a new top element
 private:
  class node {
   public:
    node(const int& input) : value_(input), next_(0) {};
    int value_; //store value
    node* next_; //link to the next element
  };
  node* head;
  node* tail;
};
int main() {
  LinkedList list;
  list.push(1);
  list.push(2);
  list.push(3);
  list.pop();
  std::cout << list.top() << std::endl;
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

-柠檬树下少年和吉他 2025-01-05 14:37:38

这部分看起来不正确,

for ( ; temp->next_ != tail; temp = temp->next_) {
    tail = temp;
}

因为一旦将 tail 设置为与 temp 相同,temp->next != tail 将始终说实话。

This part doesn't look right

for ( ; temp->next_ != tail; temp = temp->next_) {
    tail = temp;
}

because once you set tail to be the same as temp, temp->next != tail will always be true.

岁吢 2025-01-05 14:37:38
for ( ; temp->next_ != tail; temp = temp->next_) {
      tail = temp;   
}

本来的条件应该是

temp->next_ != 0
for ( ; temp->next_ != tail; temp = temp->next_) {
      tail = temp;   
}

The condition should have been

temp->next_ != 0
故事灯 2025-01-05 14:37:38

这个方法

  void pop() {
    node* temp;
    temp = head;
    for ( ; temp->next_ != tail; temp = temp->next_) {
      tail = temp;   
    }
    delete temp;
    tail->next_ = 0;
  } //removes, but does not return, the top element

必须是这样的:

  void pop() {
    if( head == tail )
    {
        delete head;
        head = 0;
    } 
    else
    {
        node* temp;
        temp = head;
        for ( ; temp->next_ != tail; temp = temp->next_) {
        }
        delete tail;
        temp->next_ = 0;
        tail = temp;
    }
  } //removes, but does not return, the top element

This method

  void pop() {
    node* temp;
    temp = head;
    for ( ; temp->next_ != tail; temp = temp->next_) {
      tail = temp;   
    }
    delete temp;
    tail->next_ = 0;
  } //removes, but does not return, the top element

must be like this:

  void pop() {
    if( head == tail )
    {
        delete head;
        head = 0;
    } 
    else
    {
        node* temp;
        temp = head;
        for ( ; temp->next_ != tail; temp = temp->next_) {
        }
        delete tail;
        temp->next_ = 0;
        tail = temp;
    }
  } //removes, but does not return, the top element
甜宝宝 2025-01-05 14:37:38

我认为的问题是:

for ( ; temp->next_ != tail; temp = temp->next_) {
  tail = temp;   
}
delete temp;
tail->next_ = 0;

tail = temp 应该在找到导致 tail 的 temp 之后(即在 for 循环之外)。
另外,temp = 不是尾部,而是尾部之前的那个。所以可能你需要:

for ( ; temp->next_ != tail; temp = temp->next_) {}
delete tail;
tail = temp;
tail->next_ = 0;

The problem I think is:

for ( ; temp->next_ != tail; temp = temp->next_) {
  tail = temp;   
}
delete temp;
tail->next_ = 0;

tail = temp should be after you find the temp that leads to tail (i.e. outside of the for loop).
Also, temp = not the tail but the one before the tail. So probably you need:

for ( ; temp->next_ != tail; temp = temp->next_) {}
delete tail;
tail = temp;
tail->next_ = 0;
ヤ经典坏疍 2025-01-05 14:37:38

析构函数对我来说看起来有问题:你一直“弹出”直到empty()返回true,当head是空指针时就会发生这种情况。但是在 while 循环结束后你不能在头上调用删除...

我不知道这是否是问题所在,但我会检查一下。

另一个谦虚的提示:您没有告诉我们 seg 错误发生在哪里...如果您使用 gdb 运行代码(或者如果您只是在代码中放入大量“cout”),您可以检测到导致您出现错误的行问题。

The destructor looks buggy to me: you keep "popping" until empty() returns true, which happens when head is the null pointer. But then you can't call delete on head after the while loop is over...

I don't know if this is the issue, but I would check it.

Another humble tip: you didn't tell us where the seg fault happens... If you run your code with gdb (or if you just put a lot of "cout" in your code), you can detect the line that causes you problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文