4 linked list
This presentation is the property of its rightful owner.
Sponsored Links
1 / 77

บทที่ 4 ลิงก์ลิสต์ ( Linked List ) PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on
  • Presentation posted in: General

บทที่ 4 ลิงก์ลิสต์ ( Linked List ). By Juthawut Chantharamalee.

Download Presentation

บทที่ 4 ลิงก์ลิสต์ ( Linked List )

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


4 linked list

4 (Linked List)

By Juthawut Chantharamalee

(Data Structure) 4122202


4 linked list

(Lists)

(Data Structure) 4122202


4 linked list

4.1

(Data Structure) 4122202


Linear list concepts

(Linear List Concepts)

4.2 n+1 n

(Data Structure) 4122201


4 linked list

Element 1

Element 2

Element 3

Element 3

4.1

(Data Structure) 4122202


2 general list

2 (General List)

(Random List) (Order List)

(Data Structure) 4122202


Restricted list

(Restricted List)

FIFO LIFO FIFO LIFO

(Data Structure) 4122202


4 linked list

4.3

(Data Structure) 4122202


Basic operations

(Basic Operations)

(Insertion) (Delete) (Retrieval) (Traversal)

(Data Structure) 4122202


Insertion

(Insertion)

(Ordered Lists) 4.4

(Data Structure) 4122202


4 linked list

10

20

25

25

insertion

Insertion data

list

10

20

25

30

4.4

(Data Structure) 4122202


Deletion

(Deletion)

4.5

(Data Structure) 4122202


4 linked list

Delete element identified by search

blue

green

red

yellow

Deletion

red

Data

list

blue

green

yellow

4.5

(Data Structure) 4122202


Retrieval

(Retrieval)

4.6

(Data Structure) 4122202


4 linked list

cat

dog

tiger

monkey

Retrieved element identified by search

Retrieval

dog

list

cat

dog

tiger

monkey

4.6

(Data Structure) 4122202


Traversal

(Traversal)

(Data Structure) 4122202


Linked list concepts

(Linked List Concepts)

4.7

(Data Structure) 4122202


4 linked list

(a) Array

Head

(b) Liked List

4.7

(Data Structure) 4122202


4 linked list

(Data)

(Link)

(Single-Linked List)

(Data Structure) 4122202


4 linked list

5000

A

5001

7800

6500

D

6501

-1

7698

C

7699

6500

7800

B

7801

7698

8000

5000

4.8

(Data Structure) 4122202


4 linked list

pHead

data

link

data

link

data

link

data

link

(a)

(b) null

4.9 (Linked List)

(Data Structure) 4122202


4 linked list

2 4.10 3 number 3 id, name, gpa

(Data Structure) 4122202


4 linked list

number

id

name

gpa

(a)

(b) 3

name

addr

phone

(c)

4.10

(Data Structure) 4122202


Linked list data structure

(Linked List Data Structure)

(Head Node Structure)

Create List Metadata 4.11 (a) Metadata count

(Data Structure) 4122202


4 linked list

list

count <integer>

head <pointer>

end list

count

head

(a) (Head Structure)

node

data <data type>

link <pointer>

end node

data

link

(b) (Data Node Structure)

4.11

(Data Structure) 4122202


4 linked list

(Data Node Structure)

4.12 (b) (Data Type) Key Field

data

key

field1

field2

..

fieldN

end data

(Data Structure) 4122201


4 linked list

1. (phead) null 2. Data Link - Data - Link 3.

(Data Structure) 4122202


4 linked list

4. 5. null

null

list

10

4

10

20

20

40

20

30

40

count

head

(b)

(a)

4.12

(Data Structure) 4122202


4 linked list

4.12 4.13

(Data Structure) 4122202


4 linked list

1. 2. 3.

(Data Structure) 4122202


4 linked list

4

tom

sam

jim

sara

count

head

insert

bob

5

tom

sam

jim

sara

count

head

insert

june

6

tom

sam

jim

bob

sara

count

head

Delete

5

june

tom

jim

bob

sara

sam

count

head

Delete

6

tom

sam

jim

bob

sara

count

head

4.13

(Data Structure) 4122202


Linked list algorithm

(Linked List Algorithm)

10 Create List, Insert Node, Delete Node, Search List, Retrieve List, Empty List, Full List, List Count, Traverse List, Destroy List

(Data Structure) 4122202


4 linked list

(Create List)

Create List Metadata Metadata 2 4.14 null 4.1

(Data Structure) 4122202


4 linked list

allocate (list)

set list head to null

set list count to 0

list

0

count

head

4.14

Algorithm createList (List)

Initializes metadata for list

