1. Trang chủ
  2. » Tất cả

Cấu trúc dữ liệu và thuật toán dsa ch5 lists

61 3 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Lists (cont) - Linked List
Tác giả Dr. Nguyen Ho Man Rang
Trường học University of Technology, VNU-HCM
Chuyên ngành Data Structures and Algorithms
Thể loại Bài giảng
Định dạng
Số trang 61
Dung lượng 1,09 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Nguyen HoMan Rang Singly linked listOther linked listsComparison ofimplementations oflist Create an empty linked list Algorithm createListref list Initializes metadata for a linked list

Trang 1

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Chapter 5

Lists (cont) - Linked List

Data Structures and Algorithms

Dr Nguyen Ho Man Rang Faculty of Computer Science and Engineering

University of Technology, VNU-HCM

Trang 2

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Outcomes

• L.O.2.1 - Depict the following concepts: (a) array list

and linked list, including single link and double links,

and multiple links.

• L.O.2.2 - Describe storage structures by using

pseudocode for: (a) array list and linked list, including

single link and double links, and multiple links.

• L.O.2.3 - List necessary methods supplied for list and

describe them using pseudocode.

• L.O.2.4 - Implement list using C/C++.

Trang 3

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Outcomes

• L.O.2.5 - Use list for problems in real-life, and choose

an appropriate implementation type (array vs link).

• L.O.2.6 - Analyze the complexity and develop

experiment (program) to evaluate the efficiency of

methods supplied for list.

• L.O.8.4 - Develop recursive implementations for

methods supplied for the following structures: list.

• L.O.1.2 - Analyze algorithms and use Big-O notation to

characterize the computational complexity of algorithms

composed by using the following control structures:

sequence, branching, and iteration (not recursion).

Trang 4

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Contents

1 Singly linked list

2 Other linked lists

3 Comparison of implementations of list

Trang 5

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Singly linked list

Trang 6

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Trang 7

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Nodes

The elements in a linked list are called nodes

A node in a linked list is a structure that has at least two

fields:

• the data,

• the address of the next node.

Trang 8

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

f i e l d 1 < >

f i e l d 2 < >

f i e l d n < >

Trang 9

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Example

Trang 10

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Example

Hình: List representing polynomial

Trang 11

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Trang 12

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Trang 13

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Trang 14

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked list implementation in C++

Trang 15

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked list operations

Trang 16

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Create an empty linked list

Trang 17

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Create an empty linked list

Algorithm createList(ref list <metadata>)

Initializes metadata for a linked list

Pre: list is a metadata structure passed

Trang 18

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Create an empty linked list

Trang 19

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked list

and set up data.

will point to the new node:

pPre points to the predecessor of the

new node.

Trang 20

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert into an empty linked list

Trang 21

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert at the beginning

Trang 22

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert in the middle

Trang 23

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert at the end

Trang 24

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked 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 −> l i n k = l i s t head

l i s t head = pNew

• There is no difference between insertion in the middle

and insertion at the end of the list.

pNew −> l i n k = pPre −> l i n k

pPre −> l i n k = pNew

Trang 25

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked list

Algorithm insertNode(ref list <metadata>,

val pPre <node pointer>, val dataIn <dataType>) Inserts data into a new node in the linked list.

Pre: list is metadata structure to a valid list

pPre is pointer to data’s logical predecessor

dataIn contains data to be inserted

Post: data have been inserted in sequence

Return true if successful, false if memory overflow

Trang 26

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked list

allocate(pNew)

if memory overflow then

return false

end

pNew -> data = dataIn

if pPre = null then

// Adding at the beginning or into empty list

pNew -> link = list.head

list.head = pNew

else

// Adding in the middle or at the end

pNew -> link = pPre -> link

pPre -> link = pNew

end

list.count = list.count + 1

Trang 27

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked list

Trang 28

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

hold the node to be deleted).

pPre points to the predecessor of the

Trang 29

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Delete first node

Trang 30

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

General deletion case

Trang 31

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

• Removal is successful when the node to be deleted is

found.

• There is no difference between deleting the node from

the beginning of the list and deleting the only node in

the list.

l i s t head = pLoc −> l i n k

r e c y c l e ( pLoc )

• There is no difference between deleting a node from the

