Data Structures and Algorithms

Size: px
Start display at page:

Download "Data Structures and Algorithms"

Transcription

1 Data Structures and Algorithms Alice E. Fischer Lecture Alice E. Fischer Data Structures L3... 1/31

2 1 Arrays Growing Arrays vectors 2 3 Empty Lists Inserting Data - Unsorted Inserting Data - Sorted 4 Alice E. Fischer Data Structures L3... 2/31

3 The Choices Outline Arrays Growing Arrays vectors Array: limited in size but simple and efficient for random access. Can be sorted. Growing Array: able to grow as needed but with a performance penalty. Linked list: able to grow as needed, sequential access only. Takes extra space to store pointers and for allocation overhead. Alice E. Fischer Data Structures L3... 3/31

4 Diagram of an array of Items Arrays Growing Arrays vectors The first data structure would result from declaring an array variable then storing six strings in it. string ar[ 8 ] ; arr Al Vi Bo Ed Oz Di?? arr Al Vi Bo Ed Oz Di?? The second diagram illustrates the result of dynamically allocating a similar array. string* ar2 = new string[ 8 ] ; Alice E. Fischer Data Structures L3... 4/31

5 Arrays Growing Arrays vectors Implementation of a growing array This is a resizeable array that expands when needed. It is intended for storing input data when the amount of data is not known ahead of time. New items are added at the end of the array, which is reallocated and enlarged when it is full. Any growing array class has these members: private: max, n (ints) and data (a pointer to a dynamically allocated array) public: void insert( item T ): Grow if necessary, then add the item to the end of the array. private: void grow(): Double the max, allocate an array using the new value of max. Copy the data into it. The growing array class that is part of C++ is vector. Alice E. Fischer Data Structures L3... 5/31

6 Diagram of a Growing Array Arrays Growing Arrays vectors This is a vector<string> that is currently big enough to contain 8 strings and does contain 5 strings. It just doubled in size because the original container of length 4 was too small to hold the fifth string. Any type or class could be substituted for string. data vec 1 2 3???? vec Al Vi Bo Ed max 4 4 n 4 vec Al Vi Bo Ed Oz??? 8 5 Alice E. Fischer Data Structures L3... 6/31

7 Arrays Growing Arrays vectors vector Constructors and basics Here is a list of the most important vector::iterator functions. In this chart, BT stands for the base type of the array, bto stands for an object of that type, and vc stands for the vector object. vector< BT > vc; vector< BT >:: iterator p1, pos; vc.push back( bto ) vc.pop back() vc.clear() k = (int) vc.size() Construct empty vector. Create iterators to point into vector. Insert object at end; grow if necessary. Remove and discard last object. Discard all elements. Return # of data objects in vc. Alice E. Fischer Data Structures L3... 7/31

8 Vector and Iterators. Outline Arrays Growing Arrays vectors In this chart, BT stands for the base type of the array, bto stands for an object of that type, and vc stands for the vector object. bto = vc[k] bto = vc.front() bto = vc.back() p1 = vc.begin(); p2 = vc.end(); p1++, ++p2 p2, p2 pos = find( p1, p2, bto ); if (pos == vc.end()) vc.sort (p1, p2) Return the contents of the kth slot. Return the first element in the vector. Return the last element in the vector. Point to first element in vector. Point to first unused address. Increment iterator. Decrement iterator. Return position of bto or vc.end() has iterator reached end of vector? Sort the elements of vc Alice E. Fischer Data Structures L3... 8/31

9 Diagram of a Linked List A linked list is composed of cells and pointers. This list contains 4 Items. The last Cell in the list points to null. The member named count is useful, but optional. scan and follow are used for searching the list. Larry head count 4 Al Ed Oz Vi scan follow A linked list can grow easily. Items can be added/removed in the middle of the list efficiently. However, random access is impossible and sequential access is slow. Alice E. Fischer Data Structures L3... 9/31

10 Construct an empty linked list Empty Lists Inserting Data - Unsorted Inserting Data - Sorted The List constructor should set the head pointer to null (since there is no data yet) and the count to. The utility pointers, scan and follow, may also be set to null, for completetness. Larry head count scan follow Alice E. Fischer Data Structures L3... 1/31

