## Introduction

This post will give a general introduction of Stack, and some of its basic operations.

### Definition

A stack is a version of a list that is particularly useful in applications involving reversing. In a stack data structure, all insertions and deletions of entries are made at one end, called the top fo the stack, which is also the only position that a user can manipulate the entries.

When it comes to the operations, we talk about push and pop. When we add an item to a stack, we usually say that we **push** it onto the stack. And when we remove an item, we usually say that we **pop** it from the stack. So, noted that the last item pushed onto a stack is always the first data to be removed from the stack. This special property is called last in first out, or LIFO.

### Implementation

Now we use C++ to implement a stack.

First I will show you the definition of the stack.(Stack.hpp)

1 | const int maxstack = 10; |

In this definition, we have a default constructor, two methods to operate entries, and two methods to check whether it is empty(full) or not.

Since I try to implement a stack structure which is similar to the one in STL, template is used in here.If you have any questions about the usage of template, just search it in this station!

Error_code is a enum variables which is used to handle different error, including overflow, underflow and so on. Try to define a Error_code.hpp and it will be convenient for you to use it in the future.

#### Stack()

*precondition*: None.*postcondition*: The Stack exists and is initialized to be empty.

1 | template <class Stack_entry> |

#### pop()

*precondition*: None.*postcondition*: If the Stack is not empty, the top of te Stack is removed, and return Success. If the Stack is empty, an Error_code of UnderFlow is returned and the Stack is left unchanged.

1 | template <class Stack_entry> |

#### push(**const** Stack_entry &item)

*precondition*: None.*postcondition*: If the Stack is not full, item is added to the top of the Stack, and return Success. If the Stack is full, an Error_code of OverFlow is returned and the Stack is left unchanged.

1 | template <class Stack_entry> |

#### top(Stack_entry &item)

*precondition*: None.*postcondition*: The top of a noneempty Stack is copied to item. A code of fail is returned if the Stack is empty.

1 | template <class Stack_entry> |

#### empty()

*precondition*: None.*postcondition*: **True** is returned if the Stack is empty, otherwise **false** is return.

1 | template <class Stack_entry> |

Hereto, I’ve introduce the Stack structure. Hope this post can be helpful to you. Thank you!