stack1.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 58 行

CPP
58
字号
 
bool Stack::empty() const
/*   
Post: Return true if the Stack is empty,
otherwise return false.
 */
{
   return top_node == NULL;
}
 
Stack::Stack()
/*   
Post: The Stack is initialized to be empty.
 */
{
   top_node = NULL;
}
 
Error_code Stack::push(const Stack_entry &item)
/*   
Post: Stack_entry item is added to the top of
the Stack; returns success or returns a code
of overflow if dynamic memory is exhausted.
 */

{
   Node *new_top = new Node(item, top_node);
   if (new_top == NULL) return overflow;
   top_node = new_top;
   return success;
}
 
Error_code Stack::top(Stack_entry &item) const
/*   
Post: The top of the Stack is reported in item. If the Stack 
is empty the method returns underflow; otherwise it returns success.
 */

{
   if (top_node == NULL) return underflow;
   item = top_node->entry;
   return success;
}
 
Error_code Stack::pop()
/*   
Post: The top of the Stack is removed.  If the Stack 
is empty the method returns underflow; otherwise it returns success.
 */

{
   Node *old_top = top_node;
   if (top_node == NULL) return underflow;
   top_node = old_top->next;
   delete old_top;
   return success;
}

⌨️ 快捷键说明

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