Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an...

Post on 16-Aug-2020

3 views 0 download

Transcript of Linked Listsanhtt/Slidesss/DataAlgo/DataAlgo-Lists.pdf · 3 Linked List zA linked list is an...

Linked ListsTruong Tuan Anh

CSE-HCMUT

2

Outline

Basic conceptsList Operations

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

4

Linked List: Implementing in C++

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

6

Nodes

7

Nodes: Implementing in C++

8

Nodes: Implementing in C++

9

Nodes: Example

10

Nodes: Example

11

Operations

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

13

Create an Empty Linked List

14

Create an Empty Linked List

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

16

Insert a Node into an Empty Linked List

17

Insert a Node at the Beginning of a List

18

Insert a Node in the Middle of a List

19

Insert a Node at the End of a List

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

21

Insert a Node into a List

22

Deletion

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

24

Delete First Node

25

General Deletion Case

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)

27

Delete a Node from a Linked List

28

Destroy a Linked List

Delete all nodes in the list→ save memory

How?

29

Destroy a Linked List

30

Searching

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

32

Searching in a Linked List

33

Searching in a Linked List

34

Other Lists

Doubly Linked List

Circularly Linked List

35

Takeaways

Basic conceptsList operations