º°©°¡® - cs.bgu.ac.ilds142/wiki.files/presentation03-adt.pdf · ± push (s, item) adds the...

Post on 10-Aug-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

ס - •

י •

•- י– : ' : י –

2

• –

• – –

• – –

3

0 1 2 3 4 5

– •

– – – –

• – –

4

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

Doubly Linked List with Sentinels

delete (item)

item.prev.next = item.next

item.next.prev = item.prev]

6

פ (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

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

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

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

11

:

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

12

:

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

13

:

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

14

:

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

15

:

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

16

:

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

[

17

:

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

[

18

:

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

[

19

:

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

[

20

:

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

[ ]

21

:

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

22

:

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

23

:

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

24

:

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

25

:

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

26

:

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

27

:

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

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

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

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

• (buffers)

31

top related