singly-linked list and doubly-linked list (thai)

Click here to load reader

Post on 11-Apr-2015

1.854 views

Category:

Documents

5 download

Embed Size (px)

DESCRIPTION

แนวความคิดเกี่ยวกับ linklist

TRANSCRIPT

Link list(One way list)

Link List-

Singly Linked Lists - Doubly Linked Lists

Link Singly

Link List Info A Link A

Link Singly

Link List Info A Link A INFO LINK

Node A

1EF2 INFO

1EF3

Node A

1EF3

INFO

NULL

Node B

1EF2 INFO

1EF3

Node A

1EF3

INFO

NULL

Node B

1EF2 INFO

INFO

NULL

Node A

Node B

Node - P P INFO(P) P LINK(P) (Address) P Node(P)

P External Pointer

Singly Link ListsP 10 NULL Node A

: P INFO(P) = ? NODE(P)

Singly Link ListsP 10 NULL Node A

: P INFO(P) = 10 NODE(P)

Singly Link ListsP 10 NULL Node A

: P INFO(P) = 10 LINK(P) = ? NODE(P)

Singly Link ListsP 10 NULL Node A

: P INFO(P) = 10 LINK(P) = NULL ^ NODE(P) NODE(P)

Singly Link Lists Lists

L = (5,10,9)

Singly Link Lists Lists5 Node 1

L = (5,10,9)10 Node 2 9 Node 3 ^

Singly Link Lists Lists5 Node 1

L = (5,10,9)10 Node 2 9 Node 3 ^

Lists

Lists External Pointer Lists Head

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = ? NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = ? NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = 2 NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = 2 INFO(LINK(Head)) = ? NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = 2 INFO(LINK(Head)) = 10 NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = 2 INFO(LINK(Head)) = 10 LINK(LINK(Head)) = ? NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = 2 INFO(LINK(Head)) = 10 LINK(LINK(Head)) = 3 NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

= Head INFO(Head) = 5 LINK(Head) = 2 INFO(LINK(Head)) = 10 LINK(LINK(Head)) = 3 INFO(LINK(LINK(Head))) = ? NODE(Head)

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

INFO(LINK(LINK(Head)))

=9

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

INFO(LINK(LINK(Head)))

=9 LINK(LINK(LINK(Head))) = ?

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

INFO(LINK(LINK(Head)))

=9 LINK(LINK(LINK(Head))) = ^

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

Link

Lists Head := LINK(Head); INFO(Head) = 10 LINK(Head) = Node 3

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

Head

Head := LINK(Head); INFO(Head) = 9

LINK(Head) = NULL ^

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

Lists

?

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3 ^

Lists External

Head

Pointer Travel

Travel := Head; Lists Travel Head

Travel := LINK(Travel);

Singly Link ListsHead Travel Node 1 Node 2 Node 3 5 10 9 ^

Lists External

Head

Pointer Travel

Travel := Head; Lists Travel Head

Travel := LINK(Travel);

Singly Link ListsHead Travel Node 1 Node 2 Node 3 5 10 9 ^

Travel

: = Head; While Travel NULL DO begin PRINT INFO(Travel); Travel := LINK(Travel); end;

Singly Link ListsHead 5 Node 1 6 10 Node 2 9 Node 3 ^

newNode

Link Lists

Lists

1. LINK(newNode) := Head;

Singly Link ListsHead 5 Node 1 6 10 Node 2 9 Node 3 ^

newNode

Link Lists

Lists

1. LINK(newNode) := Head; 2. Head := newNode;

Singly Link ListsP Head 5 Node 1 6 ^ 10 Node 2 9 Node 3 ^

newNode

P

Singly Link ListsP Head 5 Node 1 6 10 Node 2 9 Node 3 ^

newNode

P

1. LINK(newNode) := LINK(P);

Singly Link ListsP Head 5 Node 1 6 10 Node 2 9 Node 3 ^

newNode

P

1. LINK(newNode) := LINK(P); 2. LINK(P) := newNode;

Singly Link ListsP Head 5 Node 1 10 Node 2 9 Node 3 ^

Singly Link ListsP Head 5 Node 1 6 ^ 10 Node 2 9 Node 3 ^

newNode

1. LINK(newNode) := NULL;

Singly Link ListsP Head 5 Node 1 6 ^ 10 Node 2 9 Node 3

newNode

1. LINK(newNode) := NULL; 2. LINK(P) := newNode;

Singly Link ListsHead 5 Node 1 10 Node 2 9 Node 3

Lists

Singly Link ListsHead Temp 5 Node 1 10 Node 2 9 Node 3

Lists

1. Temp := Head;

Singly Link ListsHead 5 Temp Node 1 10 Node 2 9 Node 3

Lists

1. Temp := Head; 2. Head := LINK(Temp);

Singly Link ListsP Head 5 Node 1 10 Node 2 9 Node 3

P

Singly Link ListsP Head 5 Node 1 Temp 10 Node 2 9 Node 3

P

1. Temp := LINK(P);

Singly Link ListsP Head 5 Node 1 Temp 10 Node 2 9 Node 3

P

1. Temp := LINK(P); 2. LINK(P) := LINK(Temp);Back

Doubly Linked Lists

Doubly

Link List

RLink Info LLink

LLink

Doubly

Link List

RLink Info LLink

LLink

Info

Doubly

Link List

RLink Info LLink

LLink

Info

RLink

P

Q Doubly

Link List

P

Q Doubly

Link List 1. Llink(Q) = Llink(P)

P

Q Doubly Link List 1. Llink(Q) = Llink(P) 2. Rlink(Llink(P)) = Q

P

Q Doubly

Link List 1. Llink(Q) = Llink(P) 2. Rlink(Llink(P)) = Q 3. Llink(P) = Q

P

Q Doubly 1. 2. 3. 4.

Link List Llink(Q) = Llink(P) Rlink(Llink(P)) = Q Llink(P) = Q Rlink(Q) = P

P

Q Doubly Link List P

P

Q Doubly Link List P 1. Rlink(Q) = Rlink(P)

P

Q Doubly Link List P 1. Rlink(Q) = Rlink(P) 2. Llink(Rlink(P)) = Q

P

Q Doubly Link List P 1. Rlink(Q) = Rlink(P) 2. Llink(Rlink(P)) = Q 3. Rlink(P) = Q

P

Q Doubly Link List P 1. Rlink(Q) = Rlink(P) 2. Llink(Rlink(P)) = Q 3. Rlink(P) = Q 4. Llink(Q) = P

P

Doubly

Link List

P

Doubly

Link List 1. Rlink(Llink(P)) = Rlink(P)

P

Doubly

Link List 1. Rlink(Llink(P)) = Rlink(P) 2. Llink(Rlink(P)) = Llink(P)

P

Doubly

Link List 1. Rlink(Llink(P)) = Rlink(P) 2. Llink(Rlink(P)) = Llink(P) 3. P Storage Pool

FUNCTION locate(L : pointer : x: integer) : pointer; VAR trav.sentinel: pointer; BEGIN NEW(sentinel); sentinel^.info := x; sentinel^.link := NULL; last^.link := sentinel; trav := L; WHILE trav^.info x DO trav := trav^.link; locate := trav; END;