CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Lists CS 3358.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures: A Pseudocode Approach with C
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Arrays.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CPS Data processing example l Scan a large (~ 10 7 bytes) file l Print the 20 most frequently used words together with counts of how often they.
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.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CPS 100, Spring From data to information l Data that’s organized can be processed  Is this a requirement?  What does “organized” means l Purpose.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
CS342 Data Structures End-of-semester Review S2002.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
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.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Compsci 100, Fall From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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,
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.
slides adapted from Marty Stepp and Hélène Martin
CompSci 100e 7.1 Plan for the week l Review:  Boggle  Coding standards and inheritance l Understand linked lists from the bottom up and top-down  As.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
Building Java Programs
Plan for the Week Review Big-Oh
From data to information
What’s the Difference Here?
Owen Astrachan Jeff Forbes October 19, 2017
Introduction to Linked Lists
Building Java Programs
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Presentation transcript:

CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string or an ArrayList or a …  Is there a difference? Why? What's complexity?  Suppose this is an important problem: we want to grow at the front (and perhaps at the back)  Think editing film clips and film splicing  Think DNA and gene splicing  Self-referential data structures to the rescue  References, reference problems, recursion

CompSci 100E 19.2 ArrayLists and linked lists as ADTs  As an ADT (abstract data type) ArrayLists support  Constant-time or O(1) access to the k-th element  Amortized linear or O(1) storage/time with add o Total storage used in n-element vector is approx. 2n, spread over all accesses/additions (why?)  Adding a new value in the middle of an ArrayList is expensive, linear or O(n) because shifting required  Linked lists as ADT  Constant-time or O(1) insertion/deletion anywhere, but…  Linear or O(n) time to find where : sequential search  Good for sparse structures: when data are scarce, allocate exactly as many list elements as needed, no wasted space/copying (e.g., what happens when vector grows?)

CompSci 100E 19.3 Linked list applications  Remove element from middle of a collection, maintain order, no shifting. Add an element in the middle, no shifting  What’s the problem with a vector (array)?  Naively keep characters in a linked list, but in practice too much storage, need more esoteric data structures  What’s (3x 5 + 2x 3 + x + 5) + (2x 4 + 5x 3 + x 2 +4x) ?  As a vector (3, 0, 2, 0, 1, 5) and (0, 2, 5, 1, 4, 0)  As a list ((3,5), (2,3), (1,1), (5,0)) and ________?  Most polynomial operations sequentially visit terms, don’t need random access, do need “splicing”  What about (3x ) ?

CompSci 100E 19.4 Linked list applications continued  If programming in C, there are no “growable-arrays”, so typically linked lists used when # elements in a collection varies, isn’t known, can’t be fixed at compile time  Could grow array, potentially expensive/wasteful especially if # elements is small.  Also need # elements in array, requires extra parameter  With linked list, one pointer used to access all the elements in a collection  Simulation/modeling of DNA gene-splicing  Given list of millions of CGTA… for DNA strand, find locations where new DNA/gene can be spliced in o Remove target sequence, insert new sequence

CompSci 100E 19.5 Linked lists, CDT and ADT  As an ADT  A list is empty, or contains an element and a list  ( ) or (x, (y, ( ) ) )  As a picture  As a CDT (concrete data type) public class Node { Node p = new Node(); String info; p.info = ”hello”; Node next; p.next = null; } p 0

CompSci 100E 19.6 Building linked lists  Add words to the front of a list (draw a picture)  Create new node with next pointing to list, reset start of list public class Node { String info; Node next; Node(String s, Node link){ info = s; next = link; } // … declarations here Node list = null; while (scanner.hasNext()) { list = new Node(scanner.nextString(), list); }  What about adding to the end of the list?

CompSci 100E 19.7 Dissection of add-to-front  List initially empty  First node has first word  Each new word causes new node to be created  New node added to front  Rhs of operator = completely evaluated before assignment list A list = new Node(word,list); B Node(String s, Node link) { info = s; next = link;} list

CompSci 100E 19.8 Standard list processing (iterative)  Visit all nodes once, e.g., count them or process them public int size(Node list){ int count = 0; while (list != null) { count++; list = list.next; } return count; }  What changes in code above if we change what “process” means?  Print nodes?  Append “s” to all strings in list?

CompSci 100E 19.9 Splicing  Consider prepending (add to front) and two methods: public void prepend(String s) { myString = s + myString; } public void prepend(String s) { myFront = new Node(s,myFront); myCount += s.length(); }  What is hidden complexity of these operations? Why?

CompSci 100E Timings in Splice.java method length StringStrandLinkStrand , , , , ,000 ??

CompSci 100E New task in Strand.java  Rather than simply prepending, what about splicing anywhere?  We have s.insert(k,str) to add string at k th position, so prepending is s.insert(0,str)  We want to mirror this behavior in all classes  What do we do in base class?  How do we implement in LinkStrand class? o What are issues? o How fast will it be?

CompSci 100E Building linked lists continued  What about adding a node to the end of the list?  Can we search and find the end?  If we do this every time, what’s complexity of building an N-node list? Why?  Alternatively, keep pointers to first and last nodes of list  If we add node to end, which pointer changes?  What about initially empty list: values of pointers? o Will lead to consideration of header node to avoid special cases in writing code  What about keeping list in order, adding nodes by splicing into list? Issues in writing code? When do we stop searching?

CompSci 100E Standard list processing (recursive)  Visit all nodes once, e.g., count them public int recsize(Node list) { if (list == null) return 0; return 1 + recsize(list.next); }  Base case is almost always empty list: null pointer  Must return correct value, perform correct action  Recursive calls use this value/state to anchor recursion  Sometimes one node list also used, two “base” cases  Recursive calls make progress towards base case  Almost always using list.next as argument

CompSci 100E Recursion with pictures  Counting recursively int recsize(Node list){ if (list == null) return 0; return 1 + recsize(list.next); } recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) System.out.println(recsize(ptr)); ptr

CompSci 100E Recursion and linked lists  Print nodes in reverse order  Print all but first node and… o Print first node before or after other printing? public void print(Node list) { if (list != null) { } print(list.next); System.out.println(list.info); print(list.next);

CompSci 100E Complexity Practice  What is complexity of Build ? (what does it do?) public Node build(int n) { if (n == 0) return null; Node first = new Node(n, build(n-1)); for(int k = 0; k < n-1; k++) { first = new Node(n,first); } return first; }  Write an expression for T(n) and for T(0), solve.  Let T(n) be time for build to execute with n-node list  T(n) = T(n-1) + O(n)

CompSci 100E Changing a linked list recursively  Pass list to method, return altered list, assign to list  Idiom for changing value parameters list = change(list, “apple”); public Node change(Node list, String key) { if (list != null) { list.next = change(list.next, key); if (list.info.equals(key)) return list.next; else return list; } return null; }  What does this code do? How can we reason about it?  Empty list, one-node list, two-node list, n -node list  Similar to proof by induction