Linked lists Prof. Noah Snavely CS1114

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

CHP-5 LinkedList.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Comp 335 File Structures Reclaiming and Reusing File Space Techniques for File Maintenance.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 1114: Data Structures – Implementation: part 1 Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Linked lists and memory allocation Prof. Noah Snavely CS1114
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Connected components and graph traversal Prof. Noah Snavely CS1114
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Data structures Abstract data types Java classes for Data structures and ADTs.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
3 Data. Software And Data Data Data element – a single, meaningful unit of data. Name Social Security Number Data structure – a set of related data elements.
Linked List by Chapter 5 Linked List by
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Implementing stacks Prof. Ramin Zabih
Prof. Ramin Zabih Implementing queues Prof. Ramin Zabih
Top 50 Data Structures Interview Questions
MAP Assessment Proctors.
CS 1114: Implementing Search
Programming Abstractions
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 15 Lists Objectives
Arrays and Linked Lists
Data Structures – Stacks and Queus
Linked List Intro CSCE 121 J. Michael Moore.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Programming Abstractions
Indirection.
CSE 12 – Basic Data Structures
By Yogesh Neopaney Assistant Professor Department of Computer Science
Linked lists Prof. Noah Snavely CS1114
Linked List Intro CSCE 121.
Chapter 9 Linked Lists.
Presentation transcript:

Linked lists Prof. Noah Snavely CS1114

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

4 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 Conditionals with multiple branches  What if we want the robot to correctly obey a traffic signal?

Conditionals with multiple branches  What if we want the robot to correctly obey a traffic signal? 5 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

Conditionals with multiple branches  What if we want the robot to correctly obey a traffic signal? 6 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

Last time  Graph traversal  Two types of todo lists: –Stacks  Depth-first search –Queues  Breadth-first search

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

9 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

10 Linked lists 8413 Values Links

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

12 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”

13 Example X X 789

14 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

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

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

17 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

18 Linked list insertion Initial list Insert a 5 at end Insert an 8 after the XXX 789 XXXX X 789 XXXX XXX X Insert a 6 at the start First element starts at 5 Size of list is 2

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

20 Linked list deletion Initial list Delete the last cell Delete the XXX XXX Delete the first cell XXX XXX

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

23 Doubly linked lists

24 A doubly-linked list in memory First element Last element Size of list