CS24 Week 4 Lecture 1

Similar documents
CS24 Week 4 Lecture 2

CS24 Week 3 Lecture 1

Common Misunderstandings from Exam 1 Material

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

CS302 - Data Structures using C++

Data Structures and Algorithms

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

CS24 Week 2 Lecture 1

Introduction to Linked Data Structures

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

ComS 228 Exam 1. September 27, 2004

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Linked List using a Sentinel

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Protection Levels and Constructors The 'const' Keyword

CS 247: Software Engineering Principles. ADT Design

Spare Matrix Formats, and The Standard Template Library

Data Structure (CS301)

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

Today s lecture. CS 314 fall 01 C++ 1, page 1

CS64 Week 5 Lecture 1. Kyle Dewey

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

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Faculty of Information and Communication Technologies

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

CS3157: Advanced Programming. Outline

Encapsulation in C++

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)

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

September 10,

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

Linked Lists. Chapter 4

An Introduction to C++

CS250 Final Review Questions

Constants, References

Lecture 7. Log into Linux New documents posted to course webpage

CS24 Week 8 Lecture 1

CMSC 341 Lecture 7 Lists

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

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

CS1004: Intro to CS in Java, Spring 2005

G52CPP C++ Programming Lecture 9

Compiler aided optimization of the pimpl-idiom

Unit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

COEN244: Class & function templates

CS 32. Lecture 2: objects good?

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

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

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

Programming, numerics and optimization

Do not write in this area TOTAL. Maximum possible points: 75

Unit Testing. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Review Questions for Final Exam

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Recitation 7 Caches and Blocking. 9 October 2017

CITS1001 week 4 Grouping objects

"apple" "grape" "grape" "grape" "apple"

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

LECTURE 03 LINKED LIST

CSE 214 Computer Science II Java Classes and Information Hiding

Link-Based Implementations. Chapter 4

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Do not open this examination paper until instructed to do so. Answer all the questions.

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

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

Chapter 17: Linked Lists

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

DAY 3. CS3600, Northeastern University. Alan Mislove

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Programming I Part 3 Algorithms and Data Structures

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

ENCM 339 Fall 2017 Tutorial for Week 8

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Largest Online Community of VU Students

Abstract Data Types - Lists Arrays implementation Linked-lists implementation

Arrays and Linked Lists

Week 8: Operator overloading

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

CCReflect has a few interesting features that are quite desirable for DigiPen game projects:

Goals of this Lecture

Short Notes of CS201

CS250 Final Review Questions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Chapter 17: Linked Lists

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

CS201 - Introduction to Programming Glossary By

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

C++ Programming Lecture 4 Software Engineering Group

Transcription:

CS24 Week 4 Lecture 1 Kyle Dewey

Overview Additional use of const in C++ List ADT Array Lists Linked Lists

Additional Use of const We ve seen const already in two positions: What is pointed to is constant The pointer itself is constant void foo(const char* const s) { s[0] = a ; // disallowed s = NULL; // disallowed }

Additional Use of const We can also tag whole methods with const, indicating that they may not change any state of the class they are called on Great for accessors, as opposed to mutators class Foo { public: Foo(int a) { b = a; } void setvalue(int a) { b = a; } int getvalue() const { return b; } private: int b; };

this

this Allows one to refer to the object being acted upon in a method call, via a pointer class Foo { private: int x; public: int getx() { return this->x; } };

List ADT

Motivation We often work with a series of items Addresses in a phone book, cards in a deck, etc. Arrays can be painful Fixed size Error-prone (e.g., index too large) Repeated similar operations

Idea: A List ADT Handles the storage of elements and the addition of elements Holds common operations (e.g., checking if an item is contained within) Can protect against out-of-bounds

A List ADT What should the List ADT have at the logical/abstract/interface level?

A List ADT What should the List ADT have at the logical/abstract/interface level? Basic examples: get item, add item, insert item at a position, remove item, get size Many, many more examples possible

Idealized List ADT List emptylist(); int getsize(); int getint(int position); bool containsint(int item); void addint(int item); void addintatposition(int item, int position); void removefirstint(int item);

Implementing in C++ Classes? Constructors? Methods? Which methods should be marked const? List emptylist(); int getsize(); int getint(int position); bool containsint(int item); void addint(int item); void addintatposition(int item, int position); void removefirstint(int item);

Implementing in C++ Classes? Constructors? Methods? Which methods should be marked const? List emptylist(); // Constructor int getsize() const; int getint(int position) const; bool containsint(int item) const; void addint(int item); void addintatposition(int item, int position); void removefirstint(int item);

Implementing in C++ For now, let s implement this via an array What other issues are present because of this design decision?

Implementing in C++ For now, let s implement this via an array What other issues are present because of this design decision? Size of the array? Accessing out-of-bounds element? Adding an element in the middle? How might we handle each?

Implementation in C++

Array-Based List What sort of operations were hard because arrays were used?

Array-Based List What sort of operations were hard or awkward because arrays were used? Constructor needed an array size Adding an element at an arbitrary position required pushing elements to the right Removing an element required pushing elements to the left

Other Approaches How might we improve on these issues? (Fixed size, making arbitrary addition and removal easier)

Other Approaches How might we improve on these issues? (Fixed size, making arbitrary addition and removal easier) Wide variety of answers Approach we will take: linked lists

Fixed Size Observation: with arrays, we must allocate in blocks We must pre-allocate room, and expanding this room is obnoxious We would like to allocate as we go along, in a piecewise fashion

Piecewise Allocation How can we represent the list in a way that makes piecewise allocation possible? (Not just extending onto an array)

Piecewise Allocation How can we represent the list in a way that makes piecewise allocation possible? (Not just extending onto another array) Piecewise implies separate chunks that hold onto single elements How do we keep track of chunks?

Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track of only the head node List: 0, 1, 2, 3 0 1 2 3 X

Node Representation What might a node look like in C/C++?

Node Representation What might a node look like in C? struct Node { int item; struct Node* next; };

Node Representation What might a node look like in C++? class Node { public: Node(int i, Node* n); int getitem() const; void setitem(int i); Node* getnext() const; void setnext(node* n); private: int item; Node* node; };

C++ Implementation of Linked Lists