Linked lists. Prof. Noah Snavely CS1114

Similar documents
CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CS1114 Assignment 3. 1 Previously, on Assignment 2. 2 Linked lists

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

Some major graph problems

Unit 10: Data Structures CS 101, Fall 2018

(notes modified from Noah Snavely, Spring 2009) Memory allocation

About this exam review

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

Information Science 2

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CISC 3130 Data Structures Spring 2018

Dynamic Memory Allocation

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Prof. Noah Snavely CS Administrivia. A4 due on Friday (please sign up for demo slots)

Optimization and least squares. Prof. Noah Snavely CS1114

Data Structure Series

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

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Linked Lists in C and C++

CSE 333 Lecture server sockets

Multicore and Parallel Processing

csci 210: Data Structures Graph Traversals

hw6, BFS, debugging CSE 331 Section 5 10/25/12 Slides by Kellen Donohue

GRAPHS Lecture 19 CS2110 Spring 2013

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Ashish Gupta, Data JUET, Guna

CS 241 Data Organization Binary Trees

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

CS 2113 Software Engineering

CS 206 Introduction to Computer Science II

61A Lecture 31. November 14th 2011

Tuesday Extra this week; candidates for tenure-track position Refreshments 4:00 pm in CS Commons; Talk 4:15 pm in 3821 Nicole Eikmeier, Purdue

Lecture 14: Path Planning II. Autonomous Robots, Carnegie Mellon University Qatar

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CS 318 Principles of Operating Systems

CS32 Discussion Week 3

Recitation 10: Malloc Lab

RECURSION (CONTINUED)

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Inter-Process Communication

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Monte Carlo Tree Search

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

Computer Science 1 Program 4 TA Lab (Stacks & Queues) Assigned: 3/2/11 Due: 3/23/11 (Wednesday) at 11:55pm (WebCourses time)

Binary Search Trees. What is a Binary Search Tree?

Claims Management - Delay analysis Online Workshop. Week No. 1: concept of claims

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

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

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

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

Algorithms: Lecture 10. Chalmers University of Technology

Introduction to Programming in C Department of Computer Science and Engineering

Binary Trees

An Introduction to MATLAB

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Lecture 17: Hash Tables, Maps, Finish Linked Lists

Skip Lists: Motivation

Interpolation, extrapolation, etc.

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CMSC 341 Lecture 7 Lists

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

EQ-ROBO Programming : Ladybird Robot

Optimizing Dynamic Memory Management

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

CS61C : Machine Structures

(the bubble footer is automatically inserted in this space)

CS 106X, Lecture 16 More Linked Lists

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

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

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

Implementing Algorithms

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

CSE 333 Lecture server sockets

CS 302 ALGORITHMS AND DATA STRUCTURES

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

CMSC 341 Leftist Heaps

Lecture 13: more class, C++ memory management

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CS 62 Practice Final SOLUTIONS

PATH FINDING AND GRAPH TRAVERSAL

The PLATO Computer System. David Eckhardt Bruce Maggs

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Project 1. Soumya Basu. Department of Computer Science Cornell University February 5, 2016

CS 4349 Lecture October 18th, 2017

Information Science 2

EECS 470. Branches: Address prediction and recovery (And interrupt recovery too.) Lecture 7 Winter 2018

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

ITI Introduction to Computing II

CS1 Lecture 13 Feb. 13, 2019

CS 4349 Lecture October 23rd, 2017

CS 106B Lecture 26: Esoteric Data Structures: Skip Lists and Bloom Filters

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

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

CS 221 Lecture. Tuesday, 11 October 2011

Transcription:

Linked lists Prof. Noah Snavely CS1114 http://cs1114.cs.cornell.edu

Administrivia Assignment 2, Part 2 due tomorrow Please don t wait until the last minute to finish (the last two problems are challenging) Assignment 3 will be posted tomorrow Due in two weeks (Friday, 3/6) Prelim 1 next Thursday, 2/26 in class Review session: Tuesday or Wednesday evening? Topics include: running time, graphs, linked lists 2

Making quickselect fast on non-random input The version of quickselect in A2 can be very slow What is the problem? How can we fix it? 3

Conditionals with multiple branches What if we want the robot to correctly obey a traffic signal? L = getlightcolor(); if L == red robotstop(); end if L == green robotdrivestraight(r, 10, 100); end if L == yellow robotdrivestraight(r, 100, 100); end if L ~= red && L ~= green && L ~= yellow fprintf( Unknown light color\n ); end 4

