of 20
8/9/2019 Stacks 01
1/20
Stacks
8/9/2019 Stacks 01
2/20
Stack
A stack is a last in, first out (LIFO) data
structure
Items are removed from a stack in the reverseorder from the way they were inserted
8/9/2019 Stacks 01
3/20
Stack
With a stack, we always remove the item that
was most recently inserted. This policy is known
as last-in first-outorLIFO.
A common example of a stack is surfing the
Web. When you click a hyperlink, your browser
displays the new page (insert). You can keep
clicking on hyperlinks to visit new pages. You
can always revisit the previous page by clicking
the Back button (remove).
8/9/2019 Stacks 01
4/20
Stack
A stacksupports the insert and remove operations,
using a LIFO policy. By convention,
we name the stack insert operationpush and the stack
remove operationpop.
8/9/2019 Stacks 01
5/20
Initializing aStack
This algorithm initializes a stack to empty. Anempty stack has t= -1.
Input Parameters : None
Output Parameters: None
stack_init( ) {
t = -1
}
8/9/2019 Stacks 01
6/20
Testing foran EmptyStack
This algorithm returns true if the stack is empty orfalse if the stack is not empty. An empty stackhas t= -1.
Input Parameters: None
Output Parameters: None
empty() {
return t == -1}
8/9/2019 Stacks 01
7/20
Adding an Elementto aStack
This algorithm adds the value valto a stack. The stack isrepresented using an array data. The algorithm assumesthat the array is not full. The most recently added item isat index tunless the stack is empty, in which case, t= -1.
Input Parameters : val
Output Parameters: None
push(val) {
t = t + 1data[t] = val
}
8/9/2019 Stacks 01
8/20
Removing an Element From aStack
This algorithm removes the most recentlyadded item from a stack. The algorithmassumes that the stack is not empty. The
most recently added item is at index t.
Input Parameters : None
Output Parameters: Nonepop() {
t = t 1
}
8/9/2019 Stacks 01
9/20
Returning theTop Element in aStack
This algorithm returns, but does not remove, themost recently added item in a stack. Thealgorithm assumes that the stack is not empty.The stack is represented using an array data.
The most recently added item is at index t.
Input Parameters : None
Output Parameters: None
top() {return data[t]
}
8/9/2019 Stacks 01
10/20
One
This algorithm returns true if there is exactly one element on the stack.The code uses the abstract data type stack.
Input Parameter : s (the stack)
Output Parameters: None
one(s) {if (s.empty())
return false
val = s.top()
s.pop()
flag = s.empty()
s.push(val)
return flag
}
8/9/2019 Stacks 01
11/20
Initializing aStack
This algorithm initializes a stack to empty. Thestack is implemented as a linked list. The start ofthe linked list, referenced by t, is the top of the
stack. An empty stack has t=
null.
Input Parameters : None
Output Parameters: None
stack_init() {t = null
}
8/9/2019 Stacks 01
12/20
Testing foran EmptyStack
This algorithm returns true if the stack is empty or
false if the stack is not empty. An empty stack
has t= null.
Input Parameters: None
Output Parameters: None
empty() {return t == null
}
8/9/2019 Stacks 01
13/20
Adding an Elementto aStack
This algorithm adds the value valto a stack. The stack isimplemented using a linked list. The start of the linkedlist, referenced by t, is the top of the stack.
Input Parameters: valOutput Parameters: None
push(val) {
temp = new node
temp.data = val
temp.next = t
t = temp
}
8/9/2019 Stacks 01
14/20
Removing an Element From aStack
This algorithm removes the most recently addeditem from a stack. The stack is implementedusing a linked list. The start of the linked list,referenced by t, is the top of the stack. Thealgorithm assumes that the stack is not empty.
Input Parameters : None
Output Parameters: None
pop() {
t = t.next
}
8/9/2019 Stacks 01
15/20
Returning theTop Element in aStack
This algorithm returns, but does not remove, the mostrecently added item in a stack. The stack is implementedusing a linked list. The start of the linked list, referencedby t, is the top of the stack. The algorithm assumes that
the stack is not empty.
Input Parameters : None
Output Parameters: None
top() {return t.data
}
8/9/2019 Stacks 01
16/20
8/9/2019 Stacks 01
17/20
Pushing and popping
If the bottom of the stack is at location 0,then an empty stack is represented by top= -1 orcount = 0
To add (push) an element, either: Increment top and store the element in stk[top], or
Store the element in stk[count] and increment count
To remove (pop) an element, either: Get the element from stk[top] and decrement top, or
top = 3 or count = 4
17 23 97 44
0 1 2 3 4 5 6 7 8 9
stk:
8/9/2019 Stacks 01
18/20
After popping
When you pop an element, do you just leave thedeleted element sitting in the array?
The surprising answer is, it depends
If this is an array of primitives, orif you are programmingin C or C++, then doing anything more is just a waste oftime
If you are programming in Java, and the array containsobjects, you should set the deleted array element to
null
top = 2 or count = 3
17 23 97 44
0 1 2 3 4 5 6 7 8 9
stk:
8/9/2019 Stacks 01
19/20
Sharing space
Of course, the bottom of the stack could be at
the otherend
top = 6 or count = 4
17239744
0 1 2 3 4 5 6 7 8 9
stk:
Sometimes this is done to allow two stacks to
share the same storage area
topStk2 = 6
1723974449 57 3
0 1 2 3 4 5 6 7 8 9
stks:
topStk1 = 2
8/9/2019 Stacks 01
20/20
Error checking
There are two stack errors that can occur:
Underflow: trying to pop (or peek at) an empty stack
Overflow: trying to push onto an already full stack
For underflow, you should throw an exception
If you dont catch it yourself, Java will throw an
ArrayIndexOutOfBounds exception
You could create your own, more informative
exception
For overflow, you could do the same things
Or, you could check for the problem, and copy
everything into a new, larger array