0% found this document useful (0 votes)
54 views89 pages

List

The document discusses lists and their implementations using arrays and linked lists. It covers linear list concepts, the list abstract data type (ADT) including basic and extended operations, and array implementation using a dynamically allocated array in C++. Specific topics covered include insertion, removal, retrieval operations on lists, ensuring capacity of dynamic arrays, and complexity analysis of list operations.

Uploaded by

Hoàn Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views89 pages

List

The document discusses lists and their implementations using arrays and linked lists. It covers linear list concepts, the list abstract data type (ADT) including basic and extended operations, and array implementation using a dynamically allocated array in C++. Specific topics covered include insertion, removal, retrieval operations on lists, ensuring capacity of dynamic arrays, and complexity analysis of list operations.

Uploaded by

Hoàn Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

Data Structure and Algorithms [CO2003]

Chapter 4 - List

Lecturer: Duc Dung Nguyen, PhD.


Contact: [email protected]

Faculty of Computer Science and Engineering


Hochiminh city University of Technology
Contents

1. Linear list concepts

2. Array implementation

3. Singly linked list

4. Other linked lists

5. Comparison of implementations of list

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 1 / 83
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++.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 2 / 83
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).

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 83
Linear list concepts
Linear list concepts

Definition
A linear list is a data structure in which each element has a unique successor.

Example
• Array
• Linked list

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 4 / 83
Linear list concepts

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 5 / 83
Linear list concepts

General list:
• No restrictions on which operation can be used on the list.
• No restrictions on where data can be inserted/deleted.

• Unordered list (random list): Data are not in particular order.


• Ordered list: data are arranged according to a key.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 6 / 83
Linear list concepts

Restricted list:
• Only some operations can be used on the list.
• Data can be inserted/deleted only at the ends of the list.

• Queue: FIFO (First-In-First-Out).


• Stack: LIFO (Last-In-First-Out).

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 7 / 83
List ADT

Definition
A list of elements of type T is a finite sequence of elements of T.

Basic operations:
• Construct a list, leaving it empty.
• Insert an element.
• Remove an element.
• Search an element.
• Retrieve an element.
• Traverse the list, performing a given operation on each element.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 8 / 83
List ADT

Extended operations:
• Determine whether the list is empty or not.
• Determine whether the list is full or not.
• Find the size of the list.
• Clear the list to make it empty.
• Replace an element with another element.
• Merge two ordered list.
• Append an unordered list to another.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 83
Insertion

• Insert an element at a specified position p in the list

• Only with General Unordered List.

Any element formerly at position p and all later have their position numbers increased by 1.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 83
Insertion

• Insert an element with a given data


• With General Unordered List: can be made at any position in the list (at the beginning, in
the middle, at the end).
• With General Ordered List: data must be inserted so that the ordering of the list is
maintained (searching appropriate position is needed).
• With Restricted List: depend on it own definition (FIFO or LIFO).

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 83
Removal

• Remove an element at a specified position p in the list


• With General Unordered List and General Ordered List.

The element at position p is removed from the list, and all subsequent elements have their
position numbers decreased by 1.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 83
Retrieval

• Retrieve an element at a specified position p in the list


• With General Unordered List and General Ordered List.

All elements remain unchanged.


Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 83
Removal, Retrieval

• Remove/ Retrieve an element with a given data


• With General Unordered List and General Ordered List: Searching is needed in order to
locate the data being deleted/ retrieved.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 14 / 83
Success of Basic Operations

• Insertion is successful when the list is not full.

• Removal, Retrieval are successful when the list is not empty.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 15 / 83
Array implementation
Dynamically Allocated Array

L i s t // C o n t i g u o u s I m p l e m e n t a t i o n o f L i s t
// number o f u s e d e l e m e n t s ( m a n d a t o r y )
s i z e <i n t e g e r >

// ( D y n a m i c a l l y A l l o c a t e d A r r a y )
d a t a <d y n a m i c a r r a y o f <DataType> >

c a p a c i t y <i n t e g e r >
End L i s t

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 16 / 83
Dynamic Array: Implementation in C++

