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 1Luong 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 2Luong 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 3Luong 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 4Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Contents
Trang 5Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Linear list concepts
Trang 6Luong 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 7Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Linear list concepts
Trang 8Luong 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 9Luong 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 10Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 11Luong 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 12Luong 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 13Luong 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 14Luong 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 15Luong 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 16Luong 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 17Luong 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 18Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Array implementation
Trang 19Luong 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 20Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamically Allocated Array
Trang 21Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 22Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 23Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 24Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 25Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 26Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 27Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Implementation in C++
Trang 28Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Dynamic Array: Using
Trang 29Luong 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 30Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Singly linked list
Trang 31Luong 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 32Luong 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 33Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 34Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Example
Trang 35Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Example
Hình: List representing polynomial
Trang 36Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 37Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 38Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 39Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 40Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Trang 41Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Linked list implementation in C++
Trang 42Luong 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 43Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Create an empty linked list
Trang 44Luong 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 45Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Create an empty linked list
Trang 46Luong 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 47Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Insert into an empty linked list
Trang 48Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Insert at the beginning
Trang 49Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Insert in the middle
Trang 50Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Insert at the end
Trang 51Luong 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 52Luong 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 53Luong 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 54Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Insert a node into a linked list
Trang 55Luong 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 56Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Delete first node
Trang 57Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
General deletion case
Trang 58Luong 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 59Luong 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 60Luong 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 61Luong The Nhan,Tran Giang Son
Linear list conceptsArray
implementationSingly linked listOther linked listsComparison ofimplementations oflist
Delete a node from a linked list