Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Computer Programming

Template based set of collection classes STL collection types (container types)

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

SSE2034: System Software Experiment 3

CS 215 Lecture 17 Linked lists and time complexities

Computer Programming

Computer Programming

Computer Programming

Templates and Vectors

Data Structure. Recitation III

pointers + memory double x; string a; int x; main overhead int y; main overhead

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Tenka1 Programmer Contest/Tenka1 Programmer Beginner Contest 2018

PIC 10A. Lecture 23: Intro to STL containers

Week 3: Pointers (Part 2)

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

STL: C++ Standard Library

CMSC 341 Lecture 7 Lists

COEN244: Class & function templates

Lecture-5. STL Containers & Iterators

by Pearson Education, Inc. All Rights Reserved. 2

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

Standard Template Library

Computer Programming. Dr. Fahad Computer Science Deptt.

lecture09: Linked Lists

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CS197c: Programming in C++

Chapter 17 - Notes Recursion

CPSC 427a: Object-Oriented Programming

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

List Iterator Implementation

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

19.1 The Standard Template Library

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Computer Programming

To become familiar with array manipulation, searching, and sorting.

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Lecture on pointers, references, and arrays and vectors

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CS93SI Handout 04 Spring 2006 Apr Review Answers

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Computer Programming

Chapter 1: Object-Oriented Programming Using C++

C++ Standard Template Library

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

C++ basics Getting started with, and Data Types.

BBM 201 DATA STRUCTURES

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Dynamic Data Structures

COL 100. Minor 2 Exam - Practice

Programming in C/C Lecture 2

Data Structures CSci 1200 Test 2 Questions

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

COMP322 - Introduction to C++

Exceptions, Templates, and the STL

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Data Structures (CS301) LAB

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Chapter 5. The Standard Template Library.

CS 103 Unit 12 Slides

Topics. bool and string types input/output library functions comments memory allocation templates classes

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

BITG 1113: Function (Part 2) LECTURE 5

STL Standard Template Library

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Lectures 11,12. Online documentation & links

VARIABLES & ASSIGNMENTS

CPSC 427: Object-Oriented Programming

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

1a Computers, Problem Solving!

Transcription:

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

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

Overview of This Lecture The template class list 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Doubly Linked List Backward link Forward link? 1 Front of list 2 3 4 Back of list? 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 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

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

Deletion in Doubly Linked List 1 Front of list 3 4 Back of list Efficient (constant time) deletion of objects Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

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

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

Accessing n th Element in Doubly Linked List Example: Accessing 3 rd element in list 1 Front of list 2 3 4 Back of list Inefficient: Requires traversing list Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Comparison with a Vector 1 2 3 4 Contiguous memory locations for successive elements Insertion and deletion much more expensive Requires copying of data items/objects Accessing n th 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, 10

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, 11

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

Simple Programming using list #include <iostream> #include <list> using namespace std; 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, 13

Simple Programming using list #include <iostream> #include <list> using namespace std; 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, 14

Finding the Size of a list #include <iostream> #include <list> Sizes: 0 3 10 using namespace std; 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; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Adding Elements at Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); Some other code names: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Adding Elements at Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); Some other code names: Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Adding Elements at Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); Some other code names: Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

Adding Elements at Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); Some other code names: Ajanta Abdul Bobby Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Adding Elements at Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); Some other code names: Alex Ajanta Abdul Bobby Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

Removing Elements From Front/Back of a list names: Alex Ajanta Abdul Bobby 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, 21

Removing Elements From Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); names.pop_back(); names.pop_front(); Some other code names.pop_front(); names: Alex Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

Removing Elements From Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); names.pop_back(); names.pop_front(); Some other code names.pop_front(); names: Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

Removing Elements From Front/Back of a list names.push_back( Abdul ); names.push_front( Ajanta ); names.push_back( Bobby ); names.push_front( Alex ); names.pop_back(); names.pop_front(); Some other code names.pop_front(); names: Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 24

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

Iterator Related Functions in list Class names.push_front( Abdul ); rbegin(), rend() member functions 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; Abdul, Ajanta, Bobby, Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 26

Inserting in a list using Iterator 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, 27

Inserting in a list using Iterator 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, 28

Inserting in a list using Iterator 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, 29

Inserting in a list using Iterator 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, 30

Inserting in a list using Iterator 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, 31

Inserting in a list using Iterator 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, 32

Inserting in a list using Iterator 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, 33

Inserting in a list using Iterator 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, 34

Inserting in a list using Iterator 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, 35

Removing from a list using Iterator 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, 36

Removing from a list using Iterator 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, 37

Removing from a list using Iterator 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, 38

Removing from a list using Iterator 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, 39

Accessing the Front and Back Elements in a list 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, 40

Accessing the Front and Back Elements in a list 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, 41

Reversing a list names.push_front( Abdul ); names: Bobby Ajanta 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(); for (it = names.begin(); it!= names.end(); it++) { cout << *it <<, ; cout << endl; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 42

Reversing a list Bobby, Ajanta, Abdul, 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(); for (it = names.begin(); it!= names.end(); it++) { cout << *it <<, ; cout << endl; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 43

Reversing a list names.push_front( Abdul ); names: Abdul Ajanta Bobby 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(); for (it = names.begin(); it!= names.end(); it++) { cout << *it <<, ; cout << endl; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 44

Reversing a list Abdul, Ajanta, Bobby, 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(); for (it = names.begin(); it!= names.end(); it++) { cout << *it <<, ; cout << endl; return 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 45

Lists of Complex 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, 46

Summary list class and its usage Only some features studied Several more features exist Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 47