CMSC 202

24
CMSC 202 Computer Science II for Majors

description

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. - PowerPoint PPT Presentation

Transcript of CMSC 202

Page 1: CMSC 202

CMSC 202

Computer Science II for Majors

Page 2: CMSC 202

CMSC 202 UMBC

Topics

Templates Linked Lists

Page 3: CMSC 202

CMSC 202 UMBC

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

Page 4: CMSC 202

CMSC 202 UMBC

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

Page 5: CMSC 202

CMSC 202 UMBC

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 overloadingvoid 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 asswap(num1, num2);

Page 6: CMSC 202

CMSC 202 UMBC

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() functionstemplate <class T>ReturnType FunctionName (arguments of type T){

// Code // Use type T wherever appropriate

}

Page 7: CMSC 202

CMSC 202 UMBC

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

Page 8: CMSC 202

CMSC 202 UMBC

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

Page 9: CMSC 202

CMSC 202 UMBC

Class Templates … contclass 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;

};

Page 10: CMSC 202

CMSC 202 UMBC

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

Page 11: CMSC 202

CMSC 202 UMBC

Class Templates … cont

General form

template <class T>class ClassName{

// code// Use type T wherever appropriate

};

Page 12: CMSC 202

CMSC 202 UMBC

Class Templates … conttemplate <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;

};

Page 13: CMSC 202

CMSC 202 UMBC

Class Templates … cont

Thus we can declare vectors of any types Using MyVector class template

MyVector <int> v1(10); // vector of int of size 10MyVector <float> v2(20); // vector of floatsMyVector <Complex> v3(2); // vector of 2 Complex

// objects and push values onto vector v1.PushBack(5);

v2.PushBack(3.5);

Page 14: CMSC 202

CMSC 202 UMBC

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

Page 15: CMSC 202

CMSC 202 UMBC

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

Page 16: CMSC 202

CMSC 202 UMBC

Lists … cont Designing lists

Number of elements Upper bound Infinite

Inserting duplicate elements Numbering of positions Sorted lists

How would insertion, deletion be affected ?

Page 17: CMSC 202

CMSC 202 UMBC

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

Page 18: CMSC 202

CMSC 202 UMBC

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

Page 19: CMSC 202

CMSC 202 UMBC

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

Page 20: CMSC 202

CMSC 202 UMBC

Linked Lists … contclass 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;};

Page 21: CMSC 202

CMSC 202 UMBC

Linked Lists … cont

Representation

SLList Object

Node Object

Head Next Next Next

Page 22: CMSC 202

CMSC 202 UMBC

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 Next Next

Tail

Page 23: CMSC 202

CMSC 202 UMBC

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

Page 24: CMSC 202

CMSC 202 UMBC

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