📄 stack1.cpp
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -