使用链表在 c++ 中堆栈嵌入
Stack implemantation in c++ using linked list
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data; //data
node *next; //link
};
class stack // stack using linked list
{
public:
node *top; // top element of stack
public:
stack()
{
top= NULL;
}
void push(int value)
{
node *temp = new node; // create a new node
temp-> data = value;
temp-> next = NULL;
if(top==NULL) // stack is empty
{
top=temp;
temp=NULL;
}
else
{
temp-> next = top;
top=temp;
temp=NULL;
}
}
//template <class X>
void pop()
{
if(top==NULL)
{
cout<<"nStackOverflow "<<endl;
cout<<"Program Terminated "<<endl;
exit (0);
}
else
{
top=top->next;
}
}
void display()
{
node *temp=new node;
temp=top;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp-> next;
}
while(top==NULL)
{
cout<<"nStack is Empty "<<endl;
exit (0);
}
}
};
int main()
{
stack a;
a.push(5);
a.display();
a.push(10);
a.display();
a.pop();
a.pop();
a.push(20);
a.display();
a.pop();
a.display();
return 0;
}
此代码的输出为 5 10 5 20 堆栈为空。
哪个是错误的输出,正确的输出是 5 10 20 堆栈为空。.
有人告诉我为什么会发生此错误。
代码的引用:[在 c++ 中使用模板和链表实现堆栈
不,输出是正确的。
a.push(5);
a.display();
这将显示第一个5
。
a.push(10);
a.display();
5
仍在堆栈上,因此现在显示10
,然后显示5
。
a.pop();
a.pop();
a.push(20);
a.display();
现在所有内容都被删除,20
被添加并显示,所以这应该只显示20
.
然后空堆栈打印
a.pop();
a.display();
所以放在一起,它应该显示5 10 5 20 Stack is Empty
.
a.push(5); // Stack: 5
a.display(); // Output: new: 5
a.push(10); // Stack: 10 5
a.display(); // Output: old: 5 new: 10 5
a.pop(); // Stack: 5
a.pop(); // Stack: empty
a.push(20); // Stack: 20
a.display(); // Output: old: 5 10 5 new: 20
a.pop(); // Stack: empty
a.display(); // Output: old: 5 10 5 20 new:
stack() { top= NULL; // use initializ list instead of assignment // in constructors body }
-->
stack() : top{ nullptr } {}
void pop() { if (top == NULL) { cout << "nStackOverflow " << endl; cout << "Program Terminated " << endl; exit(0); // don't use exit() in C++ if there are other ways! } else { top = top->next; // the memory top pointed to // before the assignment leaks! } }
-->
void pop()
{
if (!top) {
cout << "Pop on empty stack!n";
return;
}
node *old_top = top;
top = top->next;
delete old_top;
}
void display() { node *temp = new node; // no need to allocate a node in a // function that should only make output temp = top; // the memory temp points to before the // assignment leaks while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } while (top == NULL) // why a loop? { cout << "nStack is Empty " << endl; exit (0); // again ... } }
-->
void display() const
{
if (!top) {
std::cout << "Stack is empty!n";
return;
}
for(node *current = top; current; current = current->next)
cout << current->data << ' ';
}
相关文章:
- 算法问题:查找从堆栈中弹出的所有序列
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么调用堆栈数组会导致内存泄漏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的 int main() 中出现堆栈溢出错误
- 堆栈和队列是否像C++中的数组一样传递?
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 为什么 STL 容器适配器堆栈中的 top 返回常量引用?
- 从堆栈分配的原始指针构造智能指针
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- 堆栈中大小变量输入错误 (C++)
- 堆栈问题(平衡表达式问题集)
- C++ 在堆栈中包含多态属性的类对象存储
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 在 C++ 中使用链表进行堆栈
- 变量周围的堆栈'...'已损坏
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- C++线程的可用堆栈大小