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

Data structure and algorithms cấu trúc dữ liệu và thuật toán dsa ch4 lists 1

100 10 0

Đ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
Tác giả Luong The Nhan, Tran Giang Son
Trường học University of Technology, VNU-HCM
Chuyên ngành Data Structures and Algorithms
Thể loại Lecture notes
Thành phố Ho Chi Minh City
Định dạng
Số trang 100
Dung lượng 2,26 MB

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

Nội dung

Luong The Nhan,Tran Giang SonLinear list conceptsArray implementationSingly linked listOther linked listsComparison ofimplementations oflist Outcomes • L.O.2.1 - Depict the following con

Trang 1

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Chapter 4

Lists

Data Structures and Algorithms

Luong The Nhan, Tran Giang Son Faculty of Computer Science and Engineering

University of Technology, VNU-HCM

Trang 2

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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; (b) stack; and (c) queue and circular

queue.

• 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; (b)

stack; and (c) queue and circular queue.

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

stack, and queue, and describe them using pseudocode.

• L.O.2.4 - Implement list, stack, and queue using

C/C++.

Trang 3

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Outcomes

• L.O.2.5 - Use list, stack, and queue 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, stack, and queue.

• L.O.8.4 - Develop recursive implementations for

methods supplied for the following structures: list, tree,

heap, searching, and graphs.

• 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

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Contents

Trang 5

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linear list concepts

Trang 6

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linear list concepts

Definition

A linear list is a data structure in which each

element has a unique successor.

Example

• Linked list

Trang 7

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linear list concepts

Trang 8

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linear list concepts

General list:

used on the list.

Trang 9

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linear list concepts

Restricted list:

list.

ends of the list.

Trang 10

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 11

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

List ADT

Extended operations:

• Find the size of the list.

Trang 12

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insertion

position p in the list

Any element formerly at position p and all later

Trang 13

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insertion

made at any position in the list (at the

beginning, in the middle, at the end).

be inserted so that the ordering of the

list is maintained (searching appropriate

position is needed).

definition (FIFO or LIFO).

Trang 14

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Removal

position p in the list

General Ordered List.

The element at position p is removed from the

Trang 15

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Retrieval

position p in the list

General Ordered List.

Trang 16

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Removal, Retrieval

given data

General Ordered List: Searching is

needed in order to locate the data being

deleted/ retrieved.

Trang 17

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Success of Basic Operations

full.

the list is not empty.

Trang 18

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Array implementation

Trang 19

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Automatically Allocated Array

Hình: Array with pre-defined maxsize and has n elements

Trang 20

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamically Allocated Array

Trang 21

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 22

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 23

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 24

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 25

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 26

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 27

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Implementation in C++

Trang 28

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Dynamic Array: Using

Trang 29

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Contiguous Implementation of List

In processing a contiguous list with n elements:

Trang 30

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Singly linked list

Trang 31

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linked List

Definition

which each element contains the location of

the next element.

Hình: Singly Linked List

l i s t // L i n k e d I m p l e m e n t a t i o n of L i s t

h e a d < pointer >

Trang 32

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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 33

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 34

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Example

Trang 35

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Example

Hình: List representing polynomial

Trang 36

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 37

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 38

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 39

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 40

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Trang 41

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linked list implementation in C++

Trang 42

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Linked list operations

• Create an empty linked list

• Insert a node into a linked list

• Delete a node from a linked list

• Traverse a linked list

• Destroy a linked list

Trang 43

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Create an empty linked list

Trang 44

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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 by

Trang 45

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Create an empty linked list

Trang 46

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked list

1 Allocate memory for the new node

and set up data.

2 Locate the pointer p in the list , which

will point to the new node:

pPre points to the predecessor of the

new node.

3 Point the new node to its successor.

Trang 47

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insert into an empty linked list

Trang 48

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insert at the beginning

Trang 49

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insert in the middle

Trang 50

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insert at the end

Trang 51

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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 52

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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 data’s logical predecessor

dataIn contains data to be inserted

Post: data have been inserted in sequence

Return true if successful, false if memory

Trang 53

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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

return true

Trang 54

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Insert a node into a linked list

Trang 55

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

1 Locate the pointer p in the list which

points to the node to be deleted ( pDel

will hold the node to be deleted).

• If that node is the first element in the

pPre points to the predecessor of the

Trang 56

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Delete first node

Trang 57

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

General deletion case

Trang 58

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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 removal the node from

node in the list.

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

R e c y c l e p D e l

• There is no difference between removal a node from the

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

Trang 59

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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>) Delete 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

Trang 60

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly 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 61

Luong The Nhan,Tran Giang Son

Linear list conceptsArray

implementationSingly linked listOther linked listsComparison ofimplementations oflist

Delete a node from a linked list

Ngày đăng: 25/03/2023, 06:13

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

TÀI LIỆU LIÊN QUAN

w