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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists CS 3358.
Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
Data Structures Using C++ 2E
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
Pointers OVERVIEW.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Linked List by Chapter 5 Linked List by
List Interface and Linked List Mrs. Furman March 25, 2010.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Linked Data Structures
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Lectures linked lists Chapter 6 of textbook
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
UNIT-3 LINKED LIST.
CISC181 Introduction to Computer Science Dr
Templates II CMSC 202.
Chapter 18: Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Chapter 17: Linked Lists.
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Computer Science II for Majors
Presentation transcript:

CMSC 202 Computer Science II for Majors

CMSC 202UMBC Topics Templates Linked Lists

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

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

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

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 }

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

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

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

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

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

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

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

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

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

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 ?

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

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

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

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

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

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

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

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