You are on page 1of 26

CHAPTER 4:

Linked Structures

Java Software Structures:


Designing and Using Data Structures

Third Edition
John Lewis & Joseph Chase
Addison Wesley
is an imprint of

© 2010 Pearson Addison-Wesley. All rights reserved.


Chapter Objectives

• Describe the use of references to create linked


structures
• Compare linked structures to array-based
structures
• Explore the techniques for managing a linked list
• Discuss the need for a separate node to form
linked structures
• Implement a stack collection using a linked list

1-2

© 2010 Pearson Addison-Wesley. All rights reserved. 1-2


References as Links

• There are many ways to implement a collection


• In chapter 3 we explored an array-based
implementation of a stack collection
• A linked structure uses object reference
variables to link one object to another
• Recall that an object reference variable stores
the address of an object
• In that sense, an object reference is a pointer to
an object

1-3

© 2010 Pearson Addison-Wesley. All rights reserved. 1-3


References as Links

Object obj;

1-4

© 2010 Pearson Addison-Wesley. All rights reserved. 1-4


Self-Referential Objects

• A Person object, for instance, could contain a


reference variable to another Person object:

public class Person


{
private String name;
private String address;

private Person next; // a link to another Person object

// whatever else

1-5

© 2010 Pearson Addison-Wesley. All rights reserved. 1-5


Linked Lists

• This type of reference can be used to form a


linked list, in which one object refers to the next,
which refers to the next, etc.
• Each object in a list is often generically called a
node
• A linked list is a dynamic data structure in that its
size grows and shrinks as needed, unlike an
array, whose size is static or fixed
• Java objects are created dynamically when they
are instantiated
1-6

© 2010 Pearson Addison-Wesley. All rights reserved. 1-6


A linked list

1-7

© 2010 Pearson Addison-Wesley. All rights reserved. 1-7


Non-linear Structures

• A linked list, as the name implies, is a linear


structure
• Object references also allow us to create non-
linear structures such as hierarchies and graphs

1-8

© 2010 Pearson Addison-Wesley. All rights reserved. 1-8


A complex linked structure

1-9

© 2010 Pearson Addison-Wesley. All rights reserved. 1-9


Managing Linked Lists

• The references in a linked list must be carefully


managed to maintain the integrity of the structure
• Special care must be taken to ensure that the
entry point into the list is maintained properly
• The order in which certain steps are taken is
important
• Consider inserting and deleting nodes in various
positions within the list

1-10

© 2010 Pearson Addison-Wesley. All rights reserved. 1-10


Accesing elements

• Considering the Person class definition:


Person current = front;
for (int i = 0; i < 3; i++)
current = current.next;

String cad = “Juan Pérez”;


Person current = front;
while (not(current.equals(cad)) && (current.next != null))
current = current.next;

1-11

© 2010 Pearson Addison-Wesley. All rights reserved. 1-11


Inserting a node at the front of a linked
list

1-12

© 2010 Pearson Addison-Wesley. All rights reserved. 1-12


Inserting a node in the middle of a linked
list

1-13

© 2010 Pearson Addison-Wesley. All rights reserved. 1-13


Deleting the first node in a linked list

1-14

© 2010 Pearson Addison-Wesley. All rights reserved. 1-14


Deleting an interior node from a linked list

1-15

© 2010 Pearson Addison-Wesley. All rights reserved. 1-15


Elements without Links

• The problem with self-referential objects is that


they must "know" they are part of a list
• A better approach is to manage a separate node
class that serves to link the elements together,
and that also reference the objects stored in the
list (see Figure 4.8)
• The list is still managed using the same
techniques
• The objects stored in the list need no special
implementation to be part of the list

1-16

© 2010 Pearson Addison-Wesley. All rights reserved. 1-16


Using separate node objects to store and
link elements

1-17

© 2010 Pearson Addison-Wesley. All rights reserved. 1-17


A linked implementation of a stack
collection

1-18

© 2010 Pearson Addison-Wesley. All rights reserved. 1-18


A “state” of a stack

1-19

© 2010 Pearson Addison-Wesley. All rights reserved. 1-19


A Linked Stack After a Push Operation

1-20

© 2010 Pearson Addison-Wesley. All rights reserved. 1-20


A Linked Stack After a Pop Operation

1-21

© 2010 Pearson Addison-Wesley. All rights reserved. 1-21


LinkedStack – the other operations

• Using a linked implementation, the peek


operation is implemented by returning a
reference to top
• The isEmpty operation returns true if the count of
elements is 0, and false otherwise
• The toString operation can be implemented by
simply traversing the linked list.

1-22

© 2010 Pearson Addison-Wesley. All rights reserved. 1-22


Analysis of Stack Operations

• Like our ArrayStack operations, the LinkedStack


operations work on one end of the collection and
are generally efficient
• The push and pop operations, for the linked
implementation are O(1)
• Likewise, the other operations are also O(1),
except for toString that is O(n).

1-23

© 2010 Pearson Addison-Wesley. All rights reserved. 1-23


Sentinel nodes

• There are variations on the implementation of


linked lists that may be useful in particular
situations
• One such solution is the use of sentinel nodes,
or dummy nodes, on either end of the list
• This practice eliminates the special cases of
inserting or deleting the first or last node

1-24

© 2010 Pearson Addison-Wesley. All rights reserved. 1-24


Doubly Linked Lists

• Another useful variation is a doubly linked list


• In a doubly linked list each node has a reference
to both the next and previous nodes in the list
• This makes traversing the list easier

1-25

© 2010 Pearson Addison-Wesley. All rights reserved. 1-25


A doubly linked list

1-26

© 2010 Pearson Addison-Wesley. All rights reserved. 1-26

You might also like