Pre list is metadata structure passed by reference

Post metadata initialized

1 allocate (list)

2 set list head to null

3 set list count to 0

end createList

4.1 (Create List)

(Data Structure) 4122202


4 linked list

(Insert Node )

(Predecessor) 1. 2. 3.

(Data Structure) 4122202


4 linked list

2 null

4 1. 2. 3. 4.

(Data Structure) 4122202


4 linked list

(Insert into Empty List )

null (pNew)

set pNew link to list head (Null pointer)

set list head to pNew(First node)

(Data Structure) 4122202


4 linked list

4.15

(a) Before add

list

0

75

count

head

pNew

set pNew link to list head

set list head to pNew

list

1

75

count

head

pNew

(b) After add

4.15

(Data Structure) 4122202


4 linked list

(Insert at Beginning)

Predecessor null

set pNew link to list head (To current first node)

set list head to pNew(To new first node)

(Data Structure) 4122202


4 linked list

4.18

(a) Before add

list

3

39

52

75

count

head

134

pPre

pNew

set pNew link to pPre link

set pPre link to pNew

list

4

39

52

75

count

head

134

pPre

pNew

(b) After add

4.18

(Data Structure) 4122202


4 linked list

  • Algorithm insertNode (list, pPre, dataIn)

  • Inserts data into a new node in the list

  • Prelist is matadata structure to a valid list.

  • pPre is pointer to datas logical predecessor

  • dataIn contains data to be inserted

  • Postdata have been inserted in sequence

  • Return true if successful. False if memory overflow

  • allocate (pNew)

  • set Pnew data to dataIn

  • if (pPre null)

  • Adding before first node or to empty list.

  • 1 set pNew link to list head

  • 2 set list head to pNew

  • else

  • Adding in middle or end.

  • 1 set pNew link to pPre link

  • 2 set pPre link to pNew

  • 5end if

  • 6Return true

  • end insertNode

4.2 (Insert List Node)

(Data Structure) 4122202


4 linked list

(Insert at End)

Predecessor Successor null

set pNew link to null pointer

set pPre link to pNew(Predecessor to new)

(Data Structure) 4122202


4 linked list

(Delete Node )

(pLoc) Predecessor (pPro) Successor

(Data Structure) 4122202


4 linked list

(Delete First Node )

(pLoc) Successor Predecessor (pPre) pPre null

(Data Structure) 4122202


4 linked list

4.19

(a) Before delete

list

3

39

75

134

count

head

pPre

pLoc

set list head to pLoc link

Recycle (pLoc)

list

2

Recycle

75

134

count

head

pPre

pLoc

(b) After Delete

4.19

(Data Structure) 4122202


4 linked list

(General Delete Node )

Predecessor Successor pLoc 4.20

(Data Structure) 4122202


4 linked list

(a) Before delete

list

3

39

75

134

count

head

pPre

pLoc

set pPre link to pLoc link

list

2

39

Recycle

134

count

head

pPre

pLoc

(b) After Delete

4.20

(Data Structure) 4122202


4 linked list

  • Algorithm DeleteNode (list, pPre, pLoc, dataOut)

  • Deletes data from list & returns it to calling module.

  • Pre list is matadata structure to a valid list.

  • pPre is pointer to predecessor node

  • pLoc is pointer to node to be deleted

  • dataOut is variable to receive deleted data

  • Post data have been deleted and return caller

  • move pLoc data to dataOut

  • if (pPre null)

  • Deleting first node

  • 1 set list head to pLoc link

  • 3else

  • Deleting other nodes

  • 1 set pPre link to pLoc link

  • end if

  • recycle (pLoc)

  • end DeleteNode

4.3 (Delete Node)

(Data Structure) 4122202


4 linked list

(Search List )

- - - Sequential Search Ordered List Search

(Data Structure) 4122202


4 linked list

data

Key

field1

field2

.

.

fieldN

end data

(Data Structure) 4122202


4 linked list

Target Key Target Key 4.21

(Data Structure) 4122202


4 linked list

(Condition)

(pPre)

(PLoc)

(Return)

Target < first node

Target = first node

First < Target < last

Target = middle node

Target = last node

Target > last node

Null

Null

Largest node < target

Nodes predecessor

Lasts predecessor

Last node

First node

First node

First node > target

Equal node

Last node

Null

False

True

False

True

True

False

4.21

(Data Structure) 4122202


4 linked list

Located first

Located middle

Located last

target

target

target

. . .

. . .

5

10

15

20

95

100

pPre

pLoc

pPre

pLoc

pPre

pLoc

(a) (Returntrue)

Located first

Located middle

Located last

Target > 5

Target < 20

Target < 5

Target > 100

. . .

. . .

5

10

15

20

95

100

pPre

pLoc

pPre

pLoc

pPre

pLoc

(b) (Returnfalse)

4.20

(Data Structure) 4122202


4 linked list

4.4 (Search List)

(Data Structure) 4122202


4 linked list

(Retrieve List )

4.5

(Data Structure) 4122202


4 linked list

  • AlgorithmRetrieveNode (list, key, dataOut)

  • Return Boolean indicating whether the list is empty.

  • Prelist is matadata structure to a valid list

  • key is target of data to be retrieved

  • dataOut is variable to receive retrieve data

  • Post data placed in dataOut or error returned if not found

  • Return true if successful, false if data not found

  • set found to searchList (list, pPre, pLoc, key)

  • if (found)

  • 1 move pLoc data to dataOut

  • 3 end if

  • 4 return found

  • end RetrieveNode

4.5 (Retrieve Node)

(Data Structure) 4122202


4 linked list

(Empty List )

4.6

(Data Structure) 4122202


4 linked list

  • AlgorithmemptyList (list)

  • Return Boolean indicating whether the list is empty.

  • Pre list is matadata structure to a valid list

  • Return true if list empty, false if list contains data

  • if (list count equal 0)

  • 1 return true

  • else

  • 1 return false

  • endemptyList

4.6 (Empty List)

(Data Structure) 4122202


4 linked list

(Full List )

C 4.7

(Data Structure) 4122202


4 linked list

  • AlgorithmfullList (list)

  • Return Boolean indicating whether the list is empty.

  • Pre list is matadata structure to a valid list

  • Return false if room for new node, true if memory full

  • if (memory full)

  • 1 return true

  • else

  • 1 return false

  • end if

  • return true

  • endfullList

4.7 (Full List)

(Data Structure) 4122202


4 linked list

(List Count)

List Count 4.8

(Data Structure) 4122202


4 linked list

Algorithm listCount (list)

Return integer representing number of nodes in list

Pre list is matadata structure to a valid list.

Return count for number of nodes in list

1return (list count)

end listCount

4.8 (ListCount)

(Data Structure) 4122202


4 linked list

(Traverse Count)

1. 2. 3. 4.

(Data Structure) 4122202


4 linked list

2 1. 2.

(Data Structure) 4122202


4 linked list

count

pos

head

list

N

. . .

5

10

15

20

95

100

4.23 (List traversal)

pHead

id

name

score

001

Jan

78

002

June

81

. . .

002

John

81

050

Joy

70

4.24

(Data Structure) 4122202


4 linked list

4.9 (Traverse List)

(Data Structure) 4122202


4 linked list

(Destroy List)

Destroy List 4.10

(Data Structure) 4122202


4 linked list

  • Algorithm destroyList (list)

  • Deletes all data in list

  • Pre list is metadata structure to a valid list.

  • Post all data deleted

  • Loop (not at end of list)

  • 1 set list head to successor node

  • 2 release memory to heap

  • End loop

  • No data left in list. Reset metadata.

  • set list pos null

  • set list count to 0

  • end destroyList

4.10 (DestroyList)

(Data Structure) 4122202


Other linked lists

(Other Linked Lists)

(Single-Linked List)

(Data Structure) 4122202


4 linked list

(Circular-Linked List)

4.25

list

N

5

10

95

count

rear

link

link

link

link

4.25 Circular-Linked List

(Data Structure) 4122202


4 linked list

(Double-Linked List)

2 (Successor) (Predecessor) 4.26

B

F

B

F

. . .

list

count

rear

head

5

95

B = Backward pointer

F = Forward pointer

4.26 Double-Linked List

(Data Structure) 4122202


4 linked list

4.27 4.28 4.11 4.12

(Data Structure) 4122202


4 linked list

list

0

count

rear

head

list

1

20

count

rear

head

B

F

pPre

pNew

20

B

F

Before

After

(a)

list

2

20

40

count

rear

head

B

F

B

F

pNew

30

pPre

B

F

pSucc

Before

3

20

30

40

list

count

rear

head

B

F

B

F

B

F

After

(b)

4.27

(Data Structure) 4122202


4 linked list

list

3

25

50

75

count

rear

head

B

F

B

F

B

F

pPre

pDlt

pSucc

(a) Before delete

list

2

25

(Recycled)

75

count

rear

head

B

F

B

F

(b) After deleting 50

4.28

(Data Structure) 4122202


4 linked list

4.11

(Data Structure) 4122202


4 linked list

4.12

(Data Structure) 4122202


4 linked list

The End

Lesson 4

(Data Structure) 4122201


  • Login