Download presentation
Presentation is loading. Please wait.
Published byStuart Gaines Modified over 9 years ago
1
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
2
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.
3
List Nodes ● List nodes – Contain only one element but it also contains (a reference to) another list node.
4
List Nodes
7
● 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());
8
List Nodes
14
Stacks and Queues ● The LinkedStack points at the top of the stack.
15
Stacks and Queues
19
● 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.
20
Stacks and Queues
26
● If we remove the last node, front becomes null. ● It does not matter that back still points to the node that was just removed.
27
Stacks and Queues
28
The LinkedList Class
29
● LinkedList methods involve walking down the chain of reference. ● General form:
30
The LinkedList Class
32
● 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.
33
The LinkedList Class
35
● 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.
36
The LinkedList Class
37
● Both ListNode and LinkedList will have to implement it. public class listNode implements Predecessor {
38
The LinkedList Class
41
● 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.
42
The LinkedList Class ● Solution:Keep track of two nodes – The previous one – The current one ● Know as a two-finger algorithm.
43
The LinkedList Class
50
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.
51
The Java Collections Framework Revisited
52
● 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.
53
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.
54
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.
55
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.