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

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

Programming in C/C Lecture 2

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

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

Dynamic Memory Allocation and Linked Lists

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

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

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

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

Ch. 17: Linked Lists. Introduction to Linked Lists

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

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

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

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

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

CS 161 Exam II Winter 2018 FORM 1

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

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

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

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

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

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

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

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

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

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

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

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

BBM 201 DATA STRUCTURES

Linked List using a Sentinel

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

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

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

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

Welcome to Solving Problems with Computers I

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

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

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

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

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

Dynamic Arrays Makefiles and Mul3ple File Compiles

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

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

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

Chapter 1: Object-Oriented Programming Using C++

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

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

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Variables, Memory and Pointers

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

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

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

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

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

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

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

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

Pointers and References

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CMSC 341 Lecture 2 Dynamic Memory and Pointers

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

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

CS 61c: Great Ideas in Computer Architecture

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

CA341 - Comparative Programming Languages

Object Oriented Design

We'll dive right in with an example linked list. Our list will hold the values 1, 2, and 3.!

1. Which of the following best describes the situation after Line 1 has been executed?

CS 103 Lab - Party Like A Char Star

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

REFERENCES, POINTERS AND STRUCTS

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

CS 103 Lab 6 - Party Like A Char Star

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Introduction to Data Structures. Systems Programming

ALL ABOUT POINTERS C/C++ POINTERS

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

WHAT POINTERS REALLY ARE

6. Pointers, Structs, and Arrays. 1. Juli 2011

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

Structures and Pointers

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

EL2310 Scientific Programming

Data Structure Series

moretosearch = (location < length);

lecture09: Linked Lists

Object Reference and Memory Allocation. Questions:

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Reference operator (&)

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Transcription:

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15 Ziad Matni Dept. of Computer Science, UCSB

The head of a List The box labeled head, in Display 13.1, is not a node, but simply a pointer variable that points to a node Pointer variable head is declared as: ListNodePtr head; struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; ListNodePtr head; 5/29/18 Matni, CS16, Sp18 2

Accessing Items in a Node item count *link Looking at this example: one way to change the number in the first node from 10 to 12: (*head).count = 12; head is a pointer variable to a node, so *head is the node that head points to The parentheses are necessary because the dot operator (.) has higher precedence than the dereference operator (*) struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; ListNodePtr head; 5/29/18 Matni, CS16, Sp18 3

The Arrow Operator item count *link The arrow operator -> combines the actions of the dereferencing operator * and the dot. operator Specifies a member of a struct or object pointed to by a pointer: (*head).count = 12; can be written as head->count = 12; The arrow operator is more commonly used than the (*head).varname approach struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; ListNodePtr head; 5/29/18 Matni, CS16, Sp18 4

NULL The pre-defined constant NULL is used as an end marker for a linked list A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list The value of a pointer that has nothing to point to is NULL The value of NULL is 0 5/29/18 Matni, CS16, Sp18 5

NULL A definition of NULL is found in several libraries, including <iostream> Any pointer can be assigned the value NULL: double* there = NULL; // a pointer pointing to nothing // C++ as Zen Buddhism?! 5/29/18 Matni, CS16, Sp18 6

Accessing Node Data head->count = 12; head->item = bagels ; cout << head->count; //prints 12 cout << head->link->count; //prints 3 cout << head->link->link->item //prints tea 5/29/18 7

struct Node { int data; Node *link; }; Building a Linked List head typedef Node* NodePtr; NodePtr head; 3 NULL head = new Node; head->data = 3; head->link = NULL; 5/29/18 Matni, CS16, Sp18 8

Function head_insert Let s create a function that inserts nodes at the head of a list. void head_insert(nodeptr& head, int the_number); The first parameter is a NodePtr parameter that points to the first node in the linked list The second parameter is the number to store in the list head_insert will create a new node with the_number First, we will copy the_number into a new node Then, this new node will be inserted in the list as the new head node 5/29/18 Matni, CS16, Sp18 9

Pseudocode for head_insert 1. Create a new dynamic variable pointed to by temp_ptr 2. Place the data (the_number) in the new node called *temp_ptr 3. Make temp_ptr's link variable point to the head node 4. Make the head pointer point to temp_ptr 5/29/18 Matni, CS16, Sp18 10

Pseudocode for head_insert 1. Create a new dynamic variable pointed to by temp_ptr 2. Place the data (the_number) in the new node called *temp_ptr 3. Make temp_ptr's link variable point to the head node 4. Make the head pointer point to temp_ptr 5/29/18 Matni, CS16, Sp18 11

Pseudocode for head_insert 1. Create a new dynamic variable pointed to by temp_ptr 2. Place the data (the_number) in the new node called *temp_ptr 3. Make temp_ptr's link variable point to the head node 4. Make the head pointer point to temp_ptr 5/29/18 Matni, CS16, Sp18 12

#include <iostream> using namespace std; struct Node { int data; Node *link; }; Translating head_insert to C++ typedef Node* NodePtr; void head_insert(nodeptr& head, int the_number); int main() { NodePtr head; head = new Node; head->data = 3; head->link = NULL; head_insert(head, 5); return 0; } void head_insert(nodeptr& head, int the_number) { NodePtr temp_ptr; temp_ptr = new Node; } temp_ptr->data = the_number; temp_ptr->link = head; head = temp_ptr; 5/29/18 Matni, CS16, Sp18 13

Reversing a LL What if you wanted to go from Node1 -> Node2 -> Node3 to Node3 -> Node2 -> Node1?? It helps to think of other pointers showing you current, previous and next nodes Repeat the following thru the LL Next becomes what current links to Current then links to previous Previous is now current Current is now next Finally make h = previous and you ve reversed it! 5/29/18 Matni, CS16, Sp18 14

Memory Leaks Nodes that are lost by assigning their pointers a new address are not accessible any longer The program has no way to refer to the nodes and cannot delete them to return their memory to the heap (freestore) Programs that lose nodes have a memory leak Significant memory leaks can cause system crashes 5/29/18 Matni, CS16, Sp18 15

Searching a Linked List To design a function that will locate a particular node in a linked list: We want the function to return a pointer to the node so we can use the data if we find it, else it should return NULL The linked list is one argument to the function The data we wish to find is the other argument This declaration should work: NodePtr search(nodeptr head, int target); 5/29/18 Matni, CS16, Sp18 16

Function search (refined) We will use a local pointer variable, named here, to move through the list checking for the target The only way to move around a linked list is to follow pointers We will start with here pointing to the first node and move the pointer from node to node following the pointer out of each node 5/29/18 Matni, CS16, Sp18 17

Pseudocode for search Make pointer variable here point to the head node While ( (here does not point to a node containing target) AND (here does not point to the last node) ) { make here point to the next node } If (here points to a node containing the target) return here; else return NULL; 5/29/18 Matni, CS16, Sp18 18

Moving Through the List The pseudocode for search requires that pointer here step through the list How does here follow the pointers from node to node? When here points to a node, here->link is the address of the next node To make here point to the next node, make the assignment: here = here->link; struct Node { int data; Node *link; }; 5/29/18 Matni, CS16, Sp18 19

5/29/18 Matni, CS16, Sp18 20

q Start Lab 9 on Wednesday q Do HW15 by Thursday YOUR TO-DOs q Visit Prof s and TAs office hours if you need help! 5/29/18 Matni, CS16, Sp18 21

5/29/18 Matni, CS16, Sp18 22