Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.

Similar presentations


Presentation on theme: "CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists."— Presentation transcript:

1 CMSC 202 Computer Science II for Majors

2 CMSC 202UMBC Topics Templates Linked Lists

3 CMSC 202UMBC Templates Templates allow us to create a family of functions or classes Templates enable programmers to create entire range of related functions or related classes e.g.  Class template for array class would enable us to create arrays of various data types like int, float  Function template for mul() function would enable us to multiply numbers of various data types like int, float

4 CMSC 202UMBC Templates… cont Templates and polymorphism  We have seen how to achieve Compile time polymorphism and Run time polymorphism  Template is a type of compile time polymorphism  Template is defined with a parameter that would be replaced by specified data type at time of actual use  Thus template is a method of achieving compile time polymorphism through parameters

5 CMSC 202UMBC Function Templates Function templates are used to create a family of functions with different argument types We want to write a function swap() in order to swap values of different data types Function overloading void swap ( const int& m, const int& n ); void swap ( const float& m, const float& n ); void swap ( const myClass& m, const myClass& n); and invoke it as swap(num1, num2);

6 CMSC 202UMBC Function Templates … cont Redundancy: we need to write implementation of swap() multiple times although the basic algorithm is same Function templates enable us to write just one function instead of all swap() functions template ReturnType FunctionName (arguments of type T) { // Code // Use type T wherever appropriate }

7 CMSC 202UMBC Function Templates … cont Function template for our swap() function template void swap ( T& x, T& y) { T temp = x; x = y; y = temp; } Invoke function like any ordinary function swap ( a, b ); // a, b can be of any type

8 CMSC 202UMBC Class Templates Class templates are used to create generic classes They work with any data type (basic or programmer-defined) Consider we want to build a vector class  Enable us to declare vector of integers  Has a constructor and a member function to push integer values into vector

9 CMSC 202UMBC Class Templates … cont class MyVector { public: vector (int size = 0) // Constructor { m_size = size; m_v = new int [m_size]; for (int i=0; i < m_size; i++) m_v[i] = 0; // initialize vector to 0 } void PushBack (int value) // push value onto vector { m_v[m_size] = value; m_size++; } private: int *m_v; int m_size; };

10 CMSC 202UMBC Class Templates … cont Our MyVector class will create a vector of integers But what if we want a vector of floats or a vector of Complex objects Simple, replace the appropriate int by float Still better, use Class Template – create a framework which could be used for any type

11 CMSC 202UMBC Class Templates … cont General form template class ClassName { // code // Use type T wherever appropriate };

12 CMSC 202UMBC Class Templates … cont template class MyVector { public: vector (int size = 0) // Constructor { m_size = size; m_v = new T [m_size]; for (int i=0; i < m_size; i++) m_v[i] = 0; // initialize vector to 0 } void PushBack (T value) // push value onto vector { m_v[m_size] = value; m_size++; } private: T *m_v; int m_size; };

13 CMSC 202UMBC Class Templates … cont Thus we can declare vectors of any types Using MyVector class template MyVector v1(10); // vector of int of size 10 MyVector v2(20); // vector of floats MyVector v3(2); // vector of 2 Complex // objects and push values onto vector v1.PushBack(5); v2.PushBack(3.5);

14 CMSC 202UMBC Linked Lists Lists  List is an ordered collection of homogenous elements  List is dynamic, new elements can be added, existing ones can be removed at any time  List is an Abstract Data Type  E.g. List of integers { 2, 10, 34, 48, 69, 82 }  User is not concerned with how the list is implemented

15 CMSC 202UMBC Lists … cont List Operations  Create a list  Insert element into list  Remove element from list  Find position of a given element in list  Assign one list to another  Delete the list  Check if list is full  Check if list is empty  Make list empty  Display contents of list

16 CMSC 202UMBC Lists … cont Designing lists  Number of elements Upper bound Infinite  Inserting duplicate elements  Numbering of positions  Sorted lists How would insertion, deletion be affected ?

17 CMSC 202UMBC Lists … cont Implementation  Array Elements are physically contiguous in memory Accessing elements is faster by using sub script e.g. array[i]  Linked List Elements are only logically contiguous, physically a node can be anywhere in memory Typically need to traverse linked list on per node basis to access elements

18 CMSC 202UMBC Linked Lists Linked list is a linear collection of nodes which are connected by pointers Linked list is a chain of nodes Node has at least two members  Data  Pointer to next node in list  Such lists are called Singly Linked List  Also have Doubly Linked List, Circular Linked Lists

19 CMSC 202UMBC Linked Lists … cont Linked Lists in C++  A class with public methods which define different operations to be performed on list Pointer to first node in list is needed (Head) Other members like tail pointer etc  Another class for representing nodes in the linked list Pointer to next node is necessary

20 CMSC 202UMBC Linked Lists … cont class Node { private: Node( int d = 0, Node* n = NULL ); int m_data; Node* m_next; friend class SLList; // We want SLList to access nodes }; class SLList { public: // public methods to implement list operations private: int m_capacity; int m_size; Node *m_head; };

21 CMSC 202UMBC Linked Lists … cont Representation SLList Object Node Object Head Next

22 CMSC 202UMBC Linked Lists … cont Adding new node  Add at front of list  Add at end of list Use a Tail pointer, which always points to last node in list Head Next Tail

23 CMSC 202UMBC Linked Lists … cont Deleting Node  Node to be deleted is Head Store the Head pointer in a temporary one Advance Head (m_head = m_head -> m_next) Delete temporary pointer  Node to be deleted is neither of above cases Connect previous node to next node Delete the current node

24 CMSC 202UMBC Linked Lists … cont Deleting Nodes  Node to be deleted is Tail Store Tail pointer in temporary one Obtain the previous node of Tail ( expensive operation, need to traverse almost entire list ) Make previous node as Tail, which points to NULL Delete temporary pointer


Download ppt "CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists."

Similar presentations


Ads by Google