Lecture 19. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 1

Similar documents
Lecture 9 Linked Lists

void set_data(const value_type& new_data) { data_field = new_data; } void set_link(node* new_link) { link_field = new_link; }

CSC212 Data Structure - Section FG

Linked Lists in Action

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

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

CS 215 Fundamentals of Programming II Spring 2018 Project 5

Linked Lists. It s a conspiracy!

Instructions PLEASE READ (notice bold and underlined phrases)

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

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)

CpSc212 Goddard Notes Chapter 10. Linked Lists

And Even More and More C++ Fundamentals of Computer Science

! 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)

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CS302 - Data Structures using C++

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

CS 215 Fundamentals of Programming II Spring 2010 Project 6. Dnode<T> structure. Specifications for Document Class. 30 points

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Review for Test 1 (Chapter 1-5)

Unit 10: Data Structures CS 101, Fall 2018

Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Beijing Jiaotong University CS-23: C++ Programming Summer, 2019 Course Syllabus

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Midterm Review. CS 211 Fall 2018

! 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)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Class Information ANNOUCEMENTS

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

Lecture 6 Dynamic Classes and the Law of the Big Three. Instructor: George Wolberg Department of Computer Science City College of New York

Linked Lists Structures

Homework 3 Grade Database Management Due Date

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

The C++ Object Lifecycle. EECS 211 Winter 2019

Intermediate Programming, Spring 2017*

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Central Washington University Department of Computer Science Course Syllabus

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

CS302 Data Structures using C++

CMSC 341 Lecture 7 Lists

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Compulsory course in Computer Science

CSC 210, Exam Two Section February 1999

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Lecture 18 Tao Wang 1

CSC212. Data Structure. Lecture 9 Templates, Iterators and STL. Instructor: George Wolberg Department of Computer Science City College of New York

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

Eric Roberts Handout #39 CS 106B February 20, 2015 Sets

PIC10B/1 Winter 2014 Exam I Study Guide

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 27 Final Exam Revision

CS Spring 2018 Homework Assignment 3 Due: 23:59, May 7, 2018

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

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

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

libqsearch A library designed for fast multiple pattern matching

Implementing Linked Lists

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

CS Fall 2018 Homework Assignment 3 Due: 23:59, Dec 26, 2018

slide 1 gaius Game Trees

CS201 Some Important Definitions

Week 8: Operator overloading

CS 241 Data Organization. August 21, 2018

CMPSCI 377 Midterm (Sample) Name:

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

Programming Assignment #4 Binary Trees in C++

CS 470 Operating Systems Spring 2013 Shell Project

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

CS 215 Fundamentals of Programming II Spring 2011 Project 2

Outline. Computer programming. Usage of time and date functions. Date and Time: time.h. Date and Time: time.h. Time functions:

SYSC 2006 CD. Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Practical C++ Programming

San José State University Department of Computer Science CS-144, Advanced C++ Programming, Section 1, Fall 2017

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

CS 202 Fundamental Structures of Computer Science II

INHERITANCE: CONSTRUCTORS,

Transcription:

