lstack.h

来自「经典c++程序的实现」· C头文件 代码 · 共 30 行

H
30
字号
class Stack {                 // Linked stack class
private:
  link *top;                  // Pointer to top stack ELEM
public:
  Stack(const int sz =LIST_SIZE) // Constructor: initialize
    { top = NULL; }
  ~Stack() { clear(); }       // Destructor: return ELEMs
  void clear();               // Remove all ELEM's from stack
  void push(const ELEM& item) // Push ELEM onto stack
    { top = new link(item, top); }
  ELEM pop();                 // Pop ELEM from top of stack
  ELEM topValue() const       // Get value of top ELEM
    { assert(!isEmpty());  return top->element; }
  bool isEmpty() const        // Return TRUE if stack is empty
    { return top == NULL; }
};

void Stack::clear() {         // Remove all ELEM's from the stack
  while(top != NULL)          // Return link nodes to the freelist
    { link*temp = top;  top = top->next;  delete temp; }
}

ELEM Stack::pop() {           // Pop ELEM from top of stack
  assert(!isEmpty());
  ELEM temp = top->element;
  link* ltemp = top->next;
  delete top; top = ltemp;
   return temp;
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?