ALGORYTMY I STRUKTURY DANYCH

Transkrypt

ALGORYTMY I STRUKTURY DANYCH
'
$
ALGORYTMY I STRUKTURY DANYCH
wykład 2
elementarne struktury danych
dr hab. inż. Andrzej Obuchowicz, prof. UZ
&
%
'
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
$
1/10
Struktura danych
Struktura danych:
S = (D, R, e),
gdzie
✏ D = {di } — zbiór danych elementarnych;
✏ e ∈ D — identyfikator struktury;
✏ R = {rio =< e, di >} ∪ {rin =< dj , di >}
— rio : relacja wejściowa; rin : relacja wewnetrzna.
˛
&
%
'
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
Słownik
$
2/10
Słownik: Struktura danych, dla której zdefiniowano
nastepuj
˛
ace
˛ algorytmy:
✍
x = find(e,k)
wyszukaj adres x elementu z polem key = k w strukturze
wskazanej przez e;
✍
insert(e,x)
wstaw element wskazany przez x do struktury wskazanej
przez e;
✍
delete(e,x)
usuń element wskazany przez x ze struktury wskazanej
przez e.
&
%
'
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
Zbiór liniowo uporzadkowany
˛
$
3/10
Zbiór liniowo uporzadkowany:
˛
Struktura danych, której
elementy (rekordy) zawieraja˛ pole typu porzadkowego
˛
(tzn. zdefiniowano dla tego typu relacje˛ mniejszości),
według którego można porzadkowa
˛
ć liniowo elementy
zbioru. Można zdefiniować algorytmy:
☞
x = next(e,y) (x = prec(e,y))
˛
acego
˛
po elemencie y
wyszukaj adres x elementu nastepuj
(poprzedzajacego
˛
element y) w strukturze wskazanej
przez e;
☞
x = min(e) (x = max(e))
wyszukaj adres x elementu minimalnego (maksymalnego)
w strukturze wskazanej przez e.
&
%
'
$
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
4/10
Stos (LIFO: last in first out)
push(top,x)
if top<>null
next[x] := top
top := x
pop(top)
if top=null
then return ’niedomiar’
else top := next[top]
&
%
'
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
Kolejka (FIFO: first in first out)
enqueue(head,tail,x)
if tail=null
dequeue(head,tail)
if head=null
then head:=x
then return ’niedomiar’
else next[tail]:=x
else if head=tail
tail:=x
$
5/10
then head:=null
tail:=null
else head:=next[head]
&
%
'
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
&
Listy
$
6/10
%
'
$
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
7/10
Lista jednokierunkowa: operacje
insert_list1(head,x)
if head<>null
then next[x]:=head
head:=x
delete_list1(head,x)
if head=x
then head:=next[head]
else y:=head
while next[y]<>x
x=search_list1(head,k)
x:=head
do y:=next[y]
next[y]:=next[x]
while x<>null & key[x]<>k
do x:=next[x]
return x
&
%
'
$
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
8/10
Lista jednokierunkowa cykliczna: operacje
insert_list1c(head,x)
if head<>null
then next[x]:=next[head]
next[head]:=x
x=search_list1c(head,k)
x:=head
if head<>null
then while next[x]<>head & key[x]<>k
else next[x]:=x
head:=x
do x:=next[x]
if key[x]<>k
then x:=null
delete_list1c(head,x)
return x
if next[x]=x
then head:=null
else y:=head
while next[y]<>x
do y:=next[y]
next[y]:=next[x]
if head=x
then head:=next[head]
&
%
'
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
$
9/10
Lista dwukierunkowa: operacje
insert_list2(head,x)
if head<>null
then next[x]:=head
prev[head]:=x
head:=x
delete_list2(head,x)
if next[x]<>null
then prev[next[x]]:=prev[x]
if prev[x]<>null
then next[prev[x]]:=next[x]
else head:=next[x]
x=search_list2(head,k)
x:=head
while x<>null & key[x]<>k
do x:=next[x]
return x
&
%
'
$
AiSD wykład 2 – dr hab. inż. A. Obuchowicz, prof. UZ
10/10
Lista dwukierunkowa cykliczna: operacje
insert_list2c(head,x)
if head<>null
then next[x]:=next[head]
prev[x]:=head
x=search_list2c(head,k)
x:=head
if head<>null
then while next[x]<>head & key[x]<>k
prev[next[head]]:=x
next[head]:=x
do x:=next[x]
if key[x]<>k
else next[x]:=x
prev[x]:=x
then x:=null
return x
head:=x
delete_list2c(head,x)
if next[x]=x
then head:=null
else prev[next[x]]:=prev[x]
next[prev[x]]:=next[x]
if head=x
then head:=next[head]
&
%

Podobne dokumenty