11 Clear all Data out of a List. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted This operation would be done after using all the data in an existing list and prior to re-using a List object. Before: Larry has one or more entries: Larry head count 4 scan follow Al Ed Oz Vi Larry After: The entries are gone, Larry is empty. The programmer must delete each of the four cells. head count scan follow Alice E. Fischer Data Structures L /31

12 Add Data to an Unsorted List. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted Since the list is not sorted, data is always entered at the head of the list because that is fastest. Step 1: Call Entry constructor with the value of the new data object and the current contents of head. Step 2: Make Larry's head point at the new object and increment Larry's count. Vi Larry head count 1 scan follow Vi Larry Step 3: Add another. head count 2 Oz Vi scan follow Alice E. Fischer Data Structures L /31

13 Add Data to a Sorted List. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted Since the list is sorted, data must be inserted in its proper place. Thus, the insertion process has two steps: a) find the right place and b) insert the cell. Let us focus first on finding the right place. Step 1: Let scan = head and follow = nullptr. Larry head count scan follow 4 Al Ed Oz Vi Given these initial assignments, we are ready to search the list. Alice E. Fischer Data Structures L /31

14 Add Data to a Sorted List. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted Suppose we want to insert Li into the list. Step 2: Compare scan's data to the new data. If the new data is larger, walk one step down the list. Larry head count 4 scan follow Al Ed Oz Vi Step 3: To go one step, set follow = scan, then set scan = scan->next. Larry head count 4 scan follow Al Ed Oz Vi Alice E. Fischer Data Structures L /31

15 Finding the Insertion Location. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted Repeat steps 2 and 3 until scan's data is larger than the new data, then stop. Larry head count scan follow 4 Al Ed Oz Vi If scan reaches the end of the list, it must stop. If follow is still null when scan stops, the insertion slot is at the head of the list. Otherwise, insertion is in the middle of the list. Alice E. Fischer Data Structures L /31

16 Do the Actual Insertion. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted Step 4. Construct a new cell with the new data and a copy of scan in it. Larry head count 4 scan follow Al Ed Oz Vi Li Step 5. Attach follow->next to the new cell. Larry head count 4 scan follow Al Ed Oz Vi Li Alice E. Fischer Data Structures L /31

17 Do the Actual Insertion. Empty Lists Inserting Data - Unsorted Inserting Data - Sorted The insertion is now done. Set scan and follow back to nullptr. Larry head count 4 scan follow Al Ed Oz Vi Li Homework: How would you do a deletion? Explain it. Supply diagrams. Due in 1 week. Alice E. Fischer Data Structures L /31

18 Design the Capabilities, Interface, and Semantics. First, a programmer must decide what he wants his data structure to do. The functions for a sorted linked-list are different from a sorted array because accessing and allocating patterns are different. Without careful planning the fixes could go on for a long time. Semantics: The list is either empty or head points at a cell. Functions needed: size, isempty, insert, remove, find, print. Iterate through the list. You can start an iteration at the beginning of the list and get to the end. The current state of the iteration is stored internally in the list. You can find a value stored anywhere in the list. The position of that value s cell and the prior cell are stored internally in the list. Alice E. Fischer Data Structures L /31

19 UML: We need a class and a helper class because a list has manycells. SList - head: Cell* - scan: Cell* - follow: Cell* - count: int + List() + ~List() + size(): int + isempty(): bool + next(): const string* + start(): void + find(string): void + insert(string): void + remove(string): void + print(ostream&):void * Cell - data: string - next: Cell* - Cell(string, Cell*) friend class List Alice E. Fischer Data Structures L /31

20 Notes on the UML diagram Each class in the application is represented by a box. Boxes have 3 layers: class name, data members, function members. Sometimes a fourth layer is added giving other essential and relevant facts. For example, a friend relationship can be noted there. On the left, a labels a private member, a + represents public, and a # represents protected. The return type of a function is given at the right side of a box. Boxes are connected by links that represent the relationship between two classes. Alice E. Fischer Data Structures L3... 2/31