c l a s s DynamicArray {
private :
int size ;
int capacity ;
int ∗storage ;

public :
DynamicArray ( ) {
capacity = 10;
size = 0;
s t o r a g e = new i n t [ c a p a c i t y ] ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 17 / 83
Dynamic Array: Implementation in C++

DynamicArray ( i n t c a p a c i t y ) {
t h i s −>c a p a c i t y = c a p a c i t y ;
size = 0;
s t o r a g e = new i n t [ c a p a c i t y ] ;
}

~DynamicArray ( ) {
delete [ ] storage ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 18 / 83
Dynamic Array: Implementation in C++

void setCapacity ( int );


void ensureCapacity ( int );
void pack ( ) ;
void trim ( ) ;

void rangeCheck ( i n t ) ;
void set ( int , int ) ;
int get ( i n t ) ;
void removeAt ( i n t ) ;
void insertAt ( int , int ) ;

void print ();


};

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 19 / 83
Dynamic Array: Implementation in C++

v o i d DynamicArray : : s e t C a p a c i t y ( i n t newCapacity ) {
i n t ∗ n e w S t o r a g e = new i n t [ n e w C a p a c i t y ] ;
memcpy ( n e w S t o r a g e , s t o r a g e ,
sizeof ( int ) ∗ size );
c a p a c i t y = newCapacity ;
delete [ ] storage ;
s t o r a g e = newStorage ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 20 / 83
Dynamic Array: Implementation in C++

v o i d DynamicArray : : e n s u r e C a p a c i t y ( i n t minCapacity ) {
i f ( minCapacity > capacity ) {
i n t newCapacity = ( c a p a c i t y ∗3)/2 + 1 ;
i f ( newCapacity < minCapacity )
newCapacity = minCapacity ;
s e t C a p a c i t y ( newCapacity ) ;
}
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 83
Dynamic Array: Implementation in C++

v o i d DynamicArray : : pack ( ) {
i f ( s i z e <= c a p a c i t y / 2 ) {
i n t newCapacity = ( s i z e ∗ 3) / 2 + 1 ;
s e t C a p a c i t y ( newCapacity ) ;
}
}

v o i d DynamicArray : : t r i m ( ) {
i n t newCapacity = s i z e ;
s e t C a p a c i t y ( newCapacity ) ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 22 / 83
Dynamic Array: Implementation in C++

v o i d DynamicArray : : rangeCheck ( i n t i n d e x ) {
i f ( i n d e x < 0 | | i n d e x >= s i z e )
throw " I n d e x ␣ out ␣ o f ␣ bounds ! " ;
}

v o i d DynamicArray : : s e t ( i n t index , int value ) {


rangeCheck ( index ) ;
storage [ index ] = value ;
}

i n t DynamicArray : : get ( i n t index ) {


rangeCheck ( index ) ;
return storage [ index ] ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 83
Dynamic Array: Implementation in C++

v o i d DynamicArray : : i n s e r t A t ( i n t index , int value ) {


i f ( index < 0 | | index > s i z e )
throw " I n d e x ␣ out ␣ o f ␣ bounds ! " ;

ensureCapacity ( size + 1);

i n t moveCount = s i z e − i n d e x ;
i f ( moveCount != 0 )
memmove ( s t o r a g e + i n d e x + 1 ,
storage + index ,
s i z e o f ( i n t ) ∗ moveCount ) ;
storage [ index ] = value ;
s i z e ++;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 83
Dynamic Array: Implementation in C++

v o i d D y n a m i c A r r a y : : removeAt ( i n t i n d e x ) {
rangeCheck ( index ) ;
i n t moveCount = s i z e − i n d e x − 1 ;
i f ( moveCount > 0 )
memmove ( s t o r a g e + i n d e x ,
storage + ( index + 1) ,
s i z e o f ( i n t ) ∗ moveCount ) ;
s i z e −−;
pack ( ) ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 25 / 83
Dynamic Array: Using

v o i d DynamicArray : : p r i n t ( ) {
f o r ( i n t i =0; i <t h i s −>s i z e ; i ++) {
c o u t << s t o r a g e [ i ] << " ␣ " ;
}
}

i n t main ( ) {
c o u t << " Dynamic ␣ A r r a y " << e n d l ;
D y n a m i c A r r a y ∗ da = new D y n a m i c A r r a y ( 1 0 ) ;
da−>i n s e r t A t ( 0 , 5 5 ) ;
// . . .
da−>p r i n t ( ) ;
return 0;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 26 / 83
Contiguous Implementation of List

In processing a contiguous list with n elements:

• Insert and Remove operate in time approximately proportional to n (require physical


shifting).

• Clear, Empty, Full, Size, Replace, and Retrieve in constant time.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 27 / 83
Singly linked list
Linked List

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

Figure 1: 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 o f L i s t
head <p o i n t e r >
c o u n t < i n t e g e r > // number o f e l e m e n t s ( o p t i o n a l )
end l i s t

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 83
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.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 29 / 83
Nodes

Figure 2: Linked list node structure

node // G e n e r a l d a t a T y p e :
d a t a <dataType> dataType
l i n k <p o i n t e r > k e y <keyType>
end node f i e l d 1 <... >
f i e l d 2 <... >
...
f i e l d n <... >
end d a t a T y p e

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 30 / 83
Example

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 31 / 83
Example

Figure 3: List representing polynomial

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 32 / 83
Implementation in C++

Example

node s t r u c t Node {
d a t a <dataType> i n t data ;
l i n k <p o i n t e r > Node ∗ l i n k ;
};
end node

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 33 / 83
Implementation in C++

Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;

s t r u c t Node {
i n t data ;
Node ∗ l i n k ;
};

i n t main ( ) {
Node ∗p = new Node ( ) ;
p−>d a t a = 5 ;
c o u t << p−>d a t a << e n d l ;
Node ∗q = p ;
c o u t << q−>d a t a << e n d l ;
Node ∗ r = new Node ( ) ;
r−>d a t a = 1 0 ;
q−>l i n k = r ;
c o u t << p−>l i n k −>d a t a << e n d l ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 34 / 83
Implementation in C++

Example

s t r u c t Node { s t r u c t Node {
i n t data ; f l o a t data ;
Node ∗ l i n k ; Node ∗ l i n k ;
}; };

t e m p l a t e < c l a s s ItemType>
s t r u c t Node {
ItemType d a t a ;
Node<ItemType> ∗ l i n k ;
};

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 35 / 83
Implementation in C++

Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;

t e m p l a t e < c l a s s ItemType>
s t r u c t Node {
I t e mT y p e d a t a ;
Node<ItemType> ∗ l i n k ;
};

i n t main ( ) {
Node<i n t > ∗p = new Node<i n t > ( ) ;
p−>d a t a = 5 ;
c o u t << p−>d a t a << e n d l ;
Node<i n t > ∗q = p ;
c o u t << q−>d a t a << e n d l ;
Node<i n t > ∗ r = new Node<i n t > ( ) ;
r−>d a t a = 1 0 ;
q−>l i n k = r ;
c o u t << p−>l i n k −>d a t a << e n d l ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 36 / 83
Node implementation in C++

t e m p l a t e < c l a s s ItemType>
c l a s s Node {
I t em T y p e d a t a ;
Node<ItemType> ∗ l i n k ;

public :
Node ( ) {
t h i s −>l i n k = NULL ;
}

Node ( I t e m T yp e d a t a ) {
t h i s −>d a t a = d a t a ;
t h i s −>l i n k = NULL ;
}
};

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 37 / 83
Linked list implementation in C++

template <c l a s s List_ItemType>


class LinkedList {
list
Node<L i s t _ I t e m T y p e > ∗ head ; head <p o i n t e r >
i n t count ;
c o u n t <i n t e g e r >
public : end l i s t
LinkedList ();
~LinkedList ();
};

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 38 / 83
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

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 39 / 83
Create an empty linked list

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 40 / 83
Create an empty linked list

Algorithm createList(ref list <metadata>)


Initializes metadata for a linked list
Pre: list is a metadata structure passed by reference
Post: metadata initialized
list.head = null
list.count= 0
return
End createList

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 41 / 83
Create an empty linked list

template <c l a s s List_ItemType>


class LinkedList {
Node<L i s t _ I t e m T y p e > ∗ head ;
i n t count ;
public :
LinkedList ();
~LinkedList ();
};

template <c l a s s List_ItemType>


L i n k e d L i s t <L i s t _ I t e m T y p e > : : L i n k e d L i s t ( ) {
t h i s −>head = NULL ;
t h i s −>c o u n t = 0 ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 42 / 83
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:
• If the new node becomes the first element in the List: p is list.head.
• Otherwise: p is pPre->link, where pPre points to the predecessor of the new node.
3. Point the new node to its successor.
4. Point the pointer p to the new node.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 43 / 83
Insert into an empty linked list

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 44 / 83
Insert at the beginning

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 45 / 83
Insert in the middle

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 46 / 83
Insert at the end

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 47 / 83
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

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 48 / 83
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

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 49 / 83
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
End insertNode

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 50 / 83
Insert a node into a linked list

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
i n t L i n k e d L i s t <L i s t _ I t e m T y p e > : : I n s e r t N o d e ( Node<L i s t _ I t e m T y p e > ∗ pPre , L i s t _ I t e m T y p e v a l u e ) {
Node<L i s t _ I t e m T y p e > ∗pNew = new Node<L i s t _ I t e m T y p e > ( ) ;
i f ( pNew == NULL)
return 0;
pNew−>d a t a = v a l u e ;
i f ( p P r e== NULL) {
pNew−>l i n k = t h i s −>head ;
t h i s −>head = pNew ;
} else {
pNew−>l i n k = pPre−>l i n k ;
pPre−>l i n k = pNew ;
}
t h i s −>c o u n t ++;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 51 / 83
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).
• If that node is the first element in the List: p is list.head.
• Otherwise: p is pPre->link, where pPre points to the predecessor of the node to be
deleted.
2. p points to the successor of the node to be deleted.
3. Recycle the memory of the deleted node.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 52 / 83
Delete first node

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 53 / 83
General deletion case

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 54 / 83
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
r e c y c l e ( pLoc )

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 55 / 83
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

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 56 / 83
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
end
list.count = list.count - 1
recycle (pLoc)
return
End deleteNode

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 57 / 83
Delete a node from a linked list

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
L i s t _ I t e m T y p e L i n k e d L i s t <L i s t _ I t e m T y p e > : : D e l e t e N o d e ( Node<L i s t _ I t e m T y p e > ∗ pPre ,
Node<L i s t _ I t e m T y p e > ∗ pLoc ) {
L i s t _ I t e m T y p e r e s u l t = pLoc−>d a t a ;
i f ( p P r e== NULL) {
t h i s −>head = pLoc−>l i n k ;
} else {
pPre−>l i n k = pLoc−>l i n k ;
}

t h i s −>c o u n t −−;
d e l e t e pLoc ;
return result ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 58 / 83
Searching in a linked list

• Sequence Search has to be used for the linked list.

• Function Search of List ADT:


<E r r o r C o d e > S e a r c h ( v a l t a r g e t <dataType >,
r e f pPre <p o i n t e r >, r e f pLoc <p o i n t e r >)
Searches a node and returns a pointer to it if found.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 59 / 83
Searching in a linked list

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 60 / 83
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.

Return found or notFound

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 61 / 83
Searching in a linked list

pPre = NULL
pLoc = list.head
while (pLoc is not NULL) AND (target != pLoc ->data) do
pPre = pLoc
pLoc = pLoc ->link
end
if pLoc is NULL then
return notFound
else
return found
end
End Search

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 62 / 83
Searching in a linked list

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
i n t L i n k e d L i s t <L i s t _ I t e m T y p e > : : S e a r c h (
List_ItemType value ,
Node<L i s t _ I t e m T y p e >∗ &pPre ,
Node<L i s t _ I t e m T y p e >∗ &pLoc ) {
p P r e = NULL ;
pLoc = t h i s −>head ;
w h i l e ( pLoc != NULL && pLoc−>d a t a != v a l u e ) {
p P r e = pLoc ;
pLoc = pLoc−>l i n k ;
}
r e t u r n ( pLoc != NULL ) ;
// f o u n d : 1 ; n o t f o u n d : 0
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 63 / 83
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)
pWalker = pWalker -> link
end
End Traverse
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 64 / 83
Traverse a linked list

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
v o i d L i n k e d L i s t <L i s t _ I t e m T y p e > : : T r a v e r s e ( ) {
Node<L i s t _ I t e m T y p e > ∗p = head ;
w h i l e ( p != NULL) {
p−>d a t a ++; // p r o c e s s d a t a h e r e ! ! !
p = p−>l i n k ;
}
}
t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
v o i d L i n k e d L i s t <L i s t _ I t e m T y p e > : :
T r a v e r s e 2 ( L i s t _ I t e m T y p e ∗& v i s i t ) {
Node<L i s t _ I t e m T y p e > ∗p = t h i s −>head ;
int i = 0;
w h i l e ( p != NULL && i < t h i s −>c o u n t ) {
v i s i t [ i ] = p−>d a t a ;
p = p−>l i n k ;
i ++;
}
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 65 / 83
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
dltPtr = list.head
list.head = this.head -> link
recycle (dltPtr)
end
No data left in list. Reset metadata
list.count = 0
return
End destroyList
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 66 / 83
Destroy a linked list

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
v o i d L i n k e d L i s t <L i s t _ I t e m T y p e > : : C l e a r ( ) {
Node<L i s t _ I t e m T y p e > ∗ temp ;
w h i l e ( t h i s −>head != NULL) {
temp = t h i s −>head ;
t h i s −>head = t h i s −>head−>l i n k ;
d e l e t e temp ;
}
t h i s −>c o u n t = 0 ;
}

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
L i n k e d L i s t <L i s t _ I t e m T y p e > : : ~ L i n k e d L i s t ( ) {
t h i s −>C l e a r ( ) ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 67 / 83
Linked list implementation in C++

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
class LinkedList {
Node<L i s t _ I t e m T y p e >∗ head ;
i n t count ;
protected :
i n t I n s e r t N o d e ( Node<L i s t _ I t e m T y p e >∗ pPre ,
List_ItemType v a l u e ) ;
L i s t _ I t e m T y p e D e l e t e N o d e ( Node<L i s t _ I t e m T y p e >∗ pPre ,
Node<L i s t _ I t e m T y p e >∗ pLoc ) ;
i n t Search ( List_ItemType value ,
Node<L i s t _ I t e m T y p e >∗ &pPre ,
Node<L i s t _ I t e m T y p e >∗ &pLoc ) ;

};

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 68 / 83
Linked list implementation in C++

t e m p l a t e <c l a s s L i s t _ I t e m T y p e >
class LinkedList {
protected :
// . . .
public :
LinkedList ();
~LinkedList ();
void I n s e r t F i r s t ( List_ItemType v a l u e ) ;
void I n s e r t L a s t ( List_ItemType v a l u e ) ;
i n t I n s e r t I t e m ( List_ItemType value , i n t p o s i t i o n ) ;
void DeleteFirst ( ) ;
void DeleteLast ( ) ;
int DeleteItem ( int postion ) ;
i n t G e t I t e m ( i n t p o s i t i o n , L i s t _ I t e m T y p e &d a t a O u t ) ;
void Traverse ( ) ;
L i n k e d L i s t <L i s t _ I t e m T y p e >∗ C l o n e ( ) ;
void Print2Console ( ) ;
void Clear ( ) ;
};

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 69 / 83
Linked list implementation in C++

How to use Linked List data structure?


i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) {
L i n k e d L i s t <i n t >∗ m y L i s t =
new L i n k e d L i s t <i n t > ( ) ;
m y L i s t −> I n s e r t F i r s t ( 1 5 ) ;
m y L i s t −> I n s e r t F i r s t ( 1 0 ) ;
m y L i s t −> I n s e r t F i r s t ( 5 ) ;
m y L i s t −>I n s e r t I t e m ( 1 8 , 3 ) ;
m y L i s t −>I n s e r t L a s t ( 2 5 ) ;
m y L i s t −>I n s e r t I t e m ( 2 0 , 3 ) ;
m y L i s t −>D e l e t e I t e m ( 2 ) ;
c o u t << " L i s t ␣ 1 : " << e n d l ;
m y L i s t −>P r i n t 2 C o n s o l e ( ) ;

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 70 / 83
Linked list implementation in C++

How to use Linked List data structure?


// . . .
int value ;
L i n k e d L i s t <i n t >∗ m y L i s t 2 = m y L i s t −>C l o n e ( ) ;
c o u t << " L i s t ␣ 2 : " << e n d l ;
m y L i s t 2 −>P r i n t 2 C o n s o l e ( ) ;
m y L i s t 2 −>G e t I t e m ( 1 , v a l u e ) ;
c o u t << " V a l u e ␣ a t ␣ p o s i t i o n ␣ 1 : ␣ " << v a l u e ;

d e l e t e myList ;
d e l e t e myList2 ;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 71 / 83
Sample Solution: Insert

template <c l a s s List_ItemType>


i n t L i n k e d L i s t <L i s t _ I t e m T y p e > : : I n s e r t I t e m (
List_ItemType value , i n t p o s i t i o n ) {
i f ( p o s i t i o n < 0 | | p o s i t i o n > t h i s −>c o u n t )
return 0;
Node<L i s t _ I t e m T y p e > ∗ newPtr , ∗ p P r e ;
newPtr = new Node<L i s t _ I t e m T y p e > ( ) ;
i f ( newPtr == NULL)
return 0;
newPtr−>d a t a = v a l u e ;
i f ( head == NULL) {
head = newPtr ;
newPtr−>l i n k = NULL ;
} e l s e i f ( p o s i t i o n == 0 ) {
newPtr−>l i n k = head ;
head = newPtr ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 72 / 83
Sample Solution: Insert

else {
// F i n d t h e p o s i t i o n o f pPre
p P r e = t h i s −>head ;

f o r ( i n t i = 0 ; i < p o s i t i o n −1; i ++)


p P r e = pPre−>l i n k ;

// I n s e r t new node
newPtr−>l i n k = pPre−>l i n k ;
pPre−>l i n k = newPtr ;
}

t h i s −>c o u n t ++;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 73 / 83
Sample Solution: Delete

template <c l a s s List_ItemType>


i n t L i n k e d L i s t <L i s t _ I t e m T y p e > : : D e l e t e I t e m ( i n t p o s i t i o n ) {
i f ( p o s i t i o n < 0 | | p o s i t i o n > t h i s −>c o u n t )
return 0;
Node<L i s t _ I t e m T y p e > ∗ d l t P t r , ∗ p P r e ;
i f ( p o s i t i o n == 0 ) {
d l t P t r = head ;
head = head−>l i n k ;
} else {
p P r e= t h i s −>head ;
f o r ( i n t i = 0 ; i < p o s i t i o n −1; i ++)
p P r e = pPre−>l i n k ;
d l t P t r = pPre−>l i n k ;
pPre−>l i n k = d l t P t r −>l i n k ;
}
delete dltPtr ;
t h i s −>c o u n t −−;
return 1;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 74 / 83
Sample Solution: Clone

template <c l a s s List_ItemType>


L i n k e d L i s t <L i s t _ I t e m T y p e >∗
L i n k e d L i s t <L i s t _ I t e m T y p e > : : C l o n e ( ) {
L i n k e d L i s t <L i s t _ I t e m T y p e >∗ r e s u l t =
new L i n k e d L i s t <L i s t _ I t e m T y p e > ( ) ;

Node<L i s t _ I t e m T y p e >∗ p = t h i s −>head ;

w h i l e ( p != NULL) {
r e s u l t −>I n s e r t L a s t ( p−>d a t a ) ;
p = p−>l i n k ;
}

r e s u l t −>c o u n t = t h i s −>c o u n t ;
return result ;
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 75 / 83
Reverse a linked list

Exercise
template <c l a s s List_ItemType>
v o i d L i n k e d L i s t <L i s t _ I t e m T y p e > : : R e v e r s e ( ) {
// . . .
}

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 76 / 83
Other linked lists
Doubly Linked List

Figure 4: Doubly Linked List allows going forward and backward.

node list
d a t a <dataType> c u r r e n t <p o i n t e r >
n e x t <p o i n t e r > end l i s t
p r e v i o u s <p o i n t e r >
end node

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 77 / 83
Doubly Linked List

Figure 5: Doubly Linked List allows going forward and backward.

Figure 6: Insert an element in Doubly Linked List.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 78 / 83
Circularly Linked List

node list
d a t a <dataType> c u r r e n t <p o i n t e r >
l i n k <p o i n t e r > end l i s t
end node

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 79 / 83
Double circularly Linked List

node list
d a t a <dataType> c u r r e n t <p o i n t e r >
n e x t <p o i n t e r > end l i s t
p r e v i o u s <p o i n t e r >
end node

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 80 / 83
Comparison of implementations
of list
Arrays: Pros and Cons

• Pros:
• Access to an array element is fast since we can compute its location quickly.

• Cons:
• If we want to insert or delete an element, we have to shift subsequent elements which slows
our computation down.
• We need a large enough block of memory to hold our array.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 81 / 83
Linked Lists: Pros and Cons

• Pros:
• Inserting and deleting data does not require us to move/shift subsequent data elements.

• Cons:
• If we want to access a specific element, we need to traverse the list from the head of the list
to find it which can take longer than an array access.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 82 / 83
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.

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 83 / 83

You might also like