A list = a Linear Recursive Structure (LRS or LRStruct) What is a list? 1.the empty list is a list 2.a pair whose tail is a list is itself a list This.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
Linked Lists Linear collections.
Section 5 Lists again. Double linked lists – insertion, deletion. Trees.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
COMP 103 Linked Stack and Linked Queue.
Linked Lists
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Special Visitor Sean Haneberg Microsoft XBoxLive Former UB-CSE student.
Preliminaries Attendance sheets –I remembered! Survey links –HW1 time survey –Anonymous feedback survey HW discussion (4PM, Commons 9)
Talk tonight Richard Stallman Norton 112 6:00 PM.
Preliminaries Stage 1 due tonight by 9:00 PM HW1 discussion (4PM, Commons 9) –may try for next week? HW2 available later today, due Monday by 9:00 PM.
No homework this week Stage 2 starts next week. Code review Team with N members is assigned N submissions to review Discuss submissions within team Everyone.
How do visitors work? This set of slides traces through the execution of a visitor we’ve seen before (the lengthVisitor) on a short list. It shows how.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Lists We’ve seen an array-based list implementation, the ArrayList. Advantage of an array-based implementation: –fast access to a specific index –typically.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Lists We’ve seen an array-based list implementation, the ArrayList.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Introduction to C Programming CE Lecture 21 Recursion and Linear Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Chapter 3: Arrays, Linked Lists, and Recursion
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
LINKED LISTS Linear Linked List. November 1, 2005Linked Lists2 of 48 Linked Lists Like stacks and queues, linked lists are lists of nodes that point to.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 5 Linked Lists II
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
(c) University of Washington20-1 CSC 143 Java Trees.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current.
Lists in Lisp and Scheme
Pointers and Linked Lists
DATA STRUCTURE QUEUE.
Computer Science 312 Haskell Lists 1.
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
List Implementations Chapter 9.
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.
Recursive Linked List Operations
Linear Recursive Structures(LRS)
CSC 143 Java Trees.
Queues: Implemented using Linked Lists
Queues and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

a list = a Linear Recursive Structure (LRS or LRStruct) What is a list? 1.the empty list is a list 2.a pair whose tail is a list is itself a list This is a recursive definition! (1) is called the base case, and (2) is called the recursive case. Note that traditional implementations do not follow this precise definition of what a list is: –many have no explicit representation of an empty list –none are recursive on the list; instead they recurse on a list node This has implications for how the structure can support extension (see Visitor support, in later slides)

States A list can therefore be in one of two states: –empty (corresponding to the base case) –non-empty (corresponding to the recursive case) The state-based implementation we will study makes this distinction explicit in the representation

Empty vs. NonEmpty state (look Ma, no NullPointerException!) An LRS object delegates all calls to its LRS State object – which can respond to all messages. Empty and NonEmpty states respond differently. There is never a null pointer in the structure! (Think about the implications of this for a while.)

What is basic (invariant) list functionality? insert new item at front remove item from front set/get first item (head) set/get rest (tail) plus (in Java) methods inherited from Object (toString, equals, etc)

How are these methods defined? public LRStruct insertFront(E item) { return _state.insertFront(this, item); } public E removeFront() { return _state.removeFront(this); } public LRStruct setDatum(E item) { return _state.setDatum(this, item); } public E getDatum() { return _state.getDatum(this); } public LRStruct setRest(LRStruct rest) { return _state.setRest(this, rest); } public LRStruct getRest() { return _state.getRest(this); }

LRStruct delegates to State! All of these methods delegate to the state of the LRStruct. States polymorphically determine what happens.

An empty LRS LRS a = new LRS(); _state LRSEmpty a

Inserting an item into an empty LRS a.insertFront(“fred”); _state LRSEmpty a

Inserting an item into an empty LRS a.insertFront(“fred”); _state LRS a Empty

Inserting an item into an empty LRS a.insertFront(“fred”); _state LRS NonEmpty a Empty _tail _dat= “fred”

Inserting an item into an empty LRS a.insertFront(“fred”); _state LRS NonEmpty a LRS Empty _state_tail _dat= “fred”

Inserting an item into an empty LRS a.insertFront(“fred”); _state LRS NonEmpty a LRS Empty _state_tail _dat= “fred”