basic abstract data types (ii)arcs.skku.edu/pmwiki/uploads/courses/datastructures/03.2... ·...
TRANSCRIPT
Basic Abstract Data Types (II)
Hwansoo Han
Stack
2
Special kind of list
Insertions and deletions only at the top
Pushdown list, LIFO (last-in-first-out) list
Operations on Stack S
MAKENULL(S) // make an empty stack
TOP (S) // retrieve the element at the top
POP (S) // delete the top element
PUSH (x, S) // insert element x at the top
EMPTY (S) // return TRUE if S is empty
Pointer Implementation of Stack
3
Linked list implementation of Stack is easy
Insertion at the header operates as PUSH
Deletion of the first element operates as POP
Array Implementation of Stack
4
Array implementation of List works but not efficient
PUSH/POP move all the elements in list up or down
Take time proportional to the length of list
Upside down the array implementation
Anchor the bottom of the stack at the bottom of the array
Stack grows towards the top of the array
Array Implementation of Stack
5
Array Implementation of Stack
6
Array Implementation of Stack
7
Check status of stack before operations
Call EMPTY (S) first inside TOP (S), POP (S)
Check if stack is full inside PUSH (S)
Update the position of top accordingly
Test code for full
Queue
8
Another special kind of list
Insertions at one end (rear)
Deletions at the other end (front)
FIFO (first-in-first-out) list
Operations on Queue Q
MAKENULL (Q) // make an empty queue
FRONT (Q) // retrieve the first element
ENQUEUE (x, Q) // insert element x at the rear
DEQUEUE (Q) // delete the first element at the front
EMPTY (Q) // return TURE if Q is empty
Pointer Implementation of Queue
9
Linked list with front and rear pointers
Pointer Implementation of Queue
10
dummy cell
Pointer Implementation of Queue
11
insert at the rear
delete at the front
Pointer Implementation of Queue
12
, dummy cell }
Circular Array Implementation of Queue
13
Array implementation of list for Queue
ENQUEUE (x, Q) : insert at the position last
DEQUEUE (Q) : shift all elements by one but the first
Not efficient!
Circular array
Think of an array as a circle
No need to shift elements for ENQUEUE/DEQUEUE
Move pointers rear and front only around the circle
Circular Array Implementation of Queue
14
Circular Array Implementation of Queue
15
Both pointers, rear and front move clockwise
Both point to array elements with valid contents
ENQUEUE at the rear
Circular-add rear index by 1 (move clockwise)
Then, write the element in that position
DEQUEUE at the front
Circular-add front index by 1 (move clockwise)
Circular Array Implementation of Queue
16
Empty condition
The rear points to the right behind the front in clockwise
Full condition
If we fill up the all elements, the condition is the same as
empty condition
Do not fill up the last element!
The rear points to the two elements behind the front in
clockwise
Circular Array Implementation of Queue
17
Circular Array Implementation of Queue
18
Mappings
19
Associative store
M(d) = r
d of domain type, r of range type
Operations on Mapping, M
MAKENULL (M) // make M be the null mapping
ASSIGN (M, d, r) // define M(d) = r
COMPUTE (M, d, r) // True and set r = M(d) if M(d) defined,
// otherwise false
Implementation of Mapping
20
Array implementation
Pointer implementation