Computer Programming

Size: px
Start display at page:

Download "Computer Programming"

Transcription

1 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

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

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

4 Doubly Linked List Backward link Forward link? 1 Front of list 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

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

6 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

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

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

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

10 Comparison with a Vector 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

11 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

12 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

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 3 strings. Each string in the list is Alice in Wonderland Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

14 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

15 Finding the Size of a list #include <iostream> #include <list> Sizes: 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

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: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

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: Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

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 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

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: Ajanta Abdul Bobby Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

20 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

21 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

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: Alex Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

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: Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

24 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

25 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

26 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

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

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

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 Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 29

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; names: Bobby Alex Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 30

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

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

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 Alex Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 33

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; names: Bobby Avi Avi Alex Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 34

35 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

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 Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 36

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 Ajanta Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 37

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; names: Bobby Alex Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 38

39 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

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; names: Bobby Alex Abdul Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 40

41 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

42 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

43 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

44 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

45 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

46 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

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

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session : C++ Standard Library The string Class Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Programming using Structures Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: for statement in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick Recap

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Structure of a Simple C++ Program Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: while and do while statements in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Character Strings Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick Recap

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Function Calls and Parameter Passing Dr. Deepak B. Phatak & Dr. Supratik

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Lectures 20, 21, 22 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 A Generic Iteration

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Merge Sort in C++ and Its Analysis Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Introduction to Pointers Part 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: An Example with Sequential and Conditional Execution Dr. Deepak B. Phatak & Dr.

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Pointers and Dynamic Memory Part 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Iteration Idioms: Motivation Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recursive Functions Part B Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Arrays and Simultaneous Equations (S31 S35) Dr. Deepak B. Phatak & Dr.

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Quiz and Practice Questions on Classes Part 1 Dr. Deepak B. Phatak & Dr. Supratik

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session : Concluding Comments on C++ Standard Library Dr. Deepak B. Phatak & Dr. Supratik

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Computer Architecture Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick

More information

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

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! Winter 2013 Instructor: Hassan Khosravi Problems with Arrays and Vectors With arrays and vectors you are allocated a large space.

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Inheritance in C++ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Overview

More information

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

Template based set of collection classes STL collection types (container types) STL Collection Types Template based set of collection classes STL collection types (container types) Sequences vector - collection of elements of type T list - doubly linked list, only sequential access

More information

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

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1 CS183 Software Design Dr. Eric A. Durant Email: durant@msoe.edu Web: http://people.msoe.edu/~durant/ courses/cs183 (coming soon) Office: CC43 (enter through CC27) Phone: 277-7439 1 Textbooks Required (same

More information

SSE2034: System Software Experiment 3

SSE2034: System Software Experiment 3 SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu STL Collection Types Template based set of collection

More information

CS 215 Lecture 17 Linked lists and time complexities

CS 215 Lecture 17 Linked lists and time complexities CS 215 Lecture 17 Linked lists and time complexities Xiwei (Jeffrey) Wang 1 Department of Computer Science University of Kentucky Lexington, Kentucky 40506 23 July 2014 1 Most of the content is from the

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: An Example Program using Member Functions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Access Control and Introduction to Classes Dr. Deepak B. Phatak & Dr. Supratik

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Object-oriented Programming using Member Functions Dr. Deepak B. Phatak & Dr. Supratik

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

Data Structure. Recitation III

Data Structure. Recitation III Data Structure Recitation III Topic Binary Search Abstract Data types Java Interface Linked List Binary search Searching a sorted collection is a common task. A dictionary is a sorted list of word definitions.

More information

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

pointers + memory double x; string a; int x; main overhead int y; main overhead pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a;

More information

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Test 1 102 total points on the test. Therefore, your score is out of 102. Class average: 89.6 / 102 Distribution: Solutions are posted

More information

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

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Lecture 4, Conditional execution of instructions Friday, August

More information

Tenka1 Programmer Contest/Tenka1 Programmer Beginner Contest 2018

Tenka1 Programmer Contest/Tenka1 Programmer Beginner Contest 2018 Tenka1 Programmer Contest/Tenka1 Programmer Beginner Contest 2018 DEGwer 2018/10/27 For International Readers: English editorial starts on page 5. A: Measure #include #include

More information

PIC 10A. Lecture 23: Intro to STL containers

PIC 10A. Lecture 23: Intro to STL containers PIC 10A Lecture 23: Intro to STL containers STL STL stands for Standard Template Library There are many data structures that share similar features These data structures can be manipulated by a common

More information

