º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the...
Transcript of º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the...
![Page 1: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/1.jpg)
1
![Page 2: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/2.jpg)
ס - •
י •
•- י– : ' : י –
2
![Page 3: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/3.jpg)
• –
• – –
• – –
3
0 1 2 3 4 5
![Page 4: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/4.jpg)
•
– •
– – – –
• – –
4
![Page 5: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/5.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/6.jpg)
Doubly Linked List with Sentinels
delete (item)
item.prev.next = item.next
item.next.prev = item.prev]
6
![Page 7: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/7.jpg)
פ (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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/8.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/9.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/10.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/11.jpg)
11
:
2*(2/[7+5])+3
![Page 12: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/12.jpg)
12
:
2*(2/[7+5])+3
![Page 13: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/13.jpg)
13
:
2*(2/[7+5])+3 (
![Page 14: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/14.jpg)
14
:
2*(2/[7+5])+3 (
![Page 15: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/15.jpg)
15
:
2*(2/[7+5])+3 (
![Page 16: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/16.jpg)
16
:
2*(2/[7+5])+3 (
[
![Page 17: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/17.jpg)
17
:
2*(2/[7+5])+3 (
[
![Page 18: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/18.jpg)
18
:
2*(2/[7+5])+3 (
[
![Page 19: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/19.jpg)
19
:
2*(2/[7+5])+3 (
[
![Page 20: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/20.jpg)
20
:
2*(2/[7+5])+3 (
[ ]
![Page 21: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/21.jpg)
21
:
2*(2/[7+5])+3 (
![Page 22: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/22.jpg)
22
:
2*(2/[7+5])+3 ( )
![Page 23: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/23.jpg)
23
:
2*(2/[7+5])+3
![Page 24: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/24.jpg)
24
:
2*(2/[7+5])+3
![Page 25: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/25.jpg)
25
:
2*(2/[7+5])+3
![Page 26: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/26.jpg)
26
:
2*(2/[7+5])+3
![Page 27: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/27.jpg)
27
:
2*(2/[7+5])+3
![Page 28: º°©°¡® - cs.bgu.ac.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/28.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/29.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/30.jpg)
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.ilds142/wiki.files/Presentation03-ADT.pdf · ± push (S, item) adds the given item to the stack S. ± pop (S) removes and returns the most recently added](https://reader033.fdocuments.net/reader033/viewer/2022050523/5fa64f70935cfb3e4a4b6748/html5/thumbnails/31.jpg)
• (buffers)
31