ch04 linked list structure

35
Chapter 4 Linked Structures

Upload: leminhvuong

Post on 05-Feb-2015

2.561 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Ch04 Linked List Structure

Chapter 4

Linked Structures

Page 2: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2

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 set collection using a linked list

Page 3: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-3

References as Links

• There are many ways to implement a collection

• In chapter 3 we explored an array-based implementation of a set 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

Page 4: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-4

FIGURE 4.1 An object reference variable pointing to an object

Page 5: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-5

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

}

Page 6: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-6

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 fixed

• Java objects are created dynamically when they are instantiated

Page 7: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-7

FIGURE 4.2 A linked list

Page 8: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-8

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

Page 9: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-9

FIGURE 4.3 A complex linked structure

Page 10: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-10

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

Page 11: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-11

FIGURE 4.4 Inserting a node at the front of a linked list

Page 12: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-12

FIGURE 4.5 Inserting a node in the middle of a linked list

Page 13: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-13

FIGURE 4.6 Deleting the first node in a linked list

Page 14: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-14

FIGURE 4.7 Deleting an interior node from a linked list

Page 15: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-15

Elements without Links• The problem with self-referential objects is that

they "know" they are part of a list

• A better approach is to manage a separate list of nodes that also reference the objects stored in the list

• 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

• A generic list collection can be used to store any kind of object

Page 16: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-16

FIGURE 4.8 Using separate node objects to store and link elements

Page 17: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-17

Doubly Linked Lists

• There are variations on the implementation of linked lists that may be useful in particular situations

• For example, 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

Page 18: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-18

FIGURE 4.9 A doubly linked list

Page 19: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-19

Listing 4.1

Page 20: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-20

Listing 4.1 (cont.)

Page 21: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-21

Listing 4.1 (cont.)

Page 22: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-22

FIGURE 4.10 A linked implementation of a set collection

Page 23: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-23

LinkedSet - Constructor

Page 24: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-24

LinkedSet - the add Operation

Page 25: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-25

LinkedSet - the removeRandom Operation

Page 26: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-26

LinkedSet - the removeRandom Operation

Page 27: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-27

LinkedSet - the remove Operation

Page 28: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-28

LinkedSet - the remove Operation

Page 29: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-29

LinkedSet - the remove Operation

Page 30: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-30

LinkedSet - the iterator Operation

Page 31: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-31

Listing 4.2

Page 32: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-32

Listing 4.2 (cont.)

Page 33: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-33

Listing 4.2 (cont.)

Page 34: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-34

FIGURE 4.11 UML description of the LinkedSet<T> class

Page 35: Ch04 Linked List Structure

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-35

Analysis of Linked Operations

• Since the order is irrelevant, and there is no capacity to expand, adding an element to the set is O(1)

• Removing a particular element, because it must be found, is O(n)

• Removing a random element requires a traversal of the list, and therefore is O(n)