Professional Documents
Culture Documents
Linked List-Class Implementation: Programming Fundamentals (CO1027) Saturday 23 April, 2022
Linked List-Class Implementation: Programming Fundamentals (CO1027) Saturday 23 April, 2022
Implementation
Tien Vu Van
Tien Vu Van
Faculty of Computer Science and Engineering
Ho Chi Minh City University of Technology
Tien Vu Van
Class Templates
1 Class Templates
Struct of Linked List
Nodes
Implemeting class
Linked List using
Template
2 Struct of Linked List Nodes
Tien Vu Van
Class Templates
Tien Vu Van
Class Templates
Tien Vu Van
Place this line above declaration of a class/struct/function:
1 template < typename T >
T serves as a generic type specifier for which a real type will Class Templates
be substituted. Struct of Linked List
Nodes
Implemeting class
Linked List using
Template
Tien Vu Van
Place this line above declaration of a class/struct/function:
1 template < typename T >
T serves as a generic type specifier for which a real type will Class Templates
be substituted. Struct of Linked List
Nodes
1 template < typename T >
Implemeting class
2 class Node { Linked List using
3 private : Template
4 T data ;
5 public :
6 Node ( T inData ) {
7 this - > data = inData ;
8 }
9 void print () {
10 cout << " Data ␣ of ␣ Node : ␣ "
11 << this - > data ;
12 }
13 };
Tien Vu Van
Tien Vu Van
Tien Vu Van
Class Templates
STRUCTURE OF A Nodes
Implemeting class
Linked List using
Tien Vu Van
Implemeting class
Linked List using
Template
Tien Vu Van
Tien Vu Van
Class Templates
Implemeting class
USING TEMPLATE
Tien Vu Van
Class Templates
4 };
Tien Vu Van
Tien Vu Van
Let’s define some basic methods for working with Linked Class Templates
List object: Struct of Linked List
Nodes
1 public :
Implemeting class
2 LinkedList (); Linked List using
3 ~ LinkedList (); Template
4 bool add ( int index , const T & data );
5 bool remove ( int index );
6 bool isEmpty ();
7 int getSize ();
8 void print ();
Tien Vu Van
6 Implemeting class
Linked List using
7 public : Template
8 LinkedList ();
9 ~ LinkedList ();
10 bool add ( int index , const T & data );
11 bool remove ( int index );
12 bool isEmpty ();
13 int getSize ();
14 void print ();
15 };
Tien Vu Van
Tien Vu Van
Class Templates
Implemeting class
it returns false. Linked List using
Template
• getSize: Returns current number of nodes in the list.
Could you implement two above methods?
Tien Vu Van
Prototype
Implemeting class
Linked List using
Method Template
Tien Vu Van
Prototype
Implemeting class
Linked List using
Implementation Template
Tien Vu Van
Prototype
Implemeting class
1 template <typename T> Linked List using
Template
2 void L i n k e d L i s t <T> : : p r i n t ( ) {
3 if ( this−>c o u n t == 0 ) {
4 c o u t << " ( Empty ␣ List ) " << e n d l ;
5 }
6 else {
7 Node<T> ∗ c u r = this−>head ;
8 c o u t << " [ ␣ " ;
9 while ( c u r != NULL) {
10 c o u t << c u r −>d a t a << " ␣ " ;
11 }
12 c o u t << " ] " ;
13 }
14 }
Tien Vu Van
Prototype
The method returns true if it can perform the insertion, Implemeting class
Linked List using
otherwise it returns false. Template
Method
• When doesn’t the insertion perform? What is the
valid range of value for index?
• Review your previous chapter for performing insertion
in a list. How many cases of insertion?
Tien Vu Van
Prototype
Method
• When doesn’t the deletion perform? What is the valid
range of value for index?
• Review your previous chapter for performing insertion
in a list. How many cases of deletion?
Tien Vu Van
Prototype
Implemeting class
Linked List using
Template
Method
• Recall that each node is allocated dynamically in
Heap (by calling operator new), we must deallocate
these memories by calling the operator delete.
• Traverse the list and use a temporary pointer to
deallocate the memory.
Tien Vu Van
Prototype
1 ∼LinkedList ();