21 Composition vs. Association A link has two ends. Each end can be decorated, as follows: A black diamond on one end represents composition. The class on the diamond end has, as part of itself, an instance of the class on the other end. (As a table has a top as part of itself.) These objects live in one contiguous area of memory. They are created, initialized, and freed together. A plain end, with no diamond, represents association. First, the primary object is created. Later, a dependent object is created and a pointer to it is stored in the primary object. So in this diagram, an SList can be associated with a Cell. SList is the primary object and Cell the dependent object. Alice E. Fischer Data Structures L /31

22 Links on the UML diagram A link has two ends. Each end can be decorated, as follows: An asterisk: this represents a possibility of many instances of the class on that end. A number: this represents a known and fixed number of instances of the class on that end. No number, no asterisk: one instance. So in this diagram, one SList is associated with anywhere between zero and many cells. Alice E. Fischer Data Structures L /31

23 Tightly-Coupled Classes Sometimes, you need two classes to describe one data structure, particularly when there is a 1-to-many relationship between two classes. Neither class is useful without the other. We implement a tightly-coupled pair of classes in such a way that the primary class is the only class able to use or manipulate parts of the dependent class. The dependent class can be defined inside the primary class, as a private member, and all members of the dependent class must then be public. The dependent class can be a separate class, all of whose members are private, and it can give friendship to the primary class. Friendship gives the primary class access to all the private parts of the dependent class. Here, Cell gives friendship to SList, and SList manages all the cells. Alice E. Fischer Data Structures L /31

24 The SList has Internal State The two-pointer scan supports all list operations. Scan and follow are class members so that they can be used to communicate from one class function to the next. The find() function sets scan and follow for use by other functions. This permits all functions to be short and clear. find() stops on a <= comparison. Thus, scan->value is either greater than the value you are looking for, or they are equal. Follow always trails one cell behind scan. All operations that change the list or the internal state must be careful about the head and the tail of the list. Alice E. Fischer Data Structures L /31

25 After calling find().... After calling find(), a function must check for and handle relevant special cases: 1 Scan will be nullptr if the list is empty. Caution: A program must check scan before trying to use it 2 Scan will be nullptr if find() reached the end of the list. 3 Follow will be nullptr if find() stopped at the head of the list. In this case, an insertion would go at the head of the list. insert() and getval() have special cases for (1) and (2). remove needs to test (1) and (3). Alice E. Fischer Data Structures L /31

26 The Cell Class. Outline This is a private class, only accessible to SList. Note the ctors. The SList class declaration follows in the same file. #include "tools.hpp" typedef string BT; // class Cell { friend class SList; private: BT data; Cell* next; // Cell(BT dt, Cell* nx=nullptr) : data(dt), next(nx){} }; Alice E. Fischer Data Structures L /31

27 The SList class Outline Data members and inline functions: class SList { private: Cell* head = nullptr; int count = ; Cell* scan = nullptr; Cell* follow = nullptr; // Permanent head pointer. // The # of items currently. // for walking down list. public: SList() = default; int size(){ return count; } bool isempty(){ return count==; } Alice E. Fischer Data Structures L /31

28 The SList class Outline Functions defined in the.cpp file: ~SList(); void start(); // Position scan at head. const BT* next(); // Return scan->data, go next. void find( BT value ); // Position scan and follow void insert( BT value ); // Calls find(). void remove( BT value ); // Calls find(). void print ( ostream& out ) const; }; Some of these are given below. Others are left for the student to complete. Alice E. Fischer Data Structures L /31

29 Destructor for SLists. All memory created with new must be freed. // SList::~SList() { for(;;) { scan = head; if (scan == nullptr ) break; head = head->next; delete scan; } } Alice E. Fischer Data Structures L /31

30 SList::print Outline List must provide the interface for itself and for the helper class. // // Print contents of all Cells in the List. void SList::print ( ostream& out ) const { out <<" \n"; Cell* s = head; while ( s!= nullptr ) { out << s->data << endl; s = s->next; } out <<"================\n"; } Alice E. Fischer Data Structures L3... 3/31

31 SList::remove Outline void SList::remove( BT val ) { find( val ); if (scan == nullptr scan->data!= val ) cout <<endl <<val <<" is not in the list.\n\n"; else { --count; if(follow == nullptr) { } } head = head->next; } else { follow->next = scan->next; } delete scan; delete scan; Alice E. Fischer Data Structures L /31

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 4, 2013 OOPP / C++ Lecture 6... 1/34 Initialization: TestInit Disasters Failure: Symptoms, Causes, and Cures End of File and Error Handling

More information

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19 Data Structures Alice E. Fischer Lecture 4, Fall 2018 Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall 2018 1 / 19 Outline 1 Ordered Lists 2 Sorted Lists Tail Pointers 3 Doubly Linked Lists

More information

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009. CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions The Midterm Exam was given in class on Wednesday, March 18, 2009. 1. [4 pts] Parameter Passing in C++. In the table below, the

More information

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009. CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions The Midterm Exam was give in class on Wednesday, October 21, 2009. 1. [4 pts] When we define a (non-template) class, we generally

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

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer June 3, 2013 OOPP / C++ Lecture 9... 1/40 Const Qualifiers Operator Extensions Polymorphism Abstract Classes Linear Data Structure Demo Ordered

More information

Sudoku-4: Logical Structure

Sudoku-4: Logical Structure Sudoku-4: Logical Structure CSCI 4526 / 6626 Fall 2016 1 Goals To use an STL vector. To use an enumerated type. To learn about tightly coupled classes. To learn about circular dependencies. To use the

More information

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 ... 1/16 CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 Alice E. Fischer February 3, 2016 ... 2/16 Outline Basic Types and Diagrams ... 3/16 Basic Types and Diagrams Types in C C has

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to

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

CSCI 2212: Intermediate Programming / C Working with Pointers

CSCI 2212: Intermediate Programming / C Working with Pointers ... 1/26 CSCI 2212: Intermediate Programming / C Working with Pointers Alice E. Fischer November 13, 2015 ... 2/26 Outline Using Pointers with Arrays Pointers to Functions ... 3/26 Using Pointers with

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer September 26, 2016 OOPP / C++ Lecture 4... 1/33 Global vs. Class Static Parameters Move Semantics OOPP / C++ Lecture 4... 2/33 Global Functions

More information

CSCI 2212: Intermediate Programming / C Recursion

CSCI 2212: Intermediate Programming / C Recursion ... 1/40 CSCI 2212: Intermediate Programming / C Recursion Alice E. Fischer November 13 and 16, 2015 ... 2/40 Outline What is Recursion? Recursively Defined Images What is a Recursive Function? How Does

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators,

More information

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators Review from Lecture 7 Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

PART V. Dynamic Data Structures

PART V. Dynamic Data Structures PART V Dynamic Data Structures 496 Copyright c 2016 by Alice E. Fischer, David W. Eggert, and Michael J. Fischer All rights reserved. No part of this manuscript may be reproduced, stored in a retrieval

More information

LECTURE 03 LINKED LIST

LECTURE 03 LINKED LIST DATA STRUCTURES AND ALGORITHMS LECTURE 03 LINKED LIST IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD LINKED LISTS DEFINITION A linked list is a data structure where each object is stored in

More information

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University C++ Review CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose of Review Review some basic C++ Familiarize us with

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Review from Lecture 7 CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

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

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2 CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.

More information

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; } Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3;

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 13, 2013 OOPP / C++ Lecture 7... 1/27 Construction and Destruction Allocation and Deallocation Move Semantics Template Classes Example:

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1 CSE 143 Linked Lists [Chapter 4; Chapter 6, pp. 265-271] Linked Lists A linked list is a collection of dynamically allocated nodes Each node contains at least one member (field) that points to another

More information

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren CS32 Discussion Sec.on 1B Week 2 TA: Zhou Ren Agenda Copy Constructor Assignment Operator Overloading Linked Lists Copy Constructors - Motivation class School { public: }; School(const string &name); string

More information

7.1 Optional Parameters

7.1 Optional Parameters Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer June 17, 2013 OOPP / C++ Lecture 9... 1/37 Abstract Classes Multiple Inheritance Template Example Casts Handling Circularly Dependent Classes

More information

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

