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.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
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.
Chapter 9: Data Structures I
M180: Data Structures & Algorithms in Java
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
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 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,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
© 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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Chapter 6 Linked Structures © 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.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
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.
Pointers and Linked Lists
G64ADS Advanced Data Structures
Pointers and Linked Lists
Building Java Programs
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Chapter 12: Data Structures
Introduction to Data Structures
Building Java Programs
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
The Generic LinkedList<> Collection class
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
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. ● Java API: See Dr. Lee Website

List Nodes ● A list node contains only one element but it also contains a reference to another list node.

List Nodes 1. To create the nodes, then link them together: 2. To create the entire chain with one expression:

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

Linked List

Doubly Linked Nodes

The LinkedStack Class ● The LinkedStack points at the top of the stack. ● See Figure 6-9 on page 162.

The LinkedStack Class (Fig. 6-10, p162) Same operations as in Fig 4-1, p88

The LinkedStack Class (Fig. 6-11, p163)

The LinkedStack Class (Fig. 6-12, p164) Pushing a series of elements (A,B,C) onto a LinkedStack.

The LinkedStack Class ● The pop() method in Fig (p163) splices a node out. ● We have to store the element from the node we are removing in a variable called “result” (Fig. 6-11, line 27). ● We have to do this before we remove the node (Fig. 6-11, line 28), because the node becomes unreachable once we change the value of the top.

The LinkedStack Class (Fig. 6-13, p165) Repeated popping (C, B, A) from a LinkedStack.

The LinkedQueue Class (Fig. 6-13, p165) Always append a new item to the back and remove an item from the front.

The LinkedQueue Class

The LinkedQueue Class line 22: to append a new node to the back

The LinkedQueue Class line 36: to remove the front node

The LinkedQueue Class (Fig. 6-16, p167)

The LinkedQueue Class (Fig. 6-17, p167) ● If we remove the last node, front becomes null. ● It does not matter that back still points to the node that was just removed, because we didn’t touch the back pointer (or reference). front = front.getNext(); front becomes null.

The LinkedList Class

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

The LinkedList Class (Fig. 6-19, p169)

The LinkedList Class (line 13 tally = count)

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 a specific number of nodes to find the node we want to get or set. (See Figure 6-20 on page 170, or next slide.) ● The get() method is to get the item from the node specified by the index number. ● The set() method is to set (store) the target item into the node specified by the index number.

The LinkedList Class (get or set the element of the node with “index” number)

The LinkedList Class (Fig. 6-22, p171) Append a new node (item = “target”) to the end of the linked list.

The LinkedList Class ● Lines 3 (for an empty list) and 9 (for non-empty list) in the add method (Fig. 6-22) 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 ListNode have no fields or methods in common, a superclass doesn't really make sense; an interface is a better choice.

The Predecessor Interface

The LinkedList Class ● Both LinkedList and ListNode will have to implement it. ● ListNode already provides these methods. So we just have to change the first noncomment line in Figure 6-4 (p159) to: public class ListNode implements Predecessor {

The Predecessor Interface ● The Predecessor interface is implemented by two classes. ● The LinkedList class implements two interfaces. Fig. 6-25

A Shorter Version of the add() Method (Fig. 6-26)

The LinkedList Class ● Two remove() methods – One of these methods removes the element at a particular position; – The other checks 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. ● Solution: Keep track of two nodes (Fig. 6-27, p173) – The previous one – The current one ● Know as a two-finger algorithm.

The LinkedList Class (Fig. 6-27, p173) The two-finger algorithm

The LinkedList Class (Fig. 6-28, p173)

The ListIterator Class Fig The iterator() method returns an instance of ListIterator.

The ListIterator 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. – Deque is a double-ended queue allowing insertion into or deletion from either end. (p115)

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 to 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 ● The LinkedList class is a general-purpose linked structure. ● 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. ● LinkedQueue class appends a new node to the back and removes a node from the front. ● 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.

Chapter 6 Self-Study Homework ● Pages: ● Exercises: 6.5, 6.10, 6.12, 6.23, 6.24.