Week 3: Pointers (Part 2)

Week 3: Pointers (Part 2) Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain

More information

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

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides. Lecture 21 Standard Template Library STL: At a C++ standards meeting in 1994, the committee voted to adopt a proposal by Alex Stepanov of Hewlett-Packard Laboratories to include, as part of the standard

More information

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

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

Lecture-5. STL Containers & Iterators

Lecture-5. STL Containers & Iterators Lecture-5 STL Containers & Iterators Containers as a form of Aggregation Fixed aggregation An object is composed of a fixed set of component objects Variable aggregation An object is composed of a variable

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 An important part of every container is the type of iterator it supports. This determines which algorithms can be applied to the container. A vector supports random-access iterators i.e., all iterator

More information

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

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Lists, Links, and Images in HTML Recap Introduced HTML Examined some basic tags

More information

Standard Template Library

Standard Template Library Standard Template Library Wednesday, October 10, 2007 10:09 AM 9.3 "Quick Peek" STL history 1990s Alex Stepanov & Meng Lee of HP Labs 1994 ANSI/IS0 standard Components Container class templates Iterators

More information

Computer Programming. Dr. Fahad Computer Science Deptt.

Computer Programming. Dr. Fahad Computer Science Deptt. Computer Programming Dr. Fahad Computer Science Deptt. Course Description This course provides the fundamental programming concepts. The emphasis will be on problem solving rather than just learning the

More information

lecture09: Linked Lists

lecture09: Linked Lists lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra

More information

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

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately, erasing items

More information

CS197c: Programming in C++

CS197c: Programming in C++ CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html Administration HW1 will be up this afternoon Written assignment Due in class next week See website

More information

Chapter 17 - Notes Recursion

Chapter 17 - Notes Recursion Chapter 17 - Notes Recursion I. Recursive Definitions A. Recursion: The process of solving a problem by reducing it to smaller versions of itself. B. Recursive Function: A function that calls itself. C.

More information

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 17 November 1, 2011 CPSC 427a, Lecture 17 1/21 CPSC 427a, Lecture 17 2/21 CPSC 427a, Lecture 17 3/21 A bit of history C++ standardization.

More information

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

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp 1 CS 103 Unit 15 Doubly-Linked Lists and Deques Mark Redekopp 2 Singly-Linked List Review Used structures/classes and pointers to make linked data structures Singly-Linked Lists dynamically allocates each

More information

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

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

List Iterator Implementation

List Iterator Implementation List Iterator Implementation Lecture 28 Section 14.6 Robb T. Koether Hampden-Sydney College Fri, Apr 10, 2015 Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 1 /

More information

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

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed

More information

19.1 The Standard Template Library

19.1 The Standard Template Library Chapter 19: The Template Library From a review of Effective STL : 50 Specific Ways to Improve Your Use of the Standard Template Library by ScottMeyers: It s hard to overestimate the importance of the Standard

More information

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

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Advanced Graphics Events Guest Lecturer: Dr. Abhiram Ranade Quick recap

More information

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

To become familiar with array manipulation, searching, and sorting. ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,

More information

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

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp 1 EE 355 Unit 11b Doubly-Linked Lists and Deques Mark Redekopp 2 Singly-Linked List Review Used structures/classes and pointers to make linked data structures Singly-Linked Lists dynamically allocates

More information

Lecture on pointers, references, and arrays and vectors

Lecture on pointers, references, and arrays and vectors Lecture on pointers, references, and arrays and vectors pointers for example, check out: http://www.programiz.com/cpp-programming/pointers [the following text is an excerpt of this website] #include

More information

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

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay CS 101 Computer Programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building Bombay Session 20 More on C++ classes Tuesday, October 11, 2011 Session

More information

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS93SI Handout 04 Spring 2006 Apr Review Answers CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly

More information

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

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators Session 5 - Pointers and Arrays Iterators Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Introduction Pointers and arrays: vestiges

More information

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Recap: handling data in files Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Recap Each

More information

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

C++ Standard Template Library

C++ Standard Template Library C++ Standard Template Library CSE 333 Summer 2018 Instructor: Hal Perkins Teaching Assistants: Renshu Gu William Kim Soumya Vasisht C++ s Standard Library C++ s Standard Library consists of four major

More information

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

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. Lectures 19, 20, 21 1. STL library examples of applications Explanations: The member function pop_back removes the last element of the controlled sequence. The member function pop_front removes the first

More information

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

C++ basics Getting started with, and Data Types. C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the