middle and deleting a node from the end of the list.

pPre −> l i n k = pLoc −> l i n k

Trang 32

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

Algorithm deleteNode(ref list <metadata>,

val pPre <node pointer>, val pLoc <node pointer>, ref dataOut <dataType>) Deletes data from a linked list and returns it to

calling module.

Pre: list is metadata structure to a valid list

pPre is a pointer to predecessor node

pLoc is a pointer to node to be deleted

dataOut is variable to receive deleted data

Post: data have been deleted and returned to caller

Trang 33

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

dataOut = pLoc -> data

if pPre = null then

// Delete first node

list.head = pLoc -> link

else

// Delete other nodes

pPre -> link = pLoc -> link

Trang 34

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

Trang 35

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Searching in a linked list

linked list.

< E r r o r C o d e > S e a r c h ( val t a r g e t < d a t a T y p e > ,

ref p P r e < pointer > , ref p L o c < pointer >)

Searches a node and returns a pointer to it

if found.

Trang 36

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Searching in a linked list

Trang 37

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Searching in a linked list

Algorithm Search(val target <dataType>,

ref pPre <node pointer>, ref pLoc <node pointer>) Searches a node in a singly linked list and return a

pointer to it if found.

Pre: target is the value need to be found

Post: pLoc points to the first node which is equal

target, or is NULL if not found.

pPre points to the predecessor of the first node

which is equal target, or points to the last node if

not found.

Trang 38

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Searching in a linked list

pPre = NULL

pLoc = list.head

while (pLoc is not NULL) AND

(target != pLoc ->data) do

Trang 39

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Searching in a linked list

Trang 40

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Traverse a linked list

Traverse module controls the loop: calling a

user-supplied algorithm to process data

Algorithm Traverse(ref <void> process ( ref Data

<DataType>) )

Traverses the list, performing the given operation on

each element.

Pre: process is user-supplied

Post: The action specified by process has been

performed on every element in the list, beginning at

the first element and doing each in turn.

pWalker = list.head

while pWalker not null do

process( pWalker -> data)

Trang 41

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Traverse a linked list

Trang 42

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Destroy a linked list

Algorithm destroyList (val list <metadata>)

Deletes all data in list.

Pre: list is metadata structure to a valid list

Post: all data deleted

while list.head not null do

Trang 43

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Destroy a linked list

Trang 44

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked list implementation in C++

i n t D e l e t e N o d e ( Node ∗ pPre , Node ∗ pLoc ) ;

i n t S e a r c h ( i n t v a l u e , Node ∗ & pPre , Node ∗ & pLoc ) ;

} ;

Trang 45

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked list implementation in C++

Trang 46

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked list implementation in C++

How to use Linked List data structure?

Trang 47

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked list implementation in C++

How to use Linked List data structure?

Trang 48

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Sample Solution: Insert

Trang 49

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Sample Solution: Insert

Trang 50

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Sample Solution: Delete

Trang 51

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Sample Solution: Clone

Trang 52

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Reverse a linked list

Exercise

//

}

Trang 53

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Other linked lists

Trang 54

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Doubly Linked List

Hình: Doubly Linked List allows going forward and backward.

Trang 55

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Doubly Linked List

Hình: Doubly Linked List allows going forward and backward.

Trang 56

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Circularly Linked List

Trang 57

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Double circularly Linked List

Trang 58

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Comparison of

implementations of list

Trang 59

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Arrays: Pros and Cons

we can compute its location quickly.

element, we have to shift subsequent

elements which slows our computation

down.

memory to hold our array.

Trang 60

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Linked Lists: Pros and Cons

require us to move/shift subsequent

data elements.

we need to traverse the list from the

head of the list to find it which can

take longer than an array access.

Trang 61

Dr Nguyen HoMan Rang

Singly linked listOther linked listsComparison ofimplementations oflist

Comparison of implementations of list

• Contiguous storage is generally preferable when:

• the entries are individually very small;

• the size of the list is known when the program is written;

• few insertions or deletions need to be made except at

the end of the list; and

• random access is important.

• Linked storage proves superior when:

• the entries are large;

• the size of the list is not known in advance; and

• flexibility is needed in inserting, deleting, and

rearranging the entries.

Ngày đăng: 25/03/2023, 07:21

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm