使用链表进行堆栈
我的代码中出现“分段错误”。怎么了?提前致谢。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这部分看起来不正确,
因为一旦将
tail
设置为与temp
相同,temp->next != tail
将始终说实话。This part doesn't look right
because once you set
tail
to be the same astemp
,temp->next != tail
will always be true.本来的条件应该是
The condition should have been
这个方法
必须是这样的:
This method
must be like this:
我认为的问题是:
tail = temp 应该在找到导致 tail 的 temp 之后(即在 for 循环之外)。
另外,temp = 不是尾部,而是尾部之前的那个。所以可能你需要:
The problem I think is:
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:
析构函数对我来说看起来有问题:你一直“弹出”直到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.