Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Beginning C++ Through Game Programming, Second Edition
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,
More on the STL vector list stack queue priority_queue.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Lecture No.01 Data Structures Dr. Sohail Aslam
Data Structures Using C++ 2E
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT 12.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 7 : Intro. to STL (Standard Template Library)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Object-Oriented Programming (OOP) Lecture No. 41
Motivation for Generic Programming in C++
Standard Template Library
Data Structures and Algorithms
Data Structures and Algorithms
Lecture 6 of Computer Science II
Cpt S 122 – Data Structures Abstract Data Types
Topic Pre-processor cout To output a message.
Lecture 04: Sequences structures
Standard Template Library
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Dr. Bernard Chen Ph.D. University of Central Arkansas
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
Linked Lists.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 17: Linked Lists.
The Standard Template Library
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
Lecture 8-2 : STL Iterators and Algorithms
Array-Based Lists & Pointers
Standard Template Library
STL List.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
STL List.
Presentation transcript:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session : Template Class “list” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics Template classes and functions C++ Standard Library The “string” class The “vector” class Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture The template class “list” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Doubly Linked List Backward link Convenient way to represent dynamically created sequence of objects/data items Memory allocated for consecutive objects in list not necessarily contiguous, may not even be allocated at same time Forward link Data Item1 Data Item 2 Data Item 3 Data Item 4 ? ? Front of list Back of list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Deletion in Doubly Linked List Efficient (constant time) deletion of objects Data Item1 Data Item 2 Data Item 3 Data Item 4 Front of list Back of list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Deletion in Doubly Linked List Efficient (constant time) deletion of objects Data Item1 Data Item 3 Data Item 4 Front of list Back of list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Insertion in Doubly Linked List Efficient (constant time) insertion of objects Data Item1 Data Item 3 Data Item 4 Front of list Back of list Data Item 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Insertion in Doubly Linked List Efficient (constant time) insertion of objects Data Item1 Data Item 2 Data Item 3 Data Item 4 Front of list Back of list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Accessing nth Element in Doubly Linked List Example: Accessing 3rd element in list Inefficient: Requires traversing list Data Item1 Data Item 2 Data Item 3 Data Item 4 Front of list Back of list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Comparison with a Vector Data Item1 Data Item 2 Data Item 3 Data Item 4 Contiguous memory locations for successive elements Insertion and deletion much more expensive Requires copying of data items/objects Accessing nth element is efficient (constant time) Choice of linked list vs vector depends on nature of program E.g., Sorting: Linked list is a good choice Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

The “list” class For representing and manipulating doubly linked lists Template class : Can be instantiated with type of data item Dynamically allocate/de-allocate memory Dynamic memory management built in “list” objects are container objects Must use #include <list> at start of program Large collection of member functions We’ll see only a small subset Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Simple Programming using “list” #include <iostream> #include <list> using namespace std; int main() { list<string> names; list<string> books (3, “Alice in Wonderland”); list<int> numbers (10, -1); … Some other code … } Creates an empty list of strings Name of list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Simple Programming using “list” #include <iostream> #include <list> using namespace std; int main() { list<string> names; list<string> books (3, “Alice in Wonderland”); list<int> numbers (10, -1); … Some other code … } Creates a list of 3 strings. Each string in the list is “Alice in Wonderland” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Simple Programming using “list” #include <iostream> #include <list> using namespace std; int main() { list<string> names; list<string> books (3, “Alice in Wonderland”); list<int> numbers (10, -1); … Some other code … } Creates a list of 10 integers. Each integer in the list is -1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Finding the Size of a “list” #include <iostream> #include <list> using namespace std; int main() { list<string> names; list<string> books (3, “Alice in Wonderland”); list<int> numbers (10, -1); cout << “Sizes: “; cout << names.size() << “ “ << books.size() << “ “ << numbers.size(); cout << endl; return 0; } Sizes: 0 3 10 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Adding Elements at Front/Back of a “list” names: int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Adding Elements at Front/Back of a “list” names: “Abdul” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Adding Elements at Front/Back of a “list” names: “Ajanta” “Abdul” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Adding Elements at Front/Back of a “list” names: “Ajanta” “Abdul” “Bobby” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Adding Elements at Front/Back of a “list” names: “Alex” “Ajanta” “Abdul” “Bobby” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing Elements From Front/Back of a “list” names: “Alex” “Ajanta” “Abdul” “Bobby” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); names.pop_back(); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing Elements From Front/Back of a “list” names: “Alex” “Ajanta” “Abdul” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); names.pop_back(); names.pop_front(); names.pop_front(); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing Elements From Front/Back of a “list” names: “Ajanta” “Abdul” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); names.pop_back(); names.pop_front(); names.pop_front(); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing Elements From Front/Back of a “list” names: “Abdul” int main() { list<string> names; names.push_back(“Abdul”); names.push_front(“Ajanta”); names.push_back(“Bobby”); names.push_front(“Alex”); names.pop_back(); names.pop_front(); names.pop_front(); … Some other code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Iterator Related Functions in “list” Class int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it; for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; begin(), end() member functions Bobby, Ajanta, Abdul, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Iterator Related Functions in “list” Class int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::reverse_iterator rit; for (rit = names.rbegin(); rit != names.rend(); rit++) { cout << *rit << “, “; } return 0; rbegin(), rend() member functions Abdul, Ajanta, Bobby, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Alex” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; Bobby, Alex, Ajanta, Abdul, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Alex” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Alex” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Avi” “Avi” “Alex” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Inserting in a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; Bobby, Avi, Avi, Alex, Ajanta, Abdul, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing from a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); names.erase(it); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing from a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); names.erase(it); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Alex” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing from a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); names.erase(it); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; names: “Bobby” “Alex” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Removing from a “list” using Iterator int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); names.erase(it); for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } return 0; Bobby, Alex, Abdul, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Accessing the Front and Back Elements in a “list” int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); names.erase(it); cout << “Front element is: “ << names.front() << endl; cout << “Back element is: “ << names.back() << endl; return 0; } names: “Bobby” “Alex” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Accessing the Front and Back Elements in a “list” int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it = names.begin(); it++; names.insert(it, “Alex”); names.erase(it); cout << “Front element is: “ << names.front() << endl; cout << “Back element is: “ << names.back() << endl; return 0; } Front element is: Bobby Back element is: Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

“Bobby” “Ajanta” “Abdul” Reversing a “list” int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it; for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; names.reverse(); cout << endl; return 0; } names: “Bobby” “Ajanta” “Abdul” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Reversing a “list” Bobby, Ajanta, Abdul, int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it; for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; names.reverse(); cout << endl; return 0; } Bobby, Ajanta, Abdul, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

“Abdul” “Ajanta” “Bobby” Reversing a “list” int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it; for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; names.reverse(); cout << endl; return 0; } names: “Abdul” “Ajanta” “Bobby” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Reversing a “list” Abdul, Ajanta, Bobby, int main() { list<string> names; names.push_front(“Abdul”); names.push_front(“Ajanta”); names.push_front(“Bobby”); list<string>::iterator it; for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; names.reverse(); cout << endl; return 0; } Abdul, Ajanta, Bobby, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Lists of Complex Data Types “list” is a template class Can be instantiated with any data type Can even have lists of lists of lists … list<V3> myList1; list<list<int *> > myList2; Note the space Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Summary “list” class and its usage Several more features exist … Only some features studied Several more features exist … Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay