linked listsanhtt/slidesss/dataalgo/dataalgo-lists.pdf · 3 linked list za linked list is an...

35
Linked Lists Truong Tuan Anh CSE-HCMUT

Upload: others

Post on 16-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

Linked ListsTruong Tuan Anh

CSE-HCMUT

Page 2: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

2

Outline

Basic conceptsList Operations

Page 3: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

3

Linked List

A linked list is an ordered collection of data in which each element contains the location of the next element.

list //Linked Implementation of List

head <pointer>

count <integer> //number of elements (optional)

end list

Page 4: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

4

Linked List: Implementing in C++

Page 5: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

5

Nodes

An element in a linked list is called a nodeA node in a linked list is a structure that has at least two fields:

The dataThe address of the next node

Page 6: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

6

Nodes

Page 7: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

7

Nodes: Implementing in C++

Page 8: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

8

Nodes: Implementing in C++

Page 9: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

9

Nodes: Example

Page 10: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

10

Nodes: Example

Page 11: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

11

Operations

Page 12: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

12

Linked List Operations

Create an empty linked listInsert a node into a linked listDelete a node from a linked listSearch a node from a linked listSort a linked list

Page 13: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

13

Create an Empty Linked List

Page 14: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

14

Create an Empty Linked List

Page 15: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

15

Insert a Node into an Empty Linked List

1. Allocate memory for the new node and set up data2. Locate the pointer p in the list, which will point to the

new node:1. If the new node becomes the first element in the

List: p is list.head2. Otherwise: p is pPre->link, where pPre points

to the predecessor of the new node3. Point the new node to its successor4. Point the pointer p to the new node

Page 16: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

16

Insert a Node into an Empty Linked List

Page 17: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

17

Insert a Node at the Beginning of a List

Page 18: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

18

Insert a Node in the Middle of a List

Page 19: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

19

Insert a Node at the End of a List

Page 20: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

20

Insert a Node into a List

Insertion is successful when allocation memory for the new node is successful

There is no difference between insertion at the beginning of the list and insertion into an empty list

pNew->link = list.Headlist.head = pNew

There is no difference between insertion in the middle and insertion at the end of the list

pNew->link = pPre->linkpPre->link = pNew

Page 21: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

21

Insert a Node into a List

Page 22: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

22

Deletion

Page 23: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

23

Delete a node from a linked list

1. Locate the pointer p in the list which points to the node to be deleted (pLoc will hold the node to be deleted)

1. If that node is the first element in the List: p is list.head2. Otherwise: p is pPre->link, where pPre points to

the predecessor of the node to be deleted2. p points to the successor of the node to be

deleted3. Recycle the memory of the deleted node

Page 24: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

24

Delete First Node

Page 25: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

25

General Deletion Case

Page 26: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

26

Delete a Node from a Linked List

Removal is successful when the node to be deleted is foundThere is no difference between deleting the node from the beginning of the list and deleting the only node in the list

list.head = pLoc->linkrecycle(pLoc)

There is no difference between deleting a node from the middle and deleting a node from the end of the list

pPre->link = pLoc->linkrecycle(pLoc)

Page 27: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

27

Delete a Node from a Linked List

Page 28: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

28

Destroy a Linked List

Delete all nodes in the list→ save memory

How?

Page 29: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

29

Destroy a Linked List

Page 30: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

30

Searching

Page 31: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

31

Searching in a Linked List

Sequence Search has to be used for the linked list

Function Search of List ADT:<ErrorCode> Search (val target

<dataType>,ref pPre <pointer >, ref pLoc <pointer>)

Searches a node and returns a pointer to it if found

Page 32: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

32

Searching in a Linked List

Page 33: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

33

Searching in a Linked List

Page 34: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

34

Other Lists

Doubly Linked List

Circularly Linked List

Page 35: Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an ordered collection of data in which each element contains the location of the next element

35

Takeaways

Basic conceptsList operations