Prof. Ramin Zabih http://cs100r.cs.cornell.edu Implementing queues Prof. Ramin Zabih http://cs100r.cs.cornell.edu.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
DATA STRUCTURES USING C++ Chapter 5
Data Structures: A Pseudocode Approach with C
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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)
Linked lists Prof. Noah Snavely CS1114
Linked lists and memory allocation Prof. Noah Snavely CS1114
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Storage allocation issues Prof. Ramin Zabih
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Finding Red Pixels Prof. Ramin Zabih
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Data Structures and Algorithm Analysis Dr. Ken Cosh 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
Dynamic Storage Allocation
Lecture 6 of Computer Science II
Week 4 - Monday CS221.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Linked List Introduction
Storage 18-May-18.
Unusual Data Types CSC-3004 Introduction to Software Development
CSE 374 Programming Concepts & Tools
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
MIS 215 Module 1 – Unordered Lists
CS 1114: Implementing Search
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Garbage collection; lightstick orientation
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Hashing Exercises.
LINKED LISTS CSCD Linked Lists.
Pointers and Dynamic Variables
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Optimizing Malloc and Free
Stacks, Queues, and Deques
Chapter 15 Lists Objectives
Object Oriented Programming COP3330 / CGS5409
Stacks, Queues, and Deques
Data Structures – Stacks and Queus
Prof. Ramin Zabih Least squares fitting Prof. Ramin Zabih
Further Data Structures
Preprocessing to accelerate 1-NN
Linked Lists.
Lists CSE 373 Data Structures.
Problem Understanding
Introduction to Data Structures
Chapter 4 Unordered List.
Data Structures Unsorted Arrays
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
Linked lists Prof. Noah Snavely CS1114
Stacks, Queues, and Deques
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Quicksort and selection
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Presentation transcript:

Prof. Ramin Zabih http://cs100r.cs.cornell.edu Implementing queues Prof. Ramin Zabih http://cs100r.cs.cornell.edu

Administrivia Assignment 3 is out, due this+next Friday Prelim on 10/11 Quiz 4 this Tuesday, 10/2 Wednesday evening 10/3, RPC205: 7PM: Bobby Kleinberg on graphs (optional!) 8PM: RDZ prelim review Gurmeet will run a review session 10/10 Timing and details to be arranged

A note on reductions The neurotic high school Q3 question actually asks you to do a reduction Turn the problem you are given (rivalries) into one you already know about (graph coloring) This is one classic use of reduction Reduce an arbitrary instance of problem A to an instance of problem B  If you can do this, what else do you know? Major topic in CS381/CS482 You’ll see a simple example or two in CS100R

Linked lists with headers To insert an element, we create a new cell and splice it into the list At the beginning or end Actually, could add it anywhere Need to update header info, and the cell (if any) before the new one To delete an element we simply update the header and change some pointers Basically, need to “skip over” deleted element

Linked list with header 5 2 1 3 4 6 X 7 8 9 Initial list: Insert (end): Insert (start): Delete (end): Delete (start): 5 3 2 1 7 4 6 E 8 9 7 3 2 1 4 5 6 S 8 9 5 1 2 3 4 6 X 7 8 9 3 1 2 4 5 6 X 7 8 9

Linked lists and deques Linked lists support insertion/deletion at beginning or end What is the complexity of these 4 operations? Is this a good way to implement queues? How do we modify linked lists so that we can delete from the end in constant time? We clearly need a pointer to the last element in the header But this is not enough!

Doubly linked lists 8 4 1 3 3 1 4 8

A doubly-linked list in memory 8 4 4 7 2 1 3 8 5 6 9

Notes on doubly-linked lists Inserting and deleting is fast, but the code is very easy to get wrong Try it on all cases, especially trivial ones Look for invariants: statements that must be true of any valid list Debug your code by checking invariants In C/C++, this is done via assert Most languages have a facility like this built in But if not, you can just write your own!

Memory allocation So far we just assumed that the hardware supplied us with a huge array M When we need more storage, we just grab locations at the end Keep track of next free memory location What can go wrong? Consider repeatedly adding, deleting an item Notice that when we delete items from a linked list we simply changed pointers so that the items were inaccessible But, they still waste space!

Storage reclamation Someone has to figure out that certain locations can be re-used (“garbage”) If this is too conservative, your program will run slower and slower (“memory leak”) If it’s too aggressive, your program will crash (“blue screen of death”) Suppose your linked list was corrupted Why do computers crash when we read/write an arbitrary location? Must they??