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

Similar documents
Linked Lists CS 16: Solving Problems with Computers I Lecture #16

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

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

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

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

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Pre- Defined Func-ons in C++ Review for Midterm #1

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Welcome to Solving Problems with Computers I

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

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

Dynamic Arrays Makefiles and Mul3ple File Compiles

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

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

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

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

File I/O CS 16: Solving Problems with Computers I Lecture #9

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

MIPS and Basic Assembly Language CS 64: Computer and Design Logic Lecture #4

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

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

Computer Science II CSci 1200 Sections 1-4,6 Week 6, Friday Class October 5, 2001 Linked Lists, Part 2

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

CSc 2310 Principles of Programming (Java) Jyoti Islam

CMSC 341 Lecture 7 Lists

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Ch. 17: Linked Lists. Introduction to Linked Lists

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

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

Unit 10: Data Structures CS 101, Fall 2018

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

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

Midterm Review. CS 211 Fall 2018

CS3: Introduction to Symbolic Programming. Lecture 5:

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

lecture09: Linked Lists

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

.:: UNIT 3 ::. LIST AND LINKED LIST

Welcome to Computer Organization and Design Logic

More Examples Using Lists Tuples and Dictionaries in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #11

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

Autumn 2012 November 7 th 9 th, 2012 Section Solution

CS32 Discussion Week 3

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

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

CS 241 Data Organization using C

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Data Structures and Algorithms

Homework Assignment #1

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

CSE 100: GRAPH SEARCH

EECE.2160: ECE Application Programming

Linked List using a Sentinel

CS Introduction to Programming Fall 2016

Linked Lists. Chapter 4

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

Algorithms & Datastructures Laboratory Exercise Sheet 1

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

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

Computer Science II CSci 1200 Test 2 Overview and Practice

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

Introduction to Computer Systems

CS 241 Data Organization. August 21, 2018

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

== isn t always equal?

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Transcription:

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17 Ziad Matni Dept. of Computer Science, UCSB

Administrative Homework situation: Labs: NO MORE HOMEWORK! J Lab10 due on Friday 6/5/18 Matni, CS16, Sp18 2

Material: Everything! Homework, Labs, Lectures, Textbook Tuesday, 6/12 in this classroom Starts at 4:00pm **SHARP** Duration: 3 hours long BRING YOUR UCSB IDs PLEASE! Arrive 10-15 minutes early Closed book: no calculators, no phones, no computers Only 1 sheet SINGLE-SIDED of written notes Must be no bigger than 8.5 x 11 You have to turn it in with the exam You will write your answers on the exam sheet itself.

Lecture Outline More exercises using pointers and ed lists 6/5/18 Matni, CS16, Sp18 4

Exercise Example 1 We ve already demonstrated how to add nodes to a LL, but what about deleting them? 6/5/18 Matni, CS16, Sp18 5

Figure Out the Algorithm! Regular case: h -> A -> B -> C -> NULL How do I remove B from the LL? And get to: h -> A -> C -> NULL??? 6/5/18 Matni, CS16, Sp18 6

Algorithm for Deletion h -> -> -> -> NULL C A B 1. Find the node to delete a) Either by the value (or one of the values) in the node b) Or by its position in the ed list 2. Get a pointer to point to that node (call it current) 3. Get a pointer to point to the node before it (call it previous) 6/5/18 Matni, CS16, Sp18 7

Algorithm for Deletion h -> A -> B -> C - > NULL previous current 4. Have previous-> be pointing to what s after current 5. Should I make current-> point to NULL? What happens to Node B?? h -> A B previous current C -> NULL You have to de-allocate it from memory Use: delete(current) 6/5/18 Matni, CS16, Sp18 8

Edge Cases Will our algorithm work for ALL cases of a ed list? What about: 1. The node to delete is a the start of the ed list? 2. The node to delete is a the tail of the ed list? 3. If the ed list has only ONE component? 4. If the ed list has NO components (h -> NULL)? 5. If I CAN T FIND my intended node to delete? Other situations??? 6/5/18 Matni, CS16, Sp18 9

Edge Case 1 Case of: h -> (DeleteThis) -> NodeX -> NodeY etc Can I just skip the first node in a simple way? Yes! So it s a special case 6/5/18 Matni, CS16, Sp18 10

Edge Case 2 Case of: h -> NodeX -> NodeY -> (DeleteThis) -> NULL Can I make previous = pointer to NodeY? Can I make current = pointer to DeleteThis node? Yes and yes So no special case Recall: 4. Have previous-> be pointing to what s after current 6/5/18 Matni, CS16, Sp18 11

Edge Case 3 Case of: h -> (DeleteThis) -> NULL Is this different from Case 1? No 6/5/18 Matni, CS16, Sp18 12

Case of: h -> NULL Should I even try? No How do I check for this? Hmmmm. Edge Case 4 Special case 6/5/18 Matni, CS16, Sp18 13

Edge Case 5 What if the search criteria fails? I cannot find a node at that position I cannot find a node value equal to my target value Sounds like a modification to my while loop Would the requirements for edge case 4 fit into this? Yes 6/5/18 Matni, CS16, Sp18 14

Entire Algorithm 1. Have head and target defined (passed into function) 2. Create 2 pointers to nodes: current = previous = head 3. If (head == NULL): a) Empty list nothing to find b) Return 4. Otherwise (head!= NULL): a) Advance thru the LL with a while loop i. previous = current and current = current -> next b) If (current = NULL), then we didn t find anything (special case: target not found) i. Return c) If (current == head), then our target is at the head (special case: skip first node) a) Adjust head to head->next d) Otherwise, it s the regular case : previous-> = current-> e) Delete the node from memory! (i.e. delete(current)) 6/5/18 Matni, CS16, Sp18 15

Entire Code Revealed Demo Code! void deletenode(nodeptr &head, int target) { NodePtr curr = head, prev = head; if(head == NULL) cout<<"nothing to delete.\n"; else { while ((curr!= NULL) && (curr->data!= target)) { prev = curr; curr = curr->next; } // end while // Special Case: target not found if(curr == NULL) { cout << "Node not found nothing to delete.\n"; return; } // Special Case: target found at head of LL if(curr == head) head = head->next; // Regular case: else prev->next = curr->next; // Free up that now deleted node in memory! delete(curr); } // end else } // end deletenode 6/5/18 16

Exercise Example 2 We ve already demonstrated how to build a ed list using the add to head approach, like: h -> NULL h -> A -> NULL h -> B -> A -> NULL h -> -> -> -> NULL C B A 6/5/18 Matni, CS16, Sp18 17

Exercise Example 2 What would it be like to build a ed list by putting new nodes at the tail instead? (without using reversing) h -> NULL h -> A -> NULL h -> A -> B -> NULL h -> -> -> -> NULL A B C 6/5/18 Matni, CS16, Sp18 18

Figure Out the Algorithm! Regular case: h -> A -> B -> NULL Here s the node C How do I get this in the LL? Edge case: h -> NULL??? Do I do anything different here? 6/5/18 Matni, CS16, Sp18 19

q Lab 10 due on Friday q NO HOMEWORK!! YOUR TO-DOs q Prepare for final exam and come with questions on Thursday! q Visit TAs office hours if you need help! 6/5/18 Matni, CS16, Sp18 20

6/5/18 Matni, CS16, Sp18 21