Next:Chapter
4: TreesUp:CS
3345: Algorithm AnalysisPrevious:Chaper
2
Chapter 3: Lists, Stacks and
Queues
Lists, Stacks, and Queues
Primitive Data Types
-
examples: integers, reals, Booleans
-
associated operations
Abstract Data Types (ADT)
-
examples: lists, sets, graphs, etc.
-
associated operations
-
well defined interface
-
internal details hidden (hopefully!)
-
transparent changes to implementation
Modularity!
-
easier to debug smaller routines
-
easier to handle big projects
-
side effects absent/minimized
List ADT
-
-
null list: list of size 0
-
successor and predecessor
-
position of ai = i
-
appropriate operations defined
-
various implementation approaches
List Operations
Operation Array Linked List
Print_List O(n) O(n)
Make_Empty O(n) O(n)
Find O(n) O(n)
Insert O(n) O(1)
Remove O(n) O(1)
Find_Kth O(1) O(n)
Next O(1) O(1)
Previous O(1) ?
Array Implementation of Lists
-
estimate of maximum list size needed
-
wasteful!
Linked Lists Implementation
-
elements have pointer to next element
-
last element's pointer = NULL
-
pointer to beginning of list
Caution!
-
insertion/deletion of list beginning
-
removal: Find_Previous
Doubly, Circularly Linked Lists
-
backward traversal of lists
-
cost of extra links
-
doubles insertion and deletion costs
-
simplifies deletions
Sorted Linked Lists
-
elements arranged in ascending/descending order
Question: Write new specification?
Answer: Inherit from list (base class)
add routine for insertion
Polynomial ADT
-
array representation: when most coefficients non-zero
-
multiplication = O(product of degrees of input)
-
linked list appropriate if most coefficients zero
-
multiplication = O() ?
Radix Sort
-
sort n integers in the range 0 to np-1
-
use base n arithmetic
-
sort by least significant digit
-
then by next least significant digit
-
make p passes
-
multiple elements in a bucket: maintain a list
-
time complexity =
Multilists
-
combining multiple lists
-
use circular lists to save space in place of doubly linked lists
-
tradeoff: time
-
use doubly linked lists to save time
-
tradeoff: space
Cursor Implementation of Linked Lists
-
some languages do not support pointers
-
use arrays of objects instead
-
start with a Freelist
-
allocate space from Freelist when needed
-
to delete: change pointers, add to Freelist
Stacks
-
operations performed only at the top of the stack
-
operations: push, pop, top
-
linked list implementation
-
array implementation
Linked List Implementation
-
singly linked list
-
Push = insert at front of list
-
Pop = delete at the front of list
-
Top = examine element at the front
-
no limit on the size of stack
Array Implementation
-
need to declare array size a priori
-
Top (tos): -1 for empty stack
-
Push(x):
-
Pop(x):
-
output(stack[tos]);
-
tos-;
Stack Applications
-
Balancing symbols
-
Postfix / reverse Polish expressions:
-
5 * 2 + 6 + 7 * 2 = ?
-
5 2 * 6 + 7 2 * + = ?
-
Infix to Postfix conversion
-
output operands
-
right parenthesis: pop stack, output until left parenthesis
-
other operands: pop, output all higher/equal precedence operators, push
operand
-
end of input: pop, output remaining stack elements
-
example: a + b * c + ( d * e + f) * g
Stack Applications (contd.)
-
Function calls
-
register contents, return address stored on function calls
-
restored on return from function
-
how to handle nested function calls?
-
how to handle recursion?
Queues
-
Queue: list
-
Enqueue: at end of list
-
Dequeue: from the beginning of list
-
linked list implementation
-
array representation
Array Implementation of Queues
-
variables Q_Front, Q_Rear, Q_Size
-
circular array implementation
-
modulo Size implementation
-
watch out if Q_Size not maintained!
-
Base case: Q_Rear == Q_Front - 1
-
cannot distinguish between full and empty queue
-
store Size-1 elements
Next:Chapter
4: TreesUp:CS
3345: Algorithm AnalysisPrevious:Chaper
2
Ravi Prakash
1999-11-17