Lecture
27 Stacks I
(Please
note that the Stack datastructure is one of the most often used
Interview Questions. If Stack-related problems appear in your Quiz or
Exam, it will be a Close-book, Close-note, and Close-internet Exam).
1. Introduction to the Stack
datastructure
A stack is a container of objects that are inserted and removed
according to the last-in first-out (LIFO) principle. In the pushdown
stacks only two operations are allowed: push the item into the stack,
and pop the item out of the stack. A stack is a limited access data
structure - elements can be added and removed from the stack only at
the top. push adds an item to the top of the stack, pop removes the
item from the top. A helpful analogy is to think of a stack of books;
you can remove only the top book, also you can add a new book on the
top. A stack is a recursive data structure.
Here is a
structural definition of a Stack: a stack is either empty or it
consistes of a top and the rest which is a stack.
A LIFO data
structure looks like this:
Fig. 1 The stack data structure
'Data 6' is the
last one goes into the stack and it'll be the first one to be popped
out of the stack. (LIFO).
A top pointer (stores a block address of stack) is used perform traverse, search, add and remove operations on the stack.
Basically, there are few operations in a stack.
* Push to move top pointer to the next new location maintaining the contact with the previous one and add object to it.
* Pop to remove an object from the top pointed location and move top to the previous one.
* Peek to check which element is present at top-most location.
* isEmpty to check if the stack is empty.
We call it stack-overflow,when the stack is full and stack-underflow, when it is emptied.
To implement
this data structure in Python is not hard. The following code in file 'stack.py' will take care of
this.
# stack.py
What the 'push'
does is just push data into the stack one-by-one, like the left part of
Fig. 1 in the tutorial. 'pop' removes the item on the top of the stack
as it shows on the right part of Fig. 1.
In the Python
implementation, if we use a list to store the data, then the list can
be treated as a stack. The right side of the list is the openning of
the stack on the top, the left side of the list is the bottom of the
stack.
2. Using the
Stack datastructure to solve problems.
2.1 Convert a
decimal number to its binary form
If you don't
know how to do it by hand, please refer to the 'Binary Basics' lecture.
The strategy to
implement this conversion using the Stack datastructure is shown in the
following figure:
The code to
implement it is showed below.
#
stack_div_by_two.py
2.2 One more
example - you can also use the stack datastructure to reverse a string.
'Reverse a string' is a very common Interview
Question.
2.3 Finally,
let's see the example of 'checking the balance of parenthesis'. Please
read the comment area on the top of the script. There are several
situations of unbalanced parenthesis that your the code must be able to
handel.
There are two
cases at Line 37 and 38 that this version can't handle. The reason for
Line 37 is all the 'left half' of the parenthesis have been popped out
/ removed but there are still some 'right half' of the parenthesis
left. When there is nothing to pop if you call the .pop() method, an
exception will be triggered.
For Line 38, the
stack never got a chance to store anything in there since there is no
'left halves' of any parenthesis.
The code should
be:
Is this good enough?
Probably not.
If
I have something like: '((', '{{', '[[[[[', or '({{{[[[', the
code doesn't have anything to handle these cases. Also, if I
have an empty string input like '', the code will return 'True'....
Let's add the following code
to handle the input of '((', '{{', '[[[[[', or '({{{[[[', or '' which cannot
be resolved by this current version. We solve the problem by doing the
following modifications:
Tasks:
1.
Understand the Stack datastructure and use it to solve the 'divided by
two', 'reverse a string', and the 'parenthesis balance check' problems
by yourself. (Understand the solutions in the tutorial first and then
type it out, use Vim, by yourself). (possible quiz/exams in the future on this will be close-book, close-notes, and close-internet problems).
(without
looking at the solution, use Vim to type these out for as many times as
you can, until you can get it right independently).
Upload your code to your GitHub account and send the link of the repository to the homework email.