7 december 20151 stacks (5.1) cse 2011 winter 2011
TRANSCRIPT
![Page 1: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/1.jpg)
April 21, 2023 1
Stacks (5.1)
CSE 2011
Winter 2011
![Page 2: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/2.jpg)
Stacks 2
Abstract Data Types (ADTs)
An abstract data type (ADT) is an abstraction of a data structure
An ADT specifies:Data storedOperations on the
dataError conditions
associated with operations
Example: ADT modeling a simple stock trading systemThe data stored are buy/sell
ordersThe operations supported are
order buy(stock, shares, price)order sell(stock, shares, price)void cancel(order)
Error conditions:Buy/sell a nonexistent stockCancel a nonexistent order
![Page 3: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/3.jpg)
3
Stacks: LIFO
Insertions and deletions follow the Last-In First-Out rule
Example applications:
– Undo operation in a text editor
– History of visited web pages
– Sequence of method calls in Java
![Page 4: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/4.jpg)
Stacks 4
Method Stack in the JVM
The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack
When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of
the statement being executed When a method ends, its frame
is popped from the stack and control is passed to the method on top of the stack
Allows for recursion
main() {int i = 5;foo(i);}
foo(int j) {int k;k = j+1;bar(k);}
bar(int m) {…}
bar PC = 1 m = 6
foo PC = 3 j = 5 k = 6
main PC = 2 i = 5
![Page 5: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/5.jpg)
5
Stack ADT
Data stored: arbitrary objects
Operations:
– push(object): inserts an element
– object pop(): removes and returns the last inserted element
Other useful operations:
– object top(): returns the last inserted element without removing it
![Page 6: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/6.jpg)
6
Error Conditions
push(object) object pop()object top()Exceptions are thrown when an operation cannot be
executed.Execution of pop() or top() on an empty stack
→ throws EmptyStackException.Another useful operation:
– boolean isEmpty(): returns true if the stack is empty;
false otherwise.
![Page 7: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/7.jpg)
7
Stack Operations
push(object) object pop()object top()boolean isEmpty()
Still another useful operation:
int size(): returns the number of elements in the stack
Any others?
Depending on implementation
![Page 8: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/8.jpg)
Stacks 8
Stack Interface in Java
Java interface corresponding to our Stack ADT
Requires the definition of class EmptyStackException
Different from the built-in Java class java.util.Stack
public interface Stack {
public int size();
public boolean isEmpty();
public Object top()throws EmptyStackException;
public void push(Object o);
public Object pop() throws EmptyStackException;
}
![Page 9: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/9.jpg)
9
Array-based Implementation
An array S of maximum size NA variable t that keeps track of the top element in array S
How to initialize t?Top element: S[t]push( ), pop( ): how to update t?Stack is empty, isEmpty( ): ?Number of elements in the stack, size(): ?
![Page 10: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/10.jpg)
10
Pseudo-code
Algorithm size():
return (t + 1);
Algorithm isEmpty():
return (t < 0);
Algorithm top():
if (isEmpty())
throw StackEmptyException;
return S[t];
Algorithm pop():
if (isEmpty())
throw StackEmptyException;
temp = S[t];
t = t – 1;
return temp;
Optimization: set S[t] to null before decrementing t.
Homework: implement pop() without any temp variable.
![Page 11: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/11.jpg)
11
Method push()
Algorithm push(object): t = t + 1; S[t] = object;
The array may become full push() method will then
throw a FullStackException Limitation of array-based
implementation One solution: extend the
stack
Algorithm push(object): if (size() == N) throw FullStackException; t = t + 1; S[t] = object;
![Page 12: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/12.jpg)
Stacks 12
Array-based Stack in Java
public class ArrayStackimplements Stack {
// holds the stack elements private Object S[ ];
// index to top elementprivate int top = -1;
// constructorpublic ArrayStack(int capacity) {
S = new Object[capacity]); }
public Object pop()throws EmptyStackException {
if isEmpty()throw new EmptyStackException
(“Empty stack: cannot pop.”);Object temp = S[top];// facilitates garbage collection S[top] = null;top = top – 1;return temp;
}
![Page 13: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/13.jpg)
13
Performance of Array Implementation
Each operation runs in O(1) time(no loops, no recursion)
Array-based implementation is simple, efficient, but …
The maximum size N of the stack is fixedHow to determine N? Not easy!
Alternatives?Extendable arraysLinked lists (singly or doubly linked???)
![Page 14: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/14.jpg)
Linked List Review
Operation Singly linked
(head, tail)
Doubly linked
(header, trailer)
insert first O( ) O( )
insert last O( ) O( )
remove first O( ) O( )
remove last O( ) O( )
![Page 15: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/15.jpg)
Linked List Implementation
Singly or doubly linked list?Where should the “top” be, head or tail?
15
A B C D
![Page 16: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/16.jpg)
16
push() Method
![Page 17: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/17.jpg)
pop() Method
17
![Page 18: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/18.jpg)
18
Analysis of Linked List Implementation
Space usage: O(n)
n = number of elements in the stack
Each operation runs in O(1) time
No limit on the stack size, subject to available memory
(run-time error OutOfMemoryError)
Java code: textbook, p. 212
![Page 19: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/19.jpg)
19
Homework
List-based and array-based operations all run in O(1) time. List-based implementation imposes no limit on the stack size, while array-based implementation does. Is list-based implementation better?
Can we perform push() and pop() at the tail of the linked list? Analyze the running time in this case.
Study the linked list implementation of stacks in Java on p. 212.
![Page 20: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/20.jpg)
More Applications of Stacks
Reversing an array using a stack (5.1.4)Matching parentheses, brackets, and quotes in
Java files (5.1.5)
20
![Page 21: 7 December 20151 Stacks (5.1) CSE 2011 Winter 2011](https://reader035.vdocuments.us/reader035/viewer/2022062322/5697bf7c1a28abf838c841b4/html5/thumbnails/21.jpg)
Next time ...
Queues (5.2)
21