Stacks
- There are several basic data structures that we need to discuss.
- The Stack is a very common tool in computer science.
- It is a homogeneous collection of data.
- It maintains the data by order inserted.
- It is characterized by access
- Last in first out (LIFO)
- Or First in last out.
- It has several basic operations
- Push add an item to the stack
- Pop remove the top item from the stack.
- This should produce an error if the stack is empty.
- In some implementations this also returns the top item.
- Top return the top item on the stack without removing it.
- This should produce an error if the stack is empty.
- Size returns the number of items on the stack.
- IsEmpty returns true if the stack is empty.
- IsFull returns true if the stack is full
- Two common implementations for stacks
- Array Based
- In an array based implementation, the stack starts at position 0
- There is an index, usually top
- This points to the next available position in the stack.
- It is normally initialized to be 0
- As items are added it increases.
- List Based
- In a list based implementation, the top pointer is to the "head" of the list.
- Items are added and removed at the top.
- A fairly simple stack problem is here.