📄 stack.cpp
字号:
// stack.cpp: implementation of the list class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "stack.h"
#include <iostream.h>
//////////////////////////////////////////////////////////////////////
// implementation of the stack class.
//////////////////////////////////////////////////////////////////////
stack::stack(int capacity)
{
stack::capacity = capacity;
size = 0;
top = NULL;
}
stack::~stack()
{
// 释放资源
while(top != NULL){
Node* p = top;
top = top->next;
delete p;
p = NULL;
}
}
void stack::clear()
{
// 释放资源
this->~stack();
top = NULL;
size = 0;
}
bool stack::empty()
{
return top == NULL;
}
void stack::print()
{
Node* pnode = top;
while(pnode != NULL){
cout << pnode->value << " ";
pnode = pnode->next;
}
cout << endl;
}
int stack::pop() throw(stackexpt)
{
if(top == NULL) // 空栈
throw stackexpt("stack is empty.");
int value = top->value; // 获得栈顶元素
Node* p = top; // 栈顶节点指针
top = top->next; // 栈顶指向下一个
delete p; // 释放栈顶节点
p = NULL;
size--;
return value;
}
void stack::push(int value) throw(stackexpt)
{
if(size >= capacity) // 栈已满
throw stackexpt("stack overflow.");
// 新分配节点
Node *pnode = new Node;
pnode->value = value;
// 将节点插入到栈顶
pnode->next = top;
top = pnode;
size++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -