ADT Stack
Idea Like You Are 10
A stack is like a pile of books.
You can:
- put a book on top
- remove a book from the top
But you cannot remove the book from the middle first.
This rule is called:
LIFO = Last In, First Out
Stack ADT Operations
push(x): add item on toppop(): remove top itempeek()ortop(): see top itemisEmpty(): check if no items existisFull(): check if stack is full in array implementationdisplay(): show all items
Array Representation
Top
|
v
[10]
[20]
[30]
If top = -1, the stack is empty.
Algorithms
Push
push(stack, x):
if top == MAX - 1:
overflow
top = top + 1
stack[top] = x
Time: O(1)
Pop
pop(stack):
if top == -1:
underflow
x = stack[top]
top = top - 1
return x
Time: O(1)
Peek
peek(stack):
if top == -1:
underflow
return stack[top]
Time: O(1)
Is Empty
isEmpty():
return top == -1
Time: O(1)
Is Full
isFull():
return top == MAX - 1
Time: O(1)
Display
display(stack):
for i from top down to 0:
print stack[i]
Time: O(n)
Complexity Analysis
| Operation | Time |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
| Is Empty | O(1) |
| Is Full | O(1) |
| Display | O(n) |
Why These Complexities?
push,pop, andpeekonly touch the topdisplaymust visit every element
Space Complexity
- Array stack stores up to
nitems - total space is
O(n)
Advantages
- very simple
- fast insert and delete at one end
- useful in many algorithms
Disadvantages
- only one end is usable
- array stack has fixed size unless resized