3 02 Stacks
3 02 Stacks
Stacks
ece.uwaterloo.ca
[email protected]
3.2 Outline
3.2.2 Applications
Numerous applications:
– Parsing code:
• Matching parenthesis
• XML (e.g., XHTML)
– Tracking function calls
– Dealing with undo/redo operations
– Reverse-Polish calculators
– Assembly language
Problem solving:
– Solving one problem may lead to subsequent problems
– These problems may result in further problems
– As problems are solved, your focus shifts back to the problem which
lead to the solved problem
3.2.3 Implementations
We will look at
– Singly linked lists
– One-ended arrays
Stacks
8
Front/1st Back/nth
Find Q(1) Q(1)
Insert Q(1) Q(1)
Erase Q(1) Q(n)
The stack class using a singly linked list has a single private
member variable:
The empty and push functions just call the appropriate functions of
the Single_list class
The top and pop functions, however, must check the boundary case:
return list.pop_front();
}
Stacks
14
Front/1st Back/nth
Find Q(1) Q(1)
Insert Q(n) Q(1)
Erase Q(n) Q(1)
Stacks
15
3.2.3.2 Destructor
3.2.3.2 Constructor
#include <algorithm>
// ...
3.2.3.2 Constructor
3.2.3.2 Destructor
3.2.3.2 Empty
3.2.3.2 Top
3.2.3.2 Pop
--stack_size;
return array[stack_size];
}
Stacks
23
3.2.3.2 Push
array[stack_size] = obj;
++stack_size;
}
Stacks
24
3.2.3.2 Exceptions
The case where the array is full is not an exception defined in the
Abstract Stack
W
Stacks
30
The implementation:
void double_capacity() {
Type *tmp_array = new Type[2*array_capacity];
}
Stacks
32
The implementation:
void double_capacity() {
Type *tmp_array = new Type[2*array_capacity];
tmp_array
}
Stacks
33
The implementation:
void double_capacity() {
Type *tmp_array = new Type[2*array_capacity];
}
Stacks
34
The implementation:
void double_capacity() {
Type *tmp_array = new Type[2*array_capacity];
delete [] array;
}
Stacks
35
The implementation:
void double_capacity() {
Type *tmp_array = new Type[2*array_capacity];
delete [] array;
array = tmp_array;
array_capacity *= 2;
}
Stacks
36
First, we recognize that any time that we push onto a full stack, this
requires n copies and the run time is Q(n)
2 k 2
k 0
h 11
1 2h 1 n 1 n
Examples includes:
– Matching tags in XHTML
– In C++, matching
• parentheses ( ... )
• brackets, and [ ... ]
• braces { ... }
Stacks
47
You will use XHTML (and more generally XML and other markup
languages) in the workplace
Stacks
48
Nesting indicates that any closing tag must match the most recent
opening tag
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
Stacks
52
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <head>
Stacks
53
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <head>
Stacks
56
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <body>
Stacks
57
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <body>
Stacks
62
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
Stacks
63
Possible errors:
– a closing tag which does not match the opening tag on top of the stack
– a closing tag when the stack is empty
– the stack is not empty at the end of the document
Stacks
64
3.2.5.1 HTML
<html>
<head><title>Hello</title></head>
<body><p>This is a list of topics:
<ol> <!-- para ends with start of list -->
<li><i>veni <!-- implied </li> -->
<li>vidi <!-- italics continues -->
<li>vici</i>
</ol> <!-- end-of-file implies </body></html> -->
3.2.5.1 XML
The next example shows how stacks may be used in parsing C++
http://xkcd.com/859/
Stacks
68
For C++, the errors are similar to that for XHTML, however:
– many XHTML parsers usually attempt to “correct” errors (e.g., insert
missing tags)
– C++ compilers will simply issue a parse error:
{eceunix:1} cat example1.cpp
#include <vector>
int main() {
std::vector<int> v(100];
return 0;
}
For C++, the errors are similar to that for XHTML, however:
– many XHTML parsers usually attempt to “correct” errors (e.g., insert
missing tags)
– C++ compilers will simply issue a parse error:
{eceunix:1} cat example2.cpp
#include <vector>
int main() {
std::vector<int> v(100);
v[0] = 3];
return 0;
}
{eceunix:2} g++ example2.cpp
example2.cpp: In function 'int main()':
example2.cpp:4: error: expected ';' before ']' token
Stacks
70
In ECE 222 Digital Computers, you will see how stacks are
implemented in hardware on all CPUs to facilitate function calling
You will notice that the when a function returns, execution and the
return value is passed back to the last function which was called
http://www.audiovis.nac.gov.pl/
http://xkcd.com/645/
Stacks
76
Other examples:
3 4 5 × + 6 –
3 20 + 6 –
23 6 –
17
3 4 5 6 – × +
3 4 –1 × +
3 –4 +
–1
Stacks
77
Benefits:
– No ambiguity and no brackets are required
– It is the same process used by a computer to perform computations:
• operands must be loaded into registers before operations can be performed
on them
– Reverse-Polish can be processed using stacks
Stacks
78
http://www.tailrecursive.org/postscript/examples/rotate.html
Stacks
80
1
Stacks
83
2
1
Stacks
84
3
2
1
Stacks
85
5
1
Stacks
86
4
5
1
Stacks
87
5
4
5
1
Stacks
88
6
5
4
5
1
Stacks
89
30
4
5
1
Stacks
90
–26
5
1
Stacks
91
7
–26
5
1
Stacks
92
–182
5
1
Stacks
93
–177
1
Stacks
94
178
Stacks
95
8
178
Stacks
96
9
8
178
Stacks
97
72
178
Stacks
98
250
Stacks
99
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value on the top: 250
The equivalent in-fix notation is
((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))
Incidentally,
1 – 2 + 3 + 4 – 5 × 6 × 7 + 8 × 9 = – 132
which has the reverse-Polish notation of
1 2 – 3 + 4 + 5 6 7 × × – 8 9 × +
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> istack;
istack.push( 13 );
istack.push( 42 );
cout << "Top: " << istack.top() << endl;
istack.pop(); // no return value
cout << "Top: " << istack.top() << endl;
cout << "Size: " << istack.size() << endl;
return 0;
}
Stacks
103
Stacks
We looked at:
– Parsing, function calls, and reverse Polish
Stacks
105
References
Donald E. Knuth, The Art of Computer Programming, Volume 1: Fundamental Algorithms, 3rd
Ed., Addison Wesley, 1997, §2.2.1, p.238.
Cormen, Leiserson, and Rivest, Introduction to Algorithms, McGraw Hill, 1990, §11.1, p.200.
Weiss, Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison Wesley, §3.6, p.94.
Koffman and Wolfgang, “Objects, Abstraction, Data Strucutes and Design using C++”, John
Wiley & Sons, Inc., Ch. 5.
Wikipedia, http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.