List
List
Chapter 4 - List
2. Array implementation
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.
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.
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
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
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 83
Removal
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
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 14 / 83
Success of Basic Operations
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 rangeCheck ( i n t ) ;
void set ( int , int ) ;
int get ( i n t ) ;
void removeAt ( i n t ) ;
void insertAt ( int , int ) ;
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 ! " ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 83
Dynamic Array: Implementation in C++
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
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.
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 data,
• the address of the next node.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 29 / 83
Nodes
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
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++
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 38 / 83
Linked list operations
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
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 41 / 83
Create an empty linked list
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 42 / 83
Insert a node into a linked list
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
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
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
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 56 / 83
Delete a node from a linked list
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
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
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.
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
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
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++
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 70 / 83
Linked list implementation in C++
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
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 ;
// 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
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 74 / 83
Sample Solution: Clone
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
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
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
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 83 / 83