Computer Science 112 Fundamentals of Programming II Lists.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Computer Science 112 Fundamentals of Programming II List Iterators.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Linked Lists Course Lecture Slides 23 July 2010 A list is only as strong.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
Computer Science 112 Fundamentals of Programming II Array-Based Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
COMPSCI 105 S Principles of Computer Science Linked Lists 1.
Data Structures Using C++ 2E
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
Linked List by Chapter 5 Linked List by
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
COMPSCI 105 SS 2015 Principles of Computer Science
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
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.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
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.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Fundamentals of Programming II Linked Lists
Lecture 6 of Computer Science II
Fundamentals of Programming II Overview of Collections
CC 215 Data Structures Queue ADT
Linked lists.
Stack and Queue APURBO DATTA.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
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.
Programming Abstractions
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Linked Lists.
Revised based on textbook author’s notes.
Linked lists.
Presentation transcript:

Computer Science 112 Fundamentals of Programming II Lists

Lists: Formal Properties A list is a linear collection that supports access, replacement, insertion, and removal at any position Lists are more general-purpose than stacks and queues No standard set of operations, but most lists support many typical ones

Categories of List Operations Supporting, such as len, isEmpty, iter, +, == Index-based (use an index position) Content-based (use an object) Position-based (move a cursor)

L1 == L2 compares items at consecutive positions L1 + L2 items from L2 follow the items from L1 iter(L) items are served up from the first position through the last one Supporting Operations

L.insert(i, item) opens up a slot in the list at index i and inserts item in this slot L[i] returns the item at index i L.pop(i) removes and returns the item at index i (or item at last position if i is omitted) L[i] = item replaces the item at index i with the item Preconditions on [] and pop : 0 <= i < length of the list Index-Based Operations

L.append(item) adds item at the list ’ s tail (method add has same effect) item in L returns true if the list contains an object equal to item L.index(item) returns the index of the first instance of item in the list L.remove(item) removes the first instance of item from the list Preconditions on index and remove : item must be in the list Content-Based Operations

LI = L.listIterator() returns a new list iterator object on a list LI.first() moves the cursor to the first item LI.hasNext() returns true if there are any items following the current position LI.next() returns the next item and advances the position LI.last() moves the cursor to the last item LI.hasPrevious() returns true if there are any items preceding the current position LI.previous() returns the previous item and moves the position backward Position-Based Operations for Navigation Similar to an extended iterator

LI.insert(newItem) inserts newItem at the current position LI.remove() removes the last item returned by next or previous LI.replace(newItem) replaces the last item returned by next or previous Position-Based Operations for Mutation

List Applications object heap storage management documents files implementations of other collections

List Implementations –list standard Python (array-based) –ArrayList array-based –LinkedList two-way nodes –ListIterator (allows insertions, removals, movement to previous items)

Performance Tradeoffs lyst = LinkedList() # Add some objects to lyst # Traverse the list for i in range(len(lyst)): print(lyst[i]) Index-based traversal of a LinkedList using [] looks like it would be linear, but it’s actually quadratic! Almost all LinkedList operations are linear, including []

Performance Tradeoffs lyst = LinkedList() # Add some objects to lyst # Traverse the list for item in lyst: print(item) The simple iterator has the same performance in all list implementations

Performance Tradeoffs lyst = LinkedList() # Add some objects to lyst # Traverse the list rator = lyst.listIterator() while rator.hasNext(): print(rator.next()) Or use a list iterator for traversals of a list, in either direction

One-Way Linked Structures A one-way linked structure supports movement in one direction only; cannot move backward Access at the head is O(1), but access anywhere else is O(N) Insertion or removal at the head is a special case 5432 head

Tweak with a Tail Pointer Access to either end is O(1), but generally is still O(N) Still cannot move backward; insertion or removal at the head or tail is still a special case Removal at the tail is still linear 5432 head tail

Two-Way Linked Structures D1D2 A circular, doubly linked structure with a dummy header node permits movement in both directions allows constant-time access to the head or tail eliminates special cases in code when access is at the beginning or the end of the structure D3

An Empty Structure When there are no data, there is a single dummy header node Its two links point ahead and back to itself Its data field is None

class TwoWayNode(object): def __init__(self, data, previous = None, next = None): self.data = data self.previous = previous self.next = next The Node Class Strictly a utility class No need for accessor or mutator methods

Declaring an External Pointer head = TwoWayNode(None) head.previous = head.next = head head class TwoWayNode(object): def __init__(self, data, previous = None, next = None): self.data = data self.previous = previous self.next = next

Appending a Node temp = TwoWayNode("A", head.previous, head) head head.previous always points to the last node The last node ’ s next pointer always points back to head “A”“A” temp

Appending a Node temp = TwoWayNode("A", head.previous, head) head.previous.next = temp head “A”“A” temp head.previous always points to the last node The last node ’ s next pointer always points back to head

Appending a Node temp = TwoWayNode("A", head.previous, head) head.previous.next = temp head.previous = temp head “A”“A” temp head.previous always points to the last node The last node ’ s next pointer always points back to head

Analysis temp = TwoWayNode("A", head.previous, head) head.previous.next = temp head.previous = temp head No loop is needed to locate the last node Append is a constant time operation! No if statements needed to check for special cases “A”“A” temp

For Monday List Iterators