l l chapter 4 introduces the often- used linked list data structures l l this presentation shows how...

37
Chapter 4 introduces the often-used linked list data structures This presentation shows how to implement the most common operations on linked lists. Linked Lists in Action CHAPTER 4 Data Structures and Other Objects

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Chapter 4 introduces the often-used linked list data structures

This presentation shows how to implement the most common operations on linked lists.

Linked Lists in ActionLinked Lists in Action

CHAPTER 4Data Structures and Other Objects

Page 2: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

For this presentation, each node in the linked list is a class, as shown here.

data

link

10

data

link

15

data

link

7

null

public class IntNode{ private int data; private IntNode link; ...}

Declarations for Linked ListsDeclarations for Linked Lists

Page 3: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

data

link

7

The data portion of each node is an int.

link

null

public class IntNode{ private int data; private IntNode link; ...}

data

link

15

Declarations for Linked ListsDeclarations for Linked Lists

data10

Page 4: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Each IntNode also contains a link which refers to another IntNode.

data15

data7

public class IntNode{ private int data; private IntNode link; ...}

Declarations for Linked ListsDeclarations for Linked Lists

data10

link

link

null

link

Page 5: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Declarations for Linked ListsDeclarations for Linked Lists

A program can keep track of the front node by using a variable such as head in this example.

Notice that head is not an IntNode -- it is a reference to an IntNode.

data

link

10

data

link

15

data

link

7

nullhead

Page 6: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Declarations for Linked ListsDeclarations for Linked Lists

A program can keep track of the front node by using an IntNode reference variable such as head.

Notice that head is not an IntNode -- it is a reference to an IntNode.

We represent the empty list by storing null in the head reference.

head

null

Page 7: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Inserting an IntNode at the FrontInserting an IntNode at the Front

We want to add a new entry, We want to add a new entry, 13, to the 13, to the frontfront of the linked of the linked list shown here.list shown here.

10

15

7

nullhead

Page 8: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Inserting an IntNode at the FrontInserting an IntNode at the Front

Create a new node...

10

15

77

nullhead

Page 9: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Inserting an IntNode at the FrontInserting an IntNode at the Front

Create a new node... Place the data in the new

node's data field.

10

15

7

nullhead

13

Page 10: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Inserting an IntNode at the FrontInserting an IntNode at the Front

10

15

7

nullhead

13

Create a new node... Place the data in the new

node's data field.... Connect the new node to

the front of the list.

Page 11: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Inserting an IntNode at the FrontInserting an IntNode at the Front

10

15

7

nullhead

13

Create a new node... Place the data in the new node's

data field.... Connect the new node to the

front of the list. Make the head refer to the new

head of the linked list.

Page 12: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Inserting an IntNode at the FrontInserting an IntNode at the Front

10

15

7

nullhead

13

Create a new node... Place the data in the new node's

data field.... Connect the new node to the

front of the list. Make the head refer to the new

head of the linked list.

head = new IntNode(13, head);

Page 13: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

public IntNode(int initialData, IntNode initialLink){ data = initialEntry;

link = initialLink;

}

Inserting an IntNode at the FrontInserting an IntNode at the Front

Page 14: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

public IntNode(int initialData, IntNode initialLink){ data = initialEntry;

link = initialLink;

}

Inserting an IntNode at the FrontInserting an IntNode at the Front

Does the constructor work

correctly for the first

node on a new list ?

Page 15: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

public IntNode(int initialData, IntNode initialLink){ data = initialEntry;

link = initialLink;

}

Inserting an IntNode at the FrontInserting an IntNode at the Front

Suppose head is null

and we execute theassignment shown

here:head = new IntNode(13, head);

head

null

Page 16: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

public IntNode(int initialData, IntNode initialLink){ data = initialEntry;

link = initialLink;

}

Inserting an IntNode at the FrontInserting an IntNode at the Front

head

null

head = new IntNode(13, head);

13

null

Page 17: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

public IntNode(int initialData, IntNode initialLink){ data = initialEntry;

link = initialLink;

}

Inserting an IntNode at the FrontInserting an IntNode at the Front

head

13

null

head = new IntNode(13, head);

Page 18: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

public IntNode(int initialData, IntNode initialLink){ data = initialEntry;

link = initialLink;

}

Inserting an IntNode at the FrontInserting an IntNode at the Front

head

13

null

When the statementfinishes, the linked list

has one node,containing 13.

head = new IntNode(13, head);

Page 19: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Caution!Caution!

Always make sure that your linked list methods work correctly with an empty list.

EMPTY LIST

Page 20: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

IntNodes are often inserted at places other than the front of a linked list.

There is a general pseudocode that you can follow for any insertion function. . .

Page 21: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

Determine whether the new node will be the first node in the linked list. If so, then there is only one step:

head = new IntNode(newEntry, head);

Page 22: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.

Page 23: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.

In this example, thenew node will bethe second node

In this example, thenew node will bethe second node

previous

Page 24: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position

What is the name of this link?

Look at the linkwhich is in the node

previous

Look at the linkwhich is in the node

previous

previous

Page 25: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position

This link is calledprevious.link

This link is calledprevious.link

What is the name of What is the name of this link ?this link ?

previous

Page 26: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first):Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position

previous.linkrefers to the headof a small linked

list, with 10 and 7

previous.linkrefers to the headof a small linked

list, with 10 and 7

previous

Page 27: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.

The new node mustbe inserted at thefront of this small

linked list.

The new node mustbe inserted at thefront of this small

linked list.

13

Write one Java statement which will do the insertion.

previous

Page 28: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

15

10

7

nullhead

Otherwise (if the new node will not be first): Start by setting a reference named previous to refer to the

node which is just before the new node's position.13

Write one Java statement which will do the insertion.

previousprevious.link = new IntNode(newEntry, previous.link);

Page 29: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

Determine whether the new node will be the first node in the linked list. If so, then there is only one step:

head = new IntNode(newEntry, head);

Otherwise (if the new node will not be first): Set a reference named previous to refer to the node

which is just before the new node's position. Execute the step:

previous.link = new IntNode(newEntry, previous.link);

Page 30: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes

The process of adding a new node in the The process of adding a new node in the middle of a list can also be incorporated as a middle of a list can also be incorporated as a separate method. This function is called separate method. This function is called addNodeAfter in the linked list toolkit of addNodeAfter in the linked list toolkit of Section 4.2.Section 4.2.

Page 31: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Pseudocode for Removing IntNodesPseudocode for Removing IntNodes

IntNodes often need to be removed from a linked list.

As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere.

We’ll look at the technique for removing a node from the front of a linked list.

Page 32: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Removing the Head IntNodeRemoving the Head IntNode

10 15

7

nullhead

13

head = head.link;

Draw the change that this statement will make to the linked list.

Page 33: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Removing the Head IntNodeRemoving the Head IntNode

10 15

7

nullhead

13

head = head.link;

Page 34: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Removing the Head IntNodeRemoving the Head IntNode

head = head.link;

10 15

7

nullhead

13

Page 35: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

Removing the Head IntNodeRemoving the Head IntNode

Here’s what the linked list looks like after the removal finishes.

10 15

7

nullhead

Page 36: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

It is easy to insert or remove a node at the front of a list.

You also need a technique for inserting or removing a node elsewhere

Summary Summary

Page 37: L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked

THE ENDTHE END

Presentation copyright 1999, Addison Wesley Longman,For use with Data Structures and Other Objects Using Javaby Michael Main.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and in public classors who use Data public classures and Other Objects Using Java are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.