A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Slides:



Advertisements
Similar presentations
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Faster Sorting Methods Chapter 12 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
The Efficiency of Algorithms Chapter 9 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques, and Priority Queues Chapter 23 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Queues, Deques, and Priority Queues Chapter 23 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Balanced Search Trees (partial) Chapter 29 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall - Edited by Nadia Al-Ghreimil.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Slides by Steve Armstrong LeTourneau University Longview, TX
Bag Implementations that Use Arrays
A Bag Implementation that Links Data
A Bag Implementation that Links Data
Chapter 14: Queue and Priority Queue Implementations
Iterators (short version)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Slides by Steve Armstrong LeTourneau University Longview, TX
Slides by Steve Armstrong LeTourneau University Longview, TX
List Implementations Chapter 9.
Slides by Steve Armstrong LeTourneau University Longview, TX
Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Balanced Search Trees (partial)
Stack Implementations
Bag Implementations that Use Arrays
Queues and Priority Queue Implementations
A List Implementation That Links Data
List Implementations that Use Arrays
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents Linked Data  Forming a Chain by Adding to Its Beginning  Forming a Chain by Adding to Its End  Forming a Chain by Adding at Various Positions A Linked Implementation of the ADT List  The Private Class Node  The Data Fields and Constructor  Choosing a Core Group of Methods to Implement

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents A Linked Implementation of the ADT List  Adding to the End of the List  Adding at a Given Position Within the List  The Private Method getNodeAt  Assertions and the Method isEmpty  The Method display Testing the Incomplete Implementation

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Linked Data 1 Consider the analogy of desks in a classroom  Placed in classroom as needed  Each desk has a unique id, the “address”  The desks are linked by keeping the address of another chair  We have a chain of chairs

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Linked Data 2 Fig. 6-1 A chain of 5 desks.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to Beginning 3 First desk placed in room  Blank desk top, no links  Address of the first chair given to teacher Fig. 6-2 One desk in the room.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to Beginning 4 Second student arrives, takes a desk  Address of first desk placed on new desk  Instructor “remembers” address of new desk Fig. 6-3 Two linked desks

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to Beginning 6 Third desk arrives  New desk gets address of second desk  Instructor remembers address of new desk Fig. 6-4 Three linked desks, newest desk first.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to End 8 This time the first student is always at the beginning of the chain  Instructor only remembers first address The address of a new desk is placed on the previous desk (at end of chain)  End of chain found by following links  Newest desk does not have a pointer address to any other desk

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to End Fig. 6-5 Two linked desks, newest desk last.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to End Fig. 6-6 Three linked desks, newest desk last.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming a Chain by Adding to End 9 Fig. 6-7 Five linked desks, newest desk last.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming Chain by Adding at Various Positions 11 Consider the requirement to organize the chain chronologically by arrival time  New arrivals are placed somewhere in the chain, not necessarily at the end Possibilities for placement of a new desk  Before all current desks  Between two existing desks  After all current desks

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming Chain by Adding at Various Positions 13 Fig. 6-8 Chain of desks prior to adding a new desk to beginning of the chain

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming Chain by Adding at Various Positions Fig. 6-9 Addition of a new desk to beginning of a chain of desks

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming Chain by Adding at Various Positions 14 Fig Two consecutive desks within a chain prior to adding new desk between

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Forming Chain by Adding at Various Positions Fig Addition of a new desk between two other desks.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X The Class Node 16 Nodes are objects that are linked together to form a data structure We will use nodes with two data fields  A reference to an entry in the list  A reference to another node View class definition of NodeView class definition

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X The Class Node 17 Fig Two linked nodes that each reference object data

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X A Linked Implementation of the ADT List Use a chain of nodes  Remember the address of the first node in the chain Record a reference to the first node  The “head reference” View implementation of class LListView implementation  Contains the class Node as a private inner class

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Choosing a Core Group of Methods 20 Must be able to create the collection  Need method add() Desire to see objects that have been added to the list  Need method display() Constructor also necessary

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Adding to the End of the List 21 Fig (a) An empty list and a new node; (b) after adding a new node to a list that was empty

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Adding to the End of the List 22 Fig A chain of nodes (a) just prior to adding a node at the end; (b) just after adding a node at the end.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Adding at Beginning of the List 25 Fig A chain of nodes (a) prior to adding a node at the beginning; (b) after adding a node at the beginning. Click to View Source Code

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Adding at a Given Position Within the List 25 Fig A chain of nodes (a) prior to adding node between adjacent nodes; (b) after adding node between adjacent nodes Click to View Source Code

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Other Core Methods Implemented Method isEmpty() isEmpty()  Note use of assert statement Method display() display()  Sends output to System.out Private method getNodeAt() getNodeAt()

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Testing the Incomplete Implementation 35 Core group of methods implemented  Now to be tested  View test program and output View test program Other methods specified as stubs to satisfy syntax checker