º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥®...

31
ʭʩʰʥʺʰ ʩʰʡʮ ʭʩʩʱʩʱʡ ʭʩʰʥʺʰ ʩʰʡʮ 1

Transcript of º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥®...

Page 1: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

1

Page 2: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

ס - •

י •

•- י– : ' : י –

2

Page 3: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

• –

• – –

• – –

3

0 1 2 3 4 5

Page 4: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

– •

– – – –

• – –

4

Page 5: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Doubly Linked List

search (k, list)

x = list.head

while ≠ null &

.ke ≠ k do

x = x.next

return x

insert (item, list)

item.next = list.head

if list.head ≠ null) then

list.head.prev = item

list.head = item

item.prev = null

delete (item, list)

if ite .pre ≠ null) then

item.prev.next = item.next

else

list.head = item.next

if ite . e t ≠ null) then

item.next.prev = item.prev

5

Page 6: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Doubly Linked List with Sentinels

delete (item)

item.prev.next = item.next

item.next.prev = item.prev]

6

Page 7: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

פ (ADT)

– a set of values

– a set of operations, which

can be applied uniformly to

all these values

– a data representation, and

the implementation of the

operations.

abstract

implementation

• Sometimes the data type is completely identified

with the implementation so we talk about concrete

data type. 7

Page 8: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Abstract Stack

• A stack is a collection of items, for which the

following operations are defined:

– create(S) creates an empty stack S.

– isEmpty(S) returns true if S is empty, and false

otherwise.

– push(S, item) adds the given item to the stack S.

– pop(S) removes and returns the most recently

added item from the stack S.

8

Page 9: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Implementing Stack with Linked List

• create - create empty list

• isEmpty – iff the list is empty

• push - simply adds the item to the head of the

list

• pop - returns the head of the list, and replaces

the list by its tail

9

Page 10: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Implementing (Finite) Stack with Array

• create(S, N) 1. Initialize an array of size N

2. Initialize S.top to 0.

• isEmpty(S) 1. if S.top = 0 return TRUE else return FALSE

• push(S,item) 1. if S.top = N // S has N elements

2. error “o erflo “

3. else S.top S.top+1

4. S[S.top] item;

• pop(S) 1. if isEmpty (S)

2. error "underflow"

3. else S.top S.top-1

4. return(S[S.top+1]) 10

Page 11: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

11

:

2*(2/[7+5])+3

Page 12: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

12

:

2*(2/[7+5])+3

Page 13: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

13

:

2*(2/[7+5])+3 (

Page 14: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

14

:

2*(2/[7+5])+3 (

Page 15: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

15

:

2*(2/[7+5])+3 (

Page 16: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

16

:

2*(2/[7+5])+3 (

[

Page 17: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

17

:

2*(2/[7+5])+3 (

[

Page 18: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

18

:

2*(2/[7+5])+3 (

[

Page 19: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

19

:

2*(2/[7+5])+3 (

[

Page 20: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

20

:

2*(2/[7+5])+3 (

[ ]

Page 21: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

21

:

2*(2/[7+5])+3 (

Page 22: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

22

:

2*(2/[7+5])+3 ( )

Page 23: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

23

:

2*(2/[7+5])+3

Page 24: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

24

:

2*(2/[7+5])+3

Page 25: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

25

:

2*(2/[7+5])+3

Page 26: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

26

:

2*(2/[7+5])+3

Page 27: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

27

:

2*(2/[7+5])+3

Page 28: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Abstract Queue

• A queue is a collection of items, for which the

following operations are defined:

– create(Q) creates an empty queue Q.

– isEmpty(Q) returns true if Q is empty, and false

otherwise.

– enqueue(Q, item) adds the given item to the

queue Q.

– dequeue(Q) removes and returns from the queue

Q the least recently added item.

28

Page 29: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Implementing Queue with Linked List

• create - create empty list

• isEmpty – true iff the list is empty

• enqueue - simply adds the item to the tail of

the list

• dequeue- returns the head of the list, and

replaces the list by its tail

29

Page 30: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

Implementing (Finite) Queue with

Array • create(Q, N)

1. Initialize an array of size N // avoid full array

2. Q.head and Q.tail are initialized to point to the first cell in Q.

• isEmpty(Q)

1. If Q.head = Q.tail return TRUE else return FALSE

• enqueue(Q, item)

1. if Q.tail = Q.N then Q.next 1

2. else Q.next Q.tail + 1

3. if Q.next = Q.head // S has N-1 elements

4. error “o erflo “

5. else Q[Q.tail] item;

6. Q.tail ← e t • dequeue(Q)

1. if isEmpty (Q)

2. error "u derflo “

3. else x Q [Q.head]

4. if Q.head = Q.N then Q.head 1

5. else Q.head Q.head + 1

6. return x 30

Page 31: º°©°¡® - cs.bgu.ac.ilds202/wiki.files/Presentation03-ADT.pdf · ADT ¨¹´¥® ©°¥º°¤°¡® ± a set of values ± a set of operations, which can be applied uniformly to

• (buffers)

31