More information

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

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements Welcome to MCS 360 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while

More information

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

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

COL 100. Minor 2 Exam - Practice

COL 100. Minor 2 Exam - Practice COL 100. Minor 2 Exam - Practice Name: Entry Number: Group: Notes: Total number of questions: 4. Max Marks: 20 All answers should be written on the question paper itself. The last two sheets in the question

More information

Programming in C/C Lecture 2

Programming in C/C Lecture 2 Programming in C/C++ 2005-2006 Lecture 2 http://few.vu.nl/~nsilvis/c++/2006 Natalia Silvis-Cividjian e-mail: nsilvis@few.vu.nl vrije Universiteit amsterdam News Check announcements on the C/C++ website

More information

Data Structures CSci 1200 Test 2 Questions

Data Structures CSci 1200 Test 2 Questions Overview Data Structures CSci 1200 Test 2 Questions Test 2 will be held Thursday, March 10, 2010, 12:00-1:30pm, Darrin 308. No make-ups will be given except for emergency situations, and even then a written

More information

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

C++ 11 and the Standard Library: Containers, Iterators, Algorithms and the Standard Library:,, Comp Sci 1575 Outline 1 2 3 4 Outline 1 2 3 4 #i n clude i n t main ( ) { i n t v a l u e 0 = 5 ; // C++ 98 i n t v a l u e 1 ( 5 ) ; // C++ 98 i n t v a

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Lecture 05 - I/O using the standard library, stl containers, stl algorithms Dan Pomerantz School of Computer Science 5 February 2013 Basic I/O in C++ Recall that in C, we

More information

Exceptions, Templates, and the STL

Exceptions, Templates, and the STL Exceptions, Templates, and the STL CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 16 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/

More information

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

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects MCS 360 Lecture

More information

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

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

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming What the heck is STL???? Another hard to understand and lazy to implement stuff? Standard Template Library The standard template

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

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

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010. 1 2 3 MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010 1 2 3 efficient updates with lists At http://www.sgi.com/tech/stl/ is the Standard Template Library Programmer

More information

Chapter 5. The Standard Template Library.

Chapter 5. The Standard Template Library. Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library. 5.1. Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov,

More information

CS 103 Unit 12 Slides

CS 103 Unit 12 Slides 1 CS 103 Unit 12 Slides Standard Template Library Vectors & Deques Mark Redekopp 2 Templates We ve built a list to store integers But what if we want a list of double s or char s or other objects We would

More information

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

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

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

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

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

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes CSE 250 Final Exam Fall 2013 Time: 3 hours. Dec 11, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do not use any extra

More information

BITG 1113: Function (Part 2) LECTURE 5

BITG 1113: Function (Part 2) LECTURE 5 BITG 1113: Function (Part 2) LECTURE 5 1 Learning Outcomes At the end of this lecture, you should be able to: explain parameter passing in programs using: Pass by Value and Pass by Reference. use reference

More information

STL Standard Template Library

STL Standard Template Library STL Standard Template Library September 22, 2016 CMPE 250 STL Standard Template Library September 22, 2016 1 / 25 STL Standard Template Library Collections of useful classes for common data structures

More information

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

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 Arrays 1. Introduction An array is a consecutive group of memory locations that all have the same name and the same type. A specific element in an array is accessed by an index. The lowest address corresponds

More information

Lectures 11,12. Online documentation & links

Lectures 11,12. Online documentation & links Lectures 11,12 1. Quicksort algorithm 2. Mergesort algorithm 3. Big O notation 4. Estimating computational efficiency of binary search, quicksort and mergesort algorithms 5. Basic Data Structures: Arrays

More information

VARIABLES & ASSIGNMENTS

VARIABLES & ASSIGNMENTS Fall 2018 CS150 - Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150 - Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 19 November 7, 2018 CPSC 427, Lecture 19, November 7, 2018 1/18 Exceptions Thowing an Exception Catching an Exception CPSC 427, Lecture

More information

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

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

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)

More information

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

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name:  ID: Page 1 of 10 Name: Email ID: You MUST write your name and e-mail ID on EACH page and bubble in your userid at the bottom of EACH page including this page and page 10. If you do not do this, you will receive

More information

1a Computers, Problem Solving!

1a Computers, Problem Solving! 1a Computers, Problem Solving!! 1E3! 1a Computers and Problem Solving 1 Objectives! n To introduce the architecture of a computer.! n To introduce the notion of a programming language.! n To explore computational

More information