Vector. Vector Class. class Vector { public: typedef unsigned int size_type; Vector Arrays in C++ must be pre-allocated to accomodate the maximum number of elements. Should an application attempt to add more than this number, the program may abort, or alter storage of adjacent

More information

Part I: Short Answer (12 questions, 65 points total)

Part I: Short Answer (12 questions, 65 points total) CSE 143 Sp01 Final Exam Sample Solution page 1 of 14 Part I: Short Answer (12 questions, 65 points total) Answer all of the following questions. READ EACH QUESTION CAREFULLY. Answer each question in the

More information

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes Scott Gibson Pointers & Dynamic Memory Lecture #1 Office: LAH 103 Email: sgibson@brookdalecc.edu sgib@optonline.net Web: www.brookdalecc.edu/fac/cos/sgibson Phone: (732) 224 2285 1 2 Pre & Co Requisites

More information

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

CpSc212 Goddard Notes Chapter 10. Linked Lists

CpSc212 Goddard Notes Chapter 10. Linked Lists CpSc212 Goddard Notes Chapter 10 Linked Lists 10.1 Links and Pointers The linked list is not an ADT in its own right; rather it is a way of implementing many data structures. It is designed to replace

More information

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

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

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation Announcements Lab 3 was a Frankenstein assembly of a new group exercise with part of a gdb lab. I hear many of you

More information

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays Unit 3: Linked Lists Part 2: More on Linked Lists 1 Deletion in singly linked lists (cont d) 1 Other Functions Engineering 4892: Data Structures 1 Doubly Linked Lists Faculty of Engineering & Applied Science

More information

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland May 6, 2010 ENGI

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 B Cynthia Lee Topics du Jour: Make your own classes! (cont.) Last time we did a BankAccount class (pretty basic) This time we will do something more like the classes

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

Faculty of Information and Communication Technologies

Faculty of Information and Communication Technologies Swinburne University Of Technology Faculty of Information and Communication Technologies ASSIGNMENT COVER SHEET Subject Code: Subject Title: Assignment number and title: Due date: Lecturer: HIT3303 Data

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

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance

More information

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Today CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Another vector operation: pop back Erasing items from vectors is inefficient! Iterators and iterator operations

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

CSCI-1200 Data Structures Test 3 Practice Problem Solutions

CSCI-1200 Data Structures Test 3 Practice Problem Solutions 1 Short Answer [ /21] CSCI-1200 Data Structures Test 3 Practice Problem Solutions 1.1 TreeNode Parent Pointers [ /5] In our initial version of the ds set class, the TreeNode object had 3 member variables:

More information

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

CMPT 135: Midterm Answer Key

CMPT 135: Midterm Answer Key CMPT 135, Spring 2017, Surrey Midterm Answer Key Page 1 of 6 CMPT 135: Midterm Answer Key Last name exactly as it appears on your student card First name exactly as it appears on your student card Student

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

CS 2604 Homework 1 Greatest Hits of C++ Summer 2000

CS 2604 Homework 1 Greatest Hits of C++ Summer 2000 Instructions: This homework assignment covers some of the basic C++ background you should have in order to take this course. I. Pointers Opscan forms will be passed out in class and will be available at

More information

Spring 2008 Data Structures (CS301) LAB

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

More information

Objects Managing a Resource

Objects Managing a Resource Objects Managing a Resource 1 What is a Resource Respects Release/Acquire protocol files (open/close) memory allocation (allocate/free) locks (acquire/release). 2 What is a Resource Objects when constructed,

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 12 October 10, 2016 CPSC 427, Lecture 12 1/27 Uses of Pointers Custody of Objects Move Semantics CPSC 427, Lecture 12 2/27 Uses of Pointers

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer October 10, 2016 OOPP / C++ Lecture 7... 1/15 Construction and Destruction Kinds of Constructors Move Semantics OOPP / C++ Lecture 7... 2/15

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

Part 3: Function Pointers, Linked Lists and nd Arrays

Part 3: Function Pointers, Linked Lists and nd Arrays Part 3: Function Pointers, Linked Lists and nd Arrays Prof. Dr. Ulrik Schroeder C++ - Einführung ins Programmieren WS 03/04 http://learntech.rwth-aachen.de/lehre/ws04/c++/folien/index.html Pointer to functions

More information

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Thursday, Sept 20th, 2018 from 6-7:50pm Students will be randomly

More information

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000 Instructions: This homework assignment covers some of the basic C++ background you should have in order to take this course. I. Pointers Opscan forms will be passed out in class. Write your name and code

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

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

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3 PIC 10B Lecture 1 Winter 2014 Homework Assignment #3 Due Tuesday, February 4, 2014 by 5:00pm. Objectives: 1. To redefine the Big 3 : Copy constructor, Assignment operator, and Destructor for a class whose

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 13 October 15, 2018 CPSC 427, Lecture 13, October 15, 2018 1/18 CPSC 427, Lecture 13, October 15, 2018 2/18 CPSC 427, Lecture 13, October

More information

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 10 Pointers and Dynamic Arrays Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic

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

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm G205 Fundamentals of Computer Engineering CLASS 3, Wed. Sept. 15 2004 Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm Function Calls A function is called via its name Actual parameters are passed for the

More information

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation Outline EDAF30 Programming in C++ 4. The standard library. Algorithms and containers. Sven Gestegård Robertz Computer Science, LTH 2018 1 Type inference 2 3 The standard library Algorithms Containers Sequences

More information

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1 CSE 1 Complexity Analysis Program Efficiency [Sections 12.1-12., 12., 12.9] Count number of instructions executed by program on inputs of a given size Express run time as a function of the input size Assume

More information

CS 2604 Homework 1 C++ Review Summer I 2003

CS 2604 Homework 1 C++ Review Summer I 2003 Instructions: This homework assignment covers some of the basic C++ background you should have in order to take this course. You will submit your answers to the Curator system. I. Pointers and Memory Management

More information

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016 Array Lists Lecture 15 Robb T. Koether Hampden-Sydney College Mon, Feb 22, 2016 Robb T. Koether (Hampden-Sydney College) Array Lists Mon, Feb 22, 2016 1 / 23 1 Inlining Functions 2 List Implementations

More information

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science Written examination Homologation C++ and Computer Organization (2DMW00) Part I: C++ - on Tuesday, November 1st 2016, 9:00h-12:00h.

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements Submitty iclicker registration is still open. Even if you already registered on the iclicker website,

More information

8.1 The Roles of a Class

8.1 The Roles of a Class Chapter 8: Interacting Classes 8. The Roles of a Class Three more basic OO design principles: A class is the expert on its own members. Oil and water don t mix. Delegate! Simplify! In this chapter we begin

More information

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List Review: Implementations CSE 143 Java ed s Reading: Ch. 23 The external interface is already defined Implementation goal: implement methods efficiently Array approach: use an array with extra space internally

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions: VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted

More information

CS24 Week 4 Lecture 2

CS24 Week 4 Lecture 2 CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track

More information

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14. Lecture 14 1 Exceptions Iterators Random numbers Casting Enumerations Pairs The Big Three Outline 2 Error Handling It is often easier to write a program by first assuming that nothing incorrect will happen

More information

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure representing a list! A series of nodes chained together in sequence CS 2308 Spring 2013 Jill Seaman - Each node points to one other

More information

Tree Travsersals and BST Iterators

Tree Travsersals and BST Iterators Tree Travsersals and BST Iterators PIC 10B May 25, 2016 PIC 10B Tree Travsersals and BST Iterators May 25, 2016 1 / 17 Overview of Lecture 1 Sorting a BST 2 In-Order Travsersal 3 Pre-Order Traversal 4

More information

Dynamic Allocation of Memory

Dynamic Allocation of Memory Dynamic Allocation of Memory Lecture 4 Sections 10.9-10.10 Robb T. Koether Hampden-Sydney College Fri, Jan 25, 2013 Robb T. Koether (Hampden-Sydney College) Dynamic Allocation of Memory Fri, Jan 25, 2013

More information

Pushing at the back 28/11/2018. Problem. Our linked list class

Pushing at the back 28/11/2018. Problem. Our linked list class ECE 150 Fundamentals of Programming Outline 2 In this lesson, we will: Understand how to push a new entry onto the back of the linked list Determine how we can speed this up Understand that the cost is

More information

Instructions. Quiz #2. Name: Solutions Student Number: Signature:

Instructions. Quiz #2. Name: Solutions Student Number: Signature: Quiz #2 Name: Solutions Student Number: Signature: Instructions 1. Fill in your Name, Student Number, and signature above. 2. This is a closed book Quiz. No electronic or paper aids permitted. 3. Do not

More information