TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists . Page 2

advertisement
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 1
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 2
J. Maluszynski, IDA, Linköpings Universitet, 2005.
ADT Stack (Last In First Out)
Overview: Stacks, Queues, Lists
abc
Sequences of data items appear in many applications;
How to represent them in memory?
What are typical and specific operations ? (define ADTs)
How to implement them?
ADT Stack
top
top
[Goodrich/Tamassia 4.2]
[Goodrich/Tamassia 4.3, 4.4.3]
ADT List and its linked representations
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
top
d
.
.
.
Top S returns the top element of stack S
Pop S removes and returns the top element of S
Push S x adds x on the top of S
MakeEmptyStack creates a new, empty stack
IsEmptyStack S returns true iff S is empty
d
.
.
.
or an error code, if S is empty
or an error code, if S is empty
– in contiguous memory
– in linked memory
Operations:
d
.
.
.
a
b
c
– important applications (recursion, evaluation of expressions)
– representation in contiguous memory,
– representation in linked memory
ADT Queue and its representations
cba
Page 3
[Goodrich/Tamassia 4.4, 5.2]
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Typical applications of ADT Stack
Page 4
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Computing Factorial
Definition:
Implementation of recursive procedures,
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Evaluation of arithmetic expressions,
f act n
Cheking correctness of parantheses nesting (e.g. XML tags validation)
1
n f act n
if n
1 if n
0
0
Store intermediate result for each recursion step on a stack S:
f act 3
3 f act 2
Remember 3 until f act 2 is computed: Push S 3
f act 2
2 f act 1
Remember 2 until f act 1 is computed: Push S 2
space consumption is O n
1;
1 Pop S Pop S Pop S
f act 0
f act 3
f act 1
1 f act 0
Remember 1 until f act 0 is computed: Push S 1
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 5
A recursive call is tail-recursive iff
the first instruction after returning from it is return.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
tail-recursive functions can be rewritten using iteration
The recursive call in fact is not tail-recursive:
function fact (integer n) : integer
if n 0 then return 1
else f
n f act n 1 ; return f
Page 7
Run-time stack
immediately before
calling fact(0)
stack not needed: everything on stack will be discarded
The first instruction after returning from the recursive call is multiplication
n must be kept on stack
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
1
return address
1
fact(1)
2
return address
2
fact(2)
3
return address
fact(3)
3
on exit from the call:
pop and discard the arguments
go to the return address
(free)
...
function fact (integer n) : integer
if n 0 then return 1
n f act n 1 ; return f
else f
for each function call:
push arguments, local variables, return address
(which form the function’s activation record),
enter the procedure
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 8
Tail-recursive function
Tail Recursion Elimination
Consider binarySearch:
Both recursive calls can be eliminated:
J. Maluszynski, IDA, Linköpings Universitet, 2005.
function binarySearch (table key T a b , key K) : integer
(1) if a b then return 1
middle
a b 2
if K T middle then
return middle
else if K T middle then
was: return binarySearch T a middle 1 K
b middle 1
else K T middle
was: return binarySearch T a middle 1 K
a middle 1
goto (1)
function binarySearch (table key T a b , key K) : integer
if a b then return 1
middle
a b 2
if K T middle then
return middle
else if K T middle then
return binarySearch T a middle 1 K
else K T middle
return binarySearch T middle 1 b K
Both recursive calls are tail-recursive.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Tail Recursion
Stack application: Implementing recursive procedures
The programming language implementation
(compiler + run-time system)
uses internally a stack to keep track of recursion.
Page 6
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 9
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Tail-recursive Factorial Function
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
n-1
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Implementing a Stack in Contiguous Memory (2)
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Implementing a Stack in Linked Memory
function MakeEmptyStack() : pointer
S
NewCell(StackListHead)
Λ
ptop S
return S
x2
Info Next
x0
Info Next
ptop
function Pop( pointer S ) : dataitem
if ptop S
Λ then error
else
y
ptop S
x In f o y
ptop S
Next y
FreeCell y
return x
...
procedure Push( pointer S, dataitem x )
y NewCell(StackListItem)
In f o y
x
ptop S
Next y
ptop S
y
S
x1
Info Next
function IsEmptyStack( pointer S ) : boolean
return Length S
0
Page 12
function Top( pointer S ) : dataitem
if Length S
0 then error
else
return A S Length S 1
Page 11
Length(S) = k + 1 <= n
function Pop( pointer S ) : dataitem
0 then error
if Length S
else
Length S
Length S 1
return A S Length S
A Length n
S
.
.
.
.
.
.
xk
0
f act2 is tail-recursive.
space consumption after recursion elimination will be O 1
1)
procedure Push( pointer S, dataitem x )
if Length S
n then error
else
A S Length S
x
Length S 1
Length S
x0
x0
function fact2 (integer n, integer f ) : integer
if n 0 then
return f
else
return f act2 n 1 n f
0 n
S
.
.
.
function MakeEmptyStack() : pointer
S
NewCell(StackTableHead)
NewCell(table dataitem
AS
Length S
0
return S
xk
function fact (integer n) : integer
return f act2 n 1
All operations take O 1 time.
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Implementing a Stack in Contiguous Memory (1)
f act can be rewritten by introducing a help function:
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 10
+ maximum size need not be known in advance
– call to NewCell in each Push operation and to FreeCell in each Pop operation
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 13
J. Maluszynski, IDA, Linköpings Universitet, 2005.
ADT Queue (First In First Out)
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 14
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Implementing a Queue by a Ring Buffer
last
.
.
.
x2 x3
x0 x1
0
a b c ...
.
.
.
abc
first
n-1
Operations:
Front Q returns the first element of Q
Dequeue Q removes and returns the first element of Q
Enqueue Q x adds x at the end of Q
MakeEmptyQueue creates a new, empty queue
IsEmptyQueue Q returns true iff Q is empty
S
A
Back
Front
Length = k < n+1
Length
Typical application of ADT Queue:
Serving requests in incoming order.
Did you try to book a SAS ticket by phone?
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 15
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Lists
x2
A list L is a sequence of elements x0
xn
1
Info Next
size or length L
pFront pBack
empty list
with length 0
Selection by index i (sometimes called rank): selects the i-th element, xi
where i is an integer in the range 0 n 1 ;
Java code for the Queue operations: see course book,
[Goodrich/Tamassia, Section 4.4.3]
Selection by actual position, e.g. first element of L or last, next, prev,...
Position abstracts from indexing.
– overhead for NewCell in Enqueue, for FreeCell in Dequeue
needs Θ k time)
+ maximum size need not be known in advance
+ pointer pBack allows Enqueue to run in O 1 time
(without pBack: need to traverse the entire list
n
Q
Page 16
x1
Info Next
B mod n
x0
F
Pseudocode for the Queue operations: [Goodrich/Tamassia, 4.3.2]
Implementing a Queue in Linked Memory
Info Next
n
Back = index of last available cell; Front = index of the first element
Length or Size to control overflow:
ADT Vector: using rank
ADT List: using position
[Goodrich, Tamassia 5.1]
[Goodrich, Tamassia 5.2]
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 17
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
ADT Vector
ADT List (1)
Domain: lists
Domain: lists
Operations on a vector S
Operations on a list L
size
1
insertAtRank i x inserts x as a new element at rank i: increases the size;
error if i 0 or i size
0 and false otherwise
first
returns the position of the first element of L; error if L is empty
last
returns the position of the last element of L; error if L is empty
prev p returns the position of the element of L preceding p ; error if p is
the first position.
next p returns the position of the element of L following p ; error if p is the
last position.
removeAtRank i removes and returns the i-th element of S: decreases the
size; error if i 0 or i size
1
returns true if S
0 or i
isEmpty
J. Maluszynski, IDA, Linköpings Universitet, 2005.
elemAtRank i returns S i ; error if i
0 and false otherwise
returns true if S
isEmpty
size L returns L
returns S
size
Page 18
...cont. next slide
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 19
J. Maluszynski, IDA, Linköpings Universitet, 2005.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 20
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Implementing ADT Vector, ADT List
ADT List (2)
Update operations
insertFirst x insert a new element x as the first element of L, return the
position of x.
insertBefore p x insert a new element x before position p of L, return the
position of x.
insertAfter p x insert a new element x before position p of L, return the
position of x.
remove p remove from L and return the element at position p of L, return
the position of x.
– elem retrieval operations in O 1 ;
– what about other operations?
insertLast x insert a new element x as the last element of L, return the
position of x.
Contiguous memory representation:
elements stored in a table/ array (e.g. Dynamic Vectors Lecture 2).
Singly linked lists: see next slide.
– positions implemented as pointers
– Analyze time complexity of the operations.
– prev, insert Before require list traversal.
Doubly linked lists: see below.
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 21
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Singly Linked Lists
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Info
Info
x0
x1
P
Insertion
D
Prev
Next
(pFront) (pBack)
L
P
Deletion
L
P
TDDB56 DALGOPT-D – Lecture 3: Stacks, Queues, Lists .
Page 23
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Insertion in a Doubly Linked List
......
Info
Info
x0
x2
Prev Next
Prev
Info
x3
Next
......
Prev Next
P
Insertion:
......
J. Maluszynski, IDA, Linköpings Universitet, 2005.
Doubly Linked Lists
Prev Next
L
Page 22
Info
Info
x0
x2
Prev
Prev Next
Info
x1
P
Prev
Next
Info
x3
Next
Prev Next
......
Prev
Info
x2
Next
Prev Next
Download