basic abstract data types (ii)arcs.skku.edu/pmwiki/uploads/courses/datastructures/03.2... ·...

20
Basic Abstract Data Types (II) Hwansoo Han

Upload: others

Post on 29-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Basic Abstract Data Types (II)

Hwansoo Han

Page 2: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 3: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 4: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 5: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Array Implementation of Stack

5

Page 6: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Array Implementation of Stack

6

Page 7: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 8: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 9: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Pointer Implementation of Queue

9

Linked list with front and rear pointers

Page 10: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Pointer Implementation of Queue

10

dummy cell

Page 11: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Pointer Implementation of Queue

11

insert at the rear

delete at the front

Page 12: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Pointer Implementation of Queue

12

, dummy cell }

Page 13: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 14: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Circular Array Implementation of Queue

14

Page 15: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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)

Page 16: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 17: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Circular Array Implementation of Queue

17

Page 18: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Circular Array Implementation of Queue

18

Page 19: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

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

Page 20: Basic Abstract Data Types (II)arcs.skku.edu/pmwiki/uploads/Courses/DataStructures/03.2... · 2015-09-29 · Basic Abstract Data Types (II) Hwansoo Han . Stack 2 ... Operations on

Implementation of Mapping

20

Array implementation

Pointer implementation