lstack.h
来自「数据结构与算法分析」· C头文件 代码 · 共 47 行
H
47 行
// This is the file to include in your code if you want access to the
// complete LStack template class
// Include the link class
#include "link.h"
// First, get the declaration for the base stack class
#include "stack.h"
// Link list-based stack implementation
template <class Elem> class LStack: public Stack<Elem> {
private:
Link<Elem>* top; // Pointer to first element
int size; // Count number of elements
public:
LStack(int sz =DefaultListSize) { top = NULL; size = 0; }
~LStack() { clear(); } // Destructor
void clear() {
while (top != NULL) { // Delete link nodes
Link<Elem>* temp = top;
top = top->next;
size = 0;
delete temp;
}
}
bool push(const Elem& item) {
top = new Link<Elem>(item, top);
size++;
return true;
}
bool pop(Elem& it) {
if (size == 0) return false;
it = top->element;
Link<Elem>* ltemp = top->next;
delete top;
top = ltemp;
size--;
return true;
}
bool topValue(Elem& it) const {
if (size == 0) return false;
it = top->element;
return true;
}
int length() const { return size; }
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?