Stacks
Stacks
Stacks
• A Stack is a sequential organization of items in which the last
element inserted is the first element removed. They are often referred
to as LIFO, which stands for “last in first out.”
• Examples: letter basket, stack of trays, stack of plates.
• The only element of a stack that may be accessed is the one that was
most recently inserted.
• There are only two basic operations on stacks, the push (insert), and
the pop (read and delete).
push pop
Stack
Stacks 2
Assume we have a stack of size 3 which holds integers between -100 and
100. Here is a series of operations, and the results.
Operation p E0 E1 E2 E3 E4 R
create -1 ? ? ? ? ?
push(55) 0 55 ? ? ? ? 1
push(-7) 1 55 -7 ? ? ? 1
push(16) 2 55 -7 16 ? ? 1
pop 1 55 -7 16 ? ? 16
push(-8) 2 55 -7 -8 ? ? 1
pop 1 55 -7 -8 ? ? -8
pop 0 55 -7 -8 ? ? -7
• Notice that some values are still in the array, but are no longer
considered to be in the stack. In general, elements E[i] are “garbage”
if i > p. Why don’t we erase the element (i.e. set it to some default
value.)?
Stacks 6
Examples
1. Input: { ( ) }
• Read {, so push {
• Read (, so push (. Stack has { (
• Read ), so pop. popped item is ( which matches ). Stack has now {.
• Read }, so pop; popped item is { which matches }.
• End of file; stack is empty, so the string is valid.
2. Input: { ( ) ( { ) } } (This will fail.)
3. Input: { ( { } ){ } ( ) } (This will succeed.)
4. Input: { ( ) } ) (This will fail.)
Stacks 8
Stack Implementation:
C++ Linked Lists
• We can use linked lists to implement stacks.
• The head of the list represents the top of the stack.
• Example After push(D), push(C), push(B), push(A), we have:
Head
A B C D
• ItemType pop () {
ItemType x = head->key;
head=head->next;
return x; }
• void push(ItemType x) {
node *x;
x=new node;
x->key = X;
insert(x); }
Stack Implementation:
JAVA Linked Lists
We assume head points to the first element of the list, and is the top of the
stack.
• public ItemType pop () {
ItemType x = head.key;
head=head.next;
return x;
}
• public void push(ItemType x) {
node A=new node();
A.key = x;
A.next=head;
head=A;
}
Stacks 13
Stack Implementation:
Array or Linked List?
Linked Lists
• Use 1 pointer extra memory per item. If an item is an integer, that
means twice as much space is used. If an item is a structure/class
consisting of many objects, it is only a small price to pay.
• Are unlimited in size.
Arrays
• Allocate a constant amount of space, some of which may never be
used. The amount of wasted memory is the number of unused
elements times the size of the item, which could be large in some
cases.
• The maximum size is determined when the stack is created.
Which is better? Why?
Stacks 14
Stack Applications
• Recursion removal can be done with stacks.
• Reversing things is easily done with stacks.
• Procedure call and procedure return is similar to matching symbols:
– When a procedure returns, it returns to the most recently active
procedure.
– When a procedure call is made, save current state on the stack.
On return, restore the state by popping the stack.