1 / 24

CMSC 202

CMSC 202. Computer Science II for Majors. Topics. Templates Linked Lists. 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.

georgehall
Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Computer Science II for Majors

  2. Topics • Templates • Linked Lists

  3. 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. 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. 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. 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 <class T> ReturnType FunctionName (arguments of type T) { // Code // Use type T wherever appropriate }

  7. Function Templates … cont • Function template for our swap() function template <class T> 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. 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. 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. 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. Class Templates … cont • General form template <class T> class ClassName { // code // Use type T wherever appropriate };

  12. Class Templates … cont template <class T> 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. Class Templates … cont • Thus we can declare vectors of any types • Using MyVector class template MyVector <int> v1(10); // vector of int of size 10 MyVector <float> v2(20); // vector of floats MyVector <Complex> v3(2); // vector of 2 Complex // objects and push values onto vector v1.PushBack(5); v2.PushBack(3.5);

  14. 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. 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. 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. 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. 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. 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. 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. Linked Lists … cont • Representation Head Next Next Next SLList Object Node Object

  22. 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 Tail Head Next Next Next

  23. 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. 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

More Related