Conditionals with multiple branches What if we want the robot to correctly obey a traffic signal? L = getlightcolor(); if L == red robotstop(); else if L == green robotdrivestraight(r, 10, 100); else if L == yellow robotdrivestraight(r, 100, 100); else fprintf( Unknown light color\n ); end end end 5

Conditionals with multiple branches What if we want the robot to correctly obey a traffic signal? L = getlightcolor(); if L == red robotstop(); elseif L == green robotdrivestraight(r, 10, 100); elseif L == yellow robotdrivestraight(r, 100, 100); else fprintf( Unknown light color\n ); end 6

Last time Graph traversal 1 1 2 10 9 2 3 6 3 5 6 8 5 4 7 9 4 7 8 10 Two types of todo lists: Stacks Depth-first search Queues Breadth-first search 7

Last time Implementing a stack and queue using arrays What went wrong? Today we ll talk about a better approach 8

Linked lists Alternative to an array Every element (cell) has two parts: 1. A value (as in an array) 2. A link to the next cell 9

Linked lists Values 8 4 1 3 Links 10

M Linked lists as memory arrays We ll implement linked lists using M A cell will be represented by a pair of adjacent array entries 11

A few details I will draw odd numbered entries in blue and even ones in red Odd entries are values Number interpreted as list elements Even ones are links Number interpreted as index of the next cell AKA location, address, or pointer The first cell is M(1) and M(2) (for now) The last cell has 0, i.e. pointer to M(0) Also called a null pointer 12

Example 8 4 1 3 1 2 3 8 3 4 4 5 6 5 1 7 7 8 9 3 0 X 1 2 3 8 5 1 4 5 6 7 4 3 7 8 9 3 0 X 13

Traversing a linked list Start at the first cell, [M(1),M(2)] Access the first value, M(1) The next cell is at location c = M(2) If c = 0, we re done Otherwise, access the next value, M(c) The next cell is at location c = M(c+1) Keep going until c = 0 14

Inserting an element arrays How can we insert an element x into an array A? Depends where it needs to go: End of the array: A = [A x]; Middle of the array (say, between elements A(5) and A(6))? Beginning of the array? 15

Inserting an element linked lists Create a new cell and splice it into the list 8 4 1 3 M(1) 5 Splicing depends on where the cell goes: How do we insert: At the end? In the middle? At the beginning? 16

Adding a header We can represent the linked list just by the initial cell, but this is problematic Problem with inserting at the beginning Instead, we add a header a few entries that are not cells, but hold information about the list 1. A pointer to the first element 2. A count of the number of elements 17

Linked list insertion 1 2 3 4 5 6 7 8 9 10 11 12 13 Initial list 5 2 2 0 1 3 X X X X X X X First element starts at 5 Insert a 5 at end Size of list is 2 1 2 3 4 5 6 5 3 2 7 1 3 7 8 9 5 0 X 10 11 12 13 X X X X Insert an 8 after the 1 1 2 3 4 5 6 7 8 9 10 11 12 13 5 4 2 7 1 9 5 0 8 3 X X X Insert a 6 at the start 1 2 3 11 5 2 4 5 6 0 1 9 7 8 9 5 0 8 10 11 12 13 3 6 5 X 18

Linked list deletion We can also delete cells Simply update the header and change one pointers (to skip over the deleted element) Deleting things is the source of many bugs in computer programs You need to make sure you delete something once, and only once 19

Linked list deletion Initial list 1 2 3 4 5 6 7 8 9 10 11 12 13 5 4 2 7 1 9 5 0 8 3 X X X Delete the last cell 1 2 3 5 3 2 4 5 6 0 1 9 7 8 9 5 0 8 10 11 12 13 3 X X X Delete the 8 1 2 3 4 5 6 7 8 9 10 11 12 13 5 2 2 0 1 3 5 0 8 3 X X X Delete the first cell 1 2 3 4 5 6 7 8 9 10 11 12 13 3 1 2 0 1 3 5 0 8 3 X X X 20

Linked lists running time We can insert an item (at the front) in constant (O(1)) time Just manipulating the pointers As long as we know where to allocate the cell We can delete an element (at the front) in constant time 21

Linked lists running time What about inserting / deleting from the end of the list? How can we fix this? 22

Doubly linked lists 8 4 1 3 8 4 1 3 23

A doubly-linked list in memory 8 4 1 2 3 4 7 2 4 5 6 0 8 7 7 8 9 4 4 0 First element Last element Size of list 24