Académique Documents
Professionnel Documents
Culture Documents
http://parasol.tamu.edu
Stacks
1/31/11
Error conditions:
Buy/sell a nonexistent stock Cancel a nonexistent order
4
1/31/11
Exceptions
Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be thrown by an operation that cannot be executed In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException
6
1/31/11
Exercise: Stacks
Describe the output of the following series of stack operations
Push(8) Push(3) Pop() Push(2) Push(5) Pop() Pop() Push(9) Push(1)
Applications of Stacks
Direct applications
Page-visited history in a Web browser Undo sequence in a text editor Saving local variables when one function calls another, and this one calls another, and so on.
Indirect applications
Auxiliary data structure for algorithms Component of other data structures
1/31/11
When a function returns, its frame is popped from the stack and control is passed to the method on top of the stack
Array-based Stack
A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element
S 0 1 2
Algorithm size()" "return t + 1" Algorithm pop()" "if isEmpty() then" " "throw EmptyStackException" " else " " "t t - 1" " "return S[t + 1]"
t
10
1/31/11
t
11
Limitations
The maximum size of the stack must be defined a priori , and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception
12
1/31/11
Algorithm push(o)" "if t = S.length - 1 then" " "A new array of" " " " " "size " " "for i 0 to t do" " " " A[i] S[i]" " " " S A" "t t + 1" "S[t] o"
13
14
1/31/11
15
16
1/31/11
geometric series 2 1 1 8
17
18
1/31/11
19
node
D
20
10
1/31/11
topt
elements Vectors
1/31/1121 02:50
Exercise
Describe how to implement a stack using a singlylinked list
Stack operations: push(x), pop( ), size(), isEmpty() For each operation, give the running time
Vectors
1/31/1122 02:50
11
1/31/11
Stack Summary
Stack Operation Complexity for Different Implementations
Array Array Fixed-Size Expandable (doubling strategy) Pop() Push(o) O(1) O(1) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1) O(1) List SinglyLinked O(1) O(1)
O(1) O(1)
O(1) O(1)
1/31/11 02:50
Vectors
23
Queues
24
12
1/31/11
25
Exceptions
Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException
26
13
1/31/11
Exercise: Queues
Describe the output of the following series of queue operations
enqueue(8) enqueue(3) dequeue() enqueue(2) enqueue(5) dequeue() dequeue() enqueue(9) enqueue(1)
27
Applications of Queues
Direct applications
Waiting lines Access to shared resources (e.g., printer) Multiprogramming
Indirect applications
Auxiliary data structure for algorithms Component of other data structures
28
14
1/31/11
Array-based Queue
Use an array of size N in a circular fashion Two variables keep track of the front and rear
f index of the front element r index immediately past the rear element
wrapped-around configuration Q 0 1 2 r f
29
Queue Operations
We use the modulo operator (remainder of division)
Q 0 1 2 Q 0 1 2 r f
30
15
1/31/11
Q 0 1 2 Q 0 1 2 r f
31
Algorithm dequeue()" "if isEmpty() then" " "throw EmptyQueueException" " else" " "o Q[f]" " "f (f + 1) mod N" " "return o"
16
1/31/11
Limitations
The maximum size of the stack must be defined a priori , and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception
34
17
1/31/11
Exercise
Describe how to implement a queue using a singlylinked list
Queue operations: enqueue(x), dequeue(), size(), isEmpty() For each operation, give the running time
Vectors
1/31/1135 02:50
The space used is O(n) and each operation of the Queue ADT takes O(1) time NOTE: we do not have the limitation of the array based implementation on the size of the stack b/c the size of the linked r list is not fixed, I.e., the queue is NEVER full. nodes rear front f
elements
36
18
1/31/11
37
Queue Summary
Queue Operation Complexity for Different Implementations
Array Array Fixed-Size Expandable (doubling strategy) dequeue() enqueue(o) O(1) O(1) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1) O(1) List SinglyLinked O(1) O(1)
O(1) O(1)
O(1) O(1)
1/31/11 02:50
Vectors
38
19
1/31/11
(4.5.1)
The Double-Ended Queue, or Auxiliary queue operations: Deque, ADT stores arbitrary first(): returns the element at the objects. (Pronounced deck) front without removing it Richer than stack or queue ADTs. last(): returns the element at the Supports insertions and deletions front without removing it at both the front and the end. size(): returns the number of Main deque operations: elements stored insertFirst(object o): inserts isEmpty(): returns a Boolean element o at the beginning of the value indicating whether no deque elements are stored
insertLast(object o): inserts element o at the end of the deque RemoveFirst(): removes and returns the element at the front of the queue RemoveLast(): removes and returns the element at the end of the queue
Exceptions
Attempting the execution of dequeue or front on an empty queue throws an EmptyDequeException
39
prev
next
elem
node
elements
40
20
1/31/11
The space used is O(n) and each operation of the Deque ADT takes O(1) time
first first last
elements
41
Limitations
NOTE: we do not have the limitation of the array based implementation on the size of the stack b/c the size of the linked list is not fixed, I.e., the deque is NEVER full.
21
1/31/11
Deque Summary
Deque Operation Complexity for Different Implementations
Array FixedSize removeFirst(), removeLast() O(1) Array Expandable (doubling strategy) O(1) List SinglyLinked O(n) for one at list tail, O(1) for other O(1) List DoublyLinked O(1)
O(1)
O(n) Worst Case O(1) Best Case O(1) Average Case O(1) O(1)
O(1)
O(1) O(1)
O(1) O(1)
O(1) O(1)
1/31/11 02:50 43
Vectors
22