You are on page 1of 25

Linked List-Class

Implementation

Tien Vu Van

Linked List-Class Implementation Class Templates

Struct of Linked List


Nodes

Programming Fundamentals (CO1027) Implemeting class


Linked List using
Saturday 23rd April, 2022 Template

Tien Vu Van
Faculty of Computer Science and Engineering
Ho Chi Minh City University of Technology

Linked List-Class Implementation.1


Linked List-Class
Overview Implementation

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

3 Implemeting class Linked List using Template

Linked List-Class Implementation.2


Linked List-Class
Implementation

Tien Vu Van

Class Templates

Struct of Linked List


Nodes

CLASS TEMPLATE Implemeting class


Linked List using
Template

Linked List-Class Implementation.3


Linked List-Class
What is Class Template? Implementation

Tien Vu Van

Class Templates

Struct of Linked List


Nodes
Definition
Implemeting class
Templates provide parameterized types - that is, they are Linked List using
Template
capable of passing a type name as an argument to a recipe
for building a class or a function or a struct.

Linked List-Class Implementation.4


Linked List-Class
Defining a Class Template Implementation

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

Linked List-Class Implementation.5


Linked List-Class
Defining a Class Template Implementation

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 };

Linked List-Class Implementation.5


Linked List-Class
Using a Class with Template Implementation

Tien Vu Van

Instantiate Class Class Templates

Struct of Linked List


1 // create a node contains int data Nodes

2 Node < int > intNode (10); Implemeting class


Linked List using
3 intNode . print (); Template

Linked List-Class Implementation.6


Linked List-Class
Using a Class with Template Implementation

Tien Vu Van

Instantiate Class Class Templates

Struct of Linked List


1 // create a node contains int data Nodes

2 Node < int > intNode (10); Implemeting class


Linked List using
3 intNode . print (); Template

1 // create a node contains string object


2 Node < string > strNode ( " Hello ␣ World ! " );
3 strNode . print ();

Linked List-Class Implementation.6


Linked List-Class
Implementation

Tien Vu Van

Class Templates

Struct of Linked List

STRUCTURE OF A Nodes

Implemeting class
Linked List using

LINKED LIST NODE


Template

Linked List-Class Implementation.7


Linked List-Class
Structure of a linked list node Implementation

Tien Vu Van

• Recall that each node of a linked list must store the


data as well as the address for the next node in list.
Class Templates

Struct of Linked List


Nodes

Implemeting class
Linked List using
Template

• Applying template to Node struct.


1 template < typename T >
2 struct Node {
3 T data ;
4 Node <T > * next ;
5 }

Linked List-Class Implementation.8


Linked List-Class
Structure of a linked list node Implementation

Tien Vu Van

• Define Constructor to easily initialize struct’s


attributes. Class Templates

Struct of Linked List


1 template < typename T > Nodes
2 struct Node {
Implemeting class
3 T data ; Linked List using
Template
4 Node <T > * next ;
5 Node ( const T & data , Node <T > * next = NULL );
6 }

• Why do we use Node<T> instead of Node at line 4,


6?
• Could you implement the Constructor of Node?

Linked List-Class Implementation.8


Linked List-Class
Implementation

Tien Vu Van

Class Templates

IMPLEMENTING Struct of Linked List


Nodes

Implemeting class

CLASS LINKED LIST Linked List using


Template

USING TEMPLATE

Linked List-Class Implementation.9


Linked List-Class
Declaration of class with template Implementation

Tien Vu Van

Class Templates

Struct of Linked List


Nodes
1 template < typename T >
Implemeting class
2 class LinkedList { Linked List using
3 // ... Template

4 };

Linked List-Class Implementation.10


Linked List-Class
Attributes of class Linked List Implementation

Tien Vu Van

We will implement the class Linked List consisting of 2 fol-


lowing attributes:
Class Templates
• head: a pointer stores the address of first node in the Struct of Linked List
Nodes
list.
Implemeting class
• count: number of nodes in list. (Why should we need Linked List using
Template
count?)
1 private :
2 Node <T > * head ;
3 int count ;

