Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows...
-
Upload
bartholomew-tate -
Category
Documents
-
view
222 -
download
0
Transcript of Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows...
![Page 1: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/1.jpg)
Chapter 5 introduces the often-used data public classure of linked lists.
This presentation shows how to implement the most common operations on linked lists.
Linked Lists in ActionLinked Lists in Action
CHAPTER 4Data public classures and Other Objects
![Page 2: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/2.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/3.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/4.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/5.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/6.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/7.jpg)
Inserting an IntNode at the FrontInserting an IntNode at the Front
We want to add a new entry, 13, We want to add a new entry, 13, to the to the frontfront of the linked list of the linked list shown here.shown here.
10
15
7
nullhead
![Page 8: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/8.jpg)
Inserting an IntNode at the FrontInserting an IntNode at the Front
Create a new node...
10
15
77
nullhead
![Page 9: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/9.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/10.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/11.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/12.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/13.jpg)
public IntNode(int initialData, IntNode initialLink){ data = initialEntry;
link = initialLink;
}
Inserting an IntNode at the FrontInserting an IntNode at the Front
![Page 14: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/14.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/15.jpg)
public IntNode(int initialData, IntNode initialLink){ data = initialEntry;
link = initialLink;
}
Inserting an IntNode at the FrontInserting an IntNode at the Front
Suppose head is nulland we execute theassignment shown
here:
head = new IntNode(13, head);
head
null
![Page 16: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/16.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/17.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/18.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/19.jpg)
Caution!Caution!
Always make sure that your linked list methods work correctly with an empty list.
EMPTY LIST
![Page 20: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/20.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/21.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/22.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/23.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/24.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/25.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/26.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/27.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/28.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/29.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/30.jpg)
Pseudocode for Inserting IntNodesPseudocode for Inserting IntNodes
The process of adding a new node in the middle The process of adding a new node in the middle of a list can also be incorporated as a separate of a list can also be incorporated as a separate method. This function is called addNodeAfter in method. This function is called addNodeAfter in the linked list toolkit of Section 4.2.the linked list toolkit of Section 4.2.
![Page 31: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/31.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/32.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/33.jpg)
Removing the Head IntNodeRemoving the Head IntNode
10 15 7
nullhead
13
head = head.link;
![Page 34: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/34.jpg)
Removing the Head IntNodeRemoving the Head IntNode
head = head.link;
10 15 7
nullhead
13
![Page 35: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/35.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/36.jpg)
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: Chapter 5 introduces the often- used data public classure of linked lists. This presentation shows how to implement the most common operations.](https://reader036.fdocuments.net/reader036/viewer/2022062309/5697bfdf1a28abf838cb2cbd/html5/thumbnails/37.jpg)
THE ENDTHE END
Presentation copyright 1999, Addison Wesley Longman,For use with Data public classures 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.