Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



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

Chapter 22 Implementing lists: linked implementations.
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.
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Chapter 12: Data Structures
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 14 Advanced Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
Problem of the Day Bezout acquired 19 camels through his trading skill, “Of the collected camels,” it said in the late merchant’s will, “Exactly half go.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Question of the Day  Two English words change their pronunciation when their first letter is capitalized. What are they?
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Week 4 - Monday CS221.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
G64ADS Advanced Data Structures
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Linked List Stacks, Linked List Queues, Dequeues
Building Java Programs
Chapter 12: Data Structures
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Road Map CS Concepts Data Structures Java Language Java Collections
Linked node problem 3 What set of statements turns this picture:
Building Java Programs
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
The Generic LinkedList<> Collection class
Road Map CS Concepts Data Structures Java Language Java Collections
Recall: stacks and queues
Building Java Programs
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 7: Linked List Basics reading: 16.2
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Presentation transcript:

Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview ● 6.1 – Introduce linked structures. ● Made from chains of nodes, connected by references. ● 6.2 – Stacked and Queue interfaces are implemented. ● 6.3 – List interface ● 6.4 – Linked structures in the Java collections framework.

List Nodes ● List nodes – Contain only one element but it also contains (a reference to) another list node.

List Nodes

● These chains of nodes are usually constructed gradually. ● We can splice a node out of a chain if we can find the node's predecessor. node1.setNext(node2.getNext());

List Nodes

Stacks and Queues ● The LinkedStack points at the top of the stack.

Stacks and Queues

● The pop() method splices a node out. ● We have to store the element from the node we are removing in a variable. ● We have to do this before we remove the node, because the node becomes unreachable once we change the value of the top.

Stacks and Queues

● If we remove the last node, front becomes null. ● It does not matter that back still points to the node that was just removed.

Stacks and Queues

The LinkedList Class

● LinkedList methods involve walking down the chain of reference. ● General form:

The LinkedList Class

● These loops do not use an int to keep track of the current position in the list. ● We know when we've reached the end of the loop when node is null. ● The get() and set() methods do use an int, since they want to advance only a specific number of nodes.

The LinkedList Class

● Lines 3 and 9 do almost exactly the same thing: – Create a new ListNode and set some reference to point to. ● We can use polymorphism to write a single line of code which does whichever of these things is appropriate. ● Since LinkedList and ListNdoe have no fields or methods in common, a superclass doesn't really make sense; an interface is a better choice.

The LinkedList Class

● Both ListNode and LinkedList will have to implement it. public class listNode implements Predecessor {

The LinkedList Class

● Two remove() methods – One of these methods removes the element ata particular position; – The other removes a particular Object. ● Both use the technique of splicing out a node. ● We walk down the list looking for either the ith node or the node containing target. ● Problem: Once we find the offending node, we've forgotten the previous node.

The LinkedList Class ● Solution:Keep track of two nodes – The previous one – The current one ● Know as a two-finger algorithm.

The LinkedList Class

The Java Collections Framework Revisited ● Java collections framework contains a class LinkedList. – It is a Doubly linked list ● Provides methods addFirst(), addLast(), removeFirst(), and removeLast(). – A LinkedList can function as a stack, a queue, or even a deque.

The Java Collections Framework Revisited

● Linked structures are sequential access. – With sequential access data structure, it is necessary to go through the preceding elements. ● Array-based structures are random access. – In a random access data structure, we can jump to any pint instantaneously.

The Java Collections Framework Revisited ● If ArrayLists and LinkedLists are built into Java, why have we bothered to write these classes ourselves? – These are relatively easy data structures to build. ● Needed to understand the more complex structures yet to come. – We will likely have a write a data structure that is very similar to, but not identical to, a built-in structure. – Knowing what's going on “under the hood” allows us to use the built-in structures more effectively.

Summary ● We can build arbitrarily long chains of ListNodes. ● A DoublyLinkedNode also contains a reference to the previous node. ● LinkedStack class splices nodes in and out of one end of a chain.

Summary ● LinkedList class is a general-purpose linked structure. ● The Predecessor interface allows us to avoid writing special code. ● Two-finger algorithms require that we keep track of two consecutive nodes. ● Java collections framework provides a class LinkedList. – It is doubly linked.