Why should we put these attributes into access modifier of


private?

Linked List-Class Implementation.11


Linked List-Class
Methods of class Linked List Implementation

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

Linked List-Class Implementation.12


Linked List-Class
Full declaration of class Implementation

Tien Vu Van

1 template < typename T >


2 class LinkedList {
3 private : Class Templates
4 Node <T > * head ; Struct of Linked List
5 int count ; Nodes

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 };

Linked List-Class Implementation.13


Linked List-Class
Class methods: Constructor Implementation

Tien Vu Van

The Constructor simply initializes the list to an empty state.


There is no node in list, the address of first node should be
Class Templates
NULL.
Struct of Linked List
Following code is the implementation of Constructor that is Nodes
outside of class’s declaration. Implemeting class
Linked List using
1 template < typename T > Template

2 LinkedList <T >:: LinkedList () {


3 this - > head = NULL ;
4 this - > count = 0;
5 }

It’s differ from a normal class (a class without template) at


declaring template at line 1 and providing type ¡T¿ for class
at line 2.

Linked List-Class Implementation.14


Linked List-Class
Class methods: isEmpty & getSize Implementation

Tien Vu Van

Class Templates

Struct of Linked List


• isEmpty: Returns true if the list is empty, otherwise Nodes

Implemeting class
it returns false. Linked List using
Template
• getSize: Returns current number of nodes in the list.
Could you implement two above methods?

Linked List-Class Implementation.15


Linked List-Class
Class methods: print Implementation

Tien Vu Van

Prototype

1 void print (); Class Templates

Struct of Linked List


Function to output the data contained in each node. Nodes

Implemeting class
Linked List using
Method Template

• Traverse over each node of list and print its data.


• Use another pointer to traverse the list. (Why don’t
we use head?)
• Go to next node using next. (When do we stop to
travers?)

Linked List-Class Implementation.16


Linked List-Class
Class methods: print Implementation

Tien Vu Van

Prototype

1 void print (); Class Templates

Struct of Linked List


Function to output the data contained in each node. Nodes

Implemeting class
Linked List using
Implementation Template

1 template < typename T >


2 void LinkedList <T >:: print () {
3 Node <T > * cur = this - > head ;
4 while ( cur != NULL ) {
5 cout << cur - > data << " ␣ " ;
6 }
7 }

Linked List-Class Implementation.16


Linked List-Class
Class methods: print Implementation

Tien Vu Van
Prototype

1 void print ();

Function to output the data contained in each node.


Class Templates

Struct of Linked List


Improve: Handle empty list Nodes

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 }

Linked List-Class Implementation.16


Linked List-Class
Class methods: add Implementation

Tien Vu Van

Prototype

1 bool add ( int index , const T & data ); Class Templates

Struct of Linked List


Insert a new node containing data at position index in list. Nodes

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?

Linked List-Class Implementation.17


Linked List-Class
Class methods: remove Implementation

Tien Vu Van

Prototype

1 bool remove ( int index ); Class Templates

Struct of Linked List


• Remove a node at position of index in the list. The Nodes

method returns true if it can perform the deletion, Implemeting class


Linked List using
otherwise it returns false. Template

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?

Linked List-Class Implementation.18


Linked List-Class
Class methods: Destructor Implementation

Tien Vu Van

Prototype

1 ∼LinkedList (); Class Templates

Struct of Linked List


• Remove all nodes from the list. Nodes

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.

Linked List-Class Implementation.19


Linked List-Class
Class methods: Destructor Implementation

Tien Vu Van

Prototype

1 ∼LinkedList ();

• Remove all nodes from the list. Class Templates

Struct of Linked List


Nodes

Implementation Implemeting class


Linked List using
Template
1 template < typename T >
2 LinkedList <T >::∼LinkedList <T >() {
3 Node <T > * temp ;
4 while ( head != NULL ) {
5 temp = head ;
6 head = head - > next ;
7 delete temp ;
8 }
9 count = 0;
10 }

Linked List-Class Implementation.19

You might also like