Lecture 19 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture19/*.* Reminder: Homework 8 due today. Homework 9 is posted (PDF only). They are sample written midterm exam problems. It is due on Monday at the beginning of class (no late work accepted) as part of the midterm exam review. Questions? Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 1

Outline Review node struct, list_length Linked list toolkit build as we go list_write not in the textbook list_head_insert, list_insert list_search const and non-const list_locate const and non-const list_head_remove, list_remove, list_clear list_copy Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 2

Review: node Structure Recall the node structure used in linked lists. struct node { typedef double value_type; // CONSTRUCTOR node(const value_type & init_data = value_type(), node *init_link = 0) { data = init_data; next = init_link; } // note, do not need ';' here // FIELDS value_type data; data next node *next; 3.14 }; Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 3

Review: Building a Linked List Examine file list-examples.cpp The first part is the examples from the last lecture that builds a linked list by hand using pointer variables to each node. The head pointer is r. r q t p 12.1 14.6 9.3-4.8 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 4

Linked-List Toolkit Examine file node1.h In addition to the node structure, it has the prototypes for some common functions that are used to access and manipulate linked lists. Call this the linked-list toolkit. Last time we looked at list_length, a function that computes the number of nodes in a linked list given its head pointer. size_t list_length (const node *head_ptr); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 5

Linked List Toolkit Implementations for the toolkit functions are in file node1.cpp. A driver program is in file list-examples.cpp. A makefile is provided. Note that many functions are stubs and output a message saying so. This allows us to compile and run the entire program that uses them without implementing all the functions. Most the code that is missing is in the textbook using a node class; we will build the toolkit using the node struct as we go. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 6

Review: list_length Pattern for accessing every node in a linked list is a for-loop. size_t list_length (const node *head_ptr) { size_t count = 0; const node *cursor; // lcv decl for (cursor = head_ptr; // init lcv cursor!= 0; // lcv end test cursor = cursor >link; // "incr" lcv count++; // loop body return count; } Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 7

In-class Exercise: list_write list_write outputs each item in a linked list to an output stream on a separate line. It receives the head pointer and an output stream. void list_write (const node *head_ptr, std::ostream & out); Write the implementation of list_write in the file node1.cpp. Compile and run the program. All of the calls to list_write should show the same 4 items, since the rest of the toolkit is not implemented. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 8

list_head_insert A common operation on linked lists is to insert a new item at the head of the list. list_head_insert receives and passes back the head pointer (since it uses the head pointer's value and also changes it) and receives the item to be inserted. void list_head_insert (node * & head_ptr, const node::value_type & entry); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 9

In-class Exercise: list_head_insert Implement this function in node1.cpp The design for this function is 1. Create a new node with entry as its data value and head_ptr as its link. 2. Make head_ptr point to the new node Here is a picture of list_head_insert(r, 1.2): At the beginning of the function headptr 12.1 At the end of the function headptr 12.1 entry 1.2 insertptr 1.2 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 10

list_insert list_insert is used to insert items anywhere else besides the head of the list. In order to do this, it receives a pointer to the node that will be in front of the new node containing entry. Call this the previous pointer. void list_insert (node * previous_ptr, const value_type & entry); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 11

In-class Exercise: list_insert Implement this function in node1.cpp The design for this function is 1. Create a new node with entry as its data value and previous_ptr's node's link as its link. 2. Make previous_ptr's node's link point to the new node Note this works even when previous_ptr is pointing the last node in the list. A picture of list_insert(q, -7.8) is shown on the next slide. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 12

list_insert At the beginning of the function previousptr 14.6 9.3 entry 7.8 At the end of the function previousptr 14.6 9.3 insertptr 7.8 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 13

list_search list_search receives a head pointer and a target value. It returns a pointer to the first node that contains the target value or 0 if the target is not found. As with searching arrays and vectors, the design of this function is to iterate through the list and returning a pointer when the target is found. It has both const and non-const versions. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 14

list_locate list_locate receives a head pointer and a position. It returns a pointer to the node at that position, or 0 if there is no such position. Per the specifications in the textbook, item positions start at 1 (not 0). This function uses an assert to ensure that position > 0. The design of this function is to iterate through the list until the position is found or the end of the list is reached. It has both const and non-const versions Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 15

list_head_remove As with inserting, removing an item at the head of a list is different than removing an item anywhere else in the list. list_head_remove receives and passes back the head pointer of a list. The result of the function is that the head pointer now points to what was the second item in the list, and the removed node is deallocated. void list_head_remove (node * & head_ptr); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 16

In-class Exercise: list_head_remove Implement this function in node1.cpp The design of this function is 1. Make the head pointer point to the second node in the list 2. Deallocate the first node Here is a picture of list_head_remove(r): At the beginning of the function head_ptr At the end of the function remove_ptr head_ptr 1.2 12.1 1.2 12.1 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 17

list_remove list_remove is used to remove items anywhere else besides the head of the list. As with list_insert, it receives a previous pointer to the node that is in front of the node being removed. The result of this function is previous_ptr's link now points to the node after the one it originally pointed to, and the removed node is deallocated. Note this assumes that previous_ptr is not pointing to the tail node. void list_remove (node * previous_ptr); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 18

In-class Exercise: list_remove Implement this function in node1.cpp The design of this function is 1. Make the previous_ptr's link point to the node after the one it currently points to 2. Deallocate the removed node Here is a picture of list_remove(t): At the beginning of the function previousptr At the end of the function previousptr removeptr 9.3 4.8 9.3 4.8 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 19

list_clear list_clear receives a head pointer. It deallocates all of the nodes in the list and sets the head to 0. This is useful for the destructor and assignment operator of classes using a linked list as an implementation. This function uses list_head_remove repeatedly until the head pointer is 0. Uncomment the implementation of list_clear in node1.cpp. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 20

list_copy The copy constructor and assignment operator of classes using a linked list as an implementation need to be able to make a copy of a linked list. list_copy receives the head pointer for a source list and passes back the head and tail pointers of the copy. Since this is a true copy, the two lists do not share any nodes. void list_copy (const node *source_ptr, node * & head_ptr, // of copy node * & tail_ptr); // of copy Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 21

In-class Exercise: list_copy Implement this function in node1.cpp The design for this function is 1. Initialize head_ptr and tail_ptr to 0. 2. Handle the case of an empty source list. 3. Create the head node of the copy and make head_ptr and tail_ptr point to it. 4. For each of the remaining source nodes, make a copy and add it at the tail of the new list. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 22