Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.

Similar presentations


Presentation on theme: "Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common."— Presentation transcript:

1 Linked Lists in Action 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. This lecture shows three linked list operations in detail. The operations are: 1. Adding a new node at the head of a linked list. 2. Adding a new node in the middle of a linked list. 3. Removing a node from a linked list. The best time for this lecture is just after the students have been introduced to linked lists (Section 4.1), and before the complete linked operations have been covered (Sections ). CHAPTER 4 Data public classures and Other Objects

2 Declarations for Linked Lists
For this presentation, each node in the linked list is a class, as shown here. data link 15 public class IntNode { private int data; private IntNode link; ... } data link 10 Here is a typical public class declaration that can be used to implement a linked list of integers, as described in Section 4.1 of the text. The actual class has methods for getting and setting the two instance variables. There is also a constructor that creates a new IntNode with specified data and link components. data link 7 null

3 Declarations for Linked Lists
The data portion of each node is an int. 15 data public class IntNode { private int data; private IntNode link; ... } link 10 data Within the public class, there is a data component of type "int". We could easily change this to some other type. For example, if we want to create a linked list of real numbers, then we would declare data to be double. In this example, we want a linked list of integers, so we have declared data as an int. In the picture, I have drawn three nodes which might be part of a linked list of integers. The data portions of these nodes contain the integers 10, 15 and 7. 7 link data null link

4 Declarations for Linked Lists
Each IntNode also contains a link which refers to another IntNode. 15 data public class IntNode { private int data; private IntNode link; ... } link 10 data Also inside each IntNode is a second member variable called link. The purpose of the link member variable is to contain a reference to the next IntNode in the sequence of nodes. Question: What appears in the link field of the final node in a linked list such as this? Answer: A special reference value called null, which means "This variable doesn't refer to anything." This makes sense because there is no node after the final node. 7 link data null link

5 Declarations 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 15 data link 10 When a linked list is implemented, we generally keep track of the first node by using a special variable called the "head reference". The head refers to the first node in the linked list. It is important to remember that head is not actually a node. It is just a single variable, which refers to the first actual node. data link 7 null head

6 Declarations 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. Also remember that it is possible for a linked list to have no nodes at all. This situation is called the "empty list". Even with the empty list, we still have a head variable, but the head has no nodes to refer to. So, instead of refering to something, the head variable of the empty list contains that special value, null. null head

7 Inserting an IntNode at the Front
We want to add a new entry, 13, to the front of the linked list shown here. 15 In this lecture, I'll show you the workings of three techniques to manipulate linked lists. The first shows how to insert a new entry at the front of a linked list. In order to develop the function, we'll trace through a small example. In the example the new entry (number 13) is being added to a list which already has three entries (10, 15 and 7). 10 7 null head

8 Inserting an IntNode at the Front
Create a new node... 15 In the technique, we’ll create a new node... 10 7 null head

9 Inserting an IntNode at the Front
Create a new node... Place the data in the new node's data field. 13 15 Once the new node has been created, we need to place values in the new node's instance variables. The first instance variable is the new node's data field, where we are supposed to place a copy of the entry. 10 7 null head

10 Inserting an IntNode at the Front
Create a new node... Place the data in the new node's data field.... Connect the new node to the front of the list. 13 15 ...and we must connect the new node to the rest of the existing list. In other words, we will place a reference into the link instance variable of the new node. 10 7 null head

11 Inserting an IntNode at the Front
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. 13 15 The insertion needs one last step: Making the head referto the newly-created node. This will be an assignment statement. Can you write the statement before I move to the next slide? 10 7 null head

12 Inserting an IntNode at the Front
head = new IntNode(13, head); 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. 13 15 Here’s the single assignment that creates a new IntNode (using the IntNode constructor) and makes head refer to this new node. 10 7 null head

13 Inserting an IntNode at the Front
public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Here's the complete implementation of the IntNode constructor.

14 Inserting an IntNode at the Front
public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Does the constructor work correctly for the first node on a new list ? Before we finish the example, we should check one last thing. Does the constructor work correctly if the list is initially empty? In other words, if the head reference is null, will the usual assignment statement (using the constructor) manage to correctly add the first node of the list? We can check with an example...

15 Inserting an IntNode at the Front
public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Suppose head is null and we execute the assignment shown here: ...as shown here. In this example, the head pointer is null, and the new entry is 13. head = new IntNode(13, head); head null

16 Inserting an IntNode at the Front
public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } The two statements of the constructor places the number 13 in the data field of the new node, and places null in the link field of the new node. head = new IntNode(13, head); head null 13 null

17 Inserting an IntNode at the Front
public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } The assignment makes head refer to this newly created node. head = new IntNode(13, head); 13 null head

18 Inserting an IntNode at the Front
public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } When the statement finishes, the linked list has one node, containing 13. So, as you can see, when the assignment finishes, there is one node in the linked list, and the head pointer correctly refers to the new node. In other words, our technique works just fine, even if we are adding the first node to a previously empty list. head = new IntNode(13, head); 13 null head

19 Caution! Always make sure that your linked list methods work correctly with an empty list. This slide is just a warning: Always make sure that your linked list techniques work sensibly with the empty list. If you run into a technique that fails for the empty list, then you will need to modify the technique by adding some special code to deal with a null head. EMPTY LIST

20 Pseudocode 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. . . That's the end of our insertion at the head. There are two more linked list techniques that we'll look at today. The first of these techniques is a inserts a new node at a place other than the front of a linked list.

21 Pseudocode 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); The first step of the pseudocode is to determine whether the new node will be inserted as the first node of the linked list. If so, then you can simply use the technique that we already saw, as shown here.

22 Pseudocode 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. On the other hand, if the new data does not belong at the front of the linked list, then you will start by setting up a reference that I call previous. This variable must be set up to refer to the node which is just before the new node's position.

23 Pseudocode 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. In this example, the new node will be the second node 10 As an example, suppose that we want to add 13 to this list, and we want to keep all the entries in order from largest to smallest. We don't want to put 13 first on this list, because 13 is smaller than 15. So, we proceed to set up the previous to point to the node which is just before the position where 13 should be inserted. In this example, previous would be set up to refer to the first node of the linked list (containing 15). The 13 will be inserted as the new second node of the list (after the 15, but before the 10). previous 15 7 null head

24 Pseudocode 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 Look at the link which is in the node previous 10 In order to insert the new node at the proper place, we need to examine the link field which is in the node refered to by previous. This link field is highlighted in the picture. Question: What is the name of this link field? Can you write the answer before I move to the next slide? previous 15 7 What is the name of this link? null head

25 Pseudocode 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 This link is called previous.link 10 As you see, the highlighted pointer is called previous.link. In other words: Start at previous, go to the node, and select the link field from the node. There is a reason that previous.link is important to us... previous 15 7 What is the name of this link ? null head

26 Pseudocode 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 previous.link refers to the head of a small linked list, with 10 and 7 10 ...as you can see here, previous.link actually refers to the first node of a small linked list. The small linked list contains 10 and 7 -- and more important, we want to add the new entry at the front of this small linked list. previous 15 7 null head

27 Pseudocode 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. 13 The new node must be inserted at the front of this small linked list. 10 Here is a small challenge: Can you write just one Java statement which will insert the new node at the front of the small linked list? Use the name newEntry for the name of the data that you are placing in the new node... previous 15 7 Write one Java statement which will do the insertion. null head

28 Pseudocode 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. 13 10 previous.link = new IntNode(newEntry, previous.link); Did anyone come up with this solution? By using the constructor, we can insert newEntry at the front of the small linked list. The key is that we have the variable previous.link which refers to the head of the small linked list. previous 15 7 Write one Java statement which will do the insertion. null head

29 Pseudocode 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: Here is the complete pseudocode for adding a new node. Remember: this pseudocode can be used for many different methods of inserting, such as keeping nodes in order from largest to smallest. previous.link = new IntNode(newEntry, previous.link);

30 Pseudocode for Inserting IntNodes
The process of adding a new node in the middle of a list can also be incorporated as a separate method. This function is called addNodeAfter in the linked list toolkit of Section 4.2. Sections 4.2 and 4.3 give a collection of IntNode methods for manipulating linked lists. One of these methods, called addNodeAfter , places a new node in the middle of a linked list, using the same technique that we have seen.

31 Pseudocode 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. Do you have energy for one more technique? I’ll just show you how to remove a node from the front of a linked list. You can figure out the code for removing other nodes, or you can read the solution in Section 4.2.

32 Removing the Head IntNode
head = head.link; Draw the change that this statement will make to the linked list. Since we are removing the first node of the list, we need to change the place where head is refering. Can you tell me where the head will refer after the assignment statement that is shown here? Be quick, because I am about to change the slide... 13 10 15 7 null head

33 Removing the Head IntNode
head = head.link; As you can see, the assignment statement makes the head refer to the second node of the list. 13 10 15 7 null head

34 Removing the Head IntNode
head = head.link; After this assignment, Java’s garbage collector will realize that the first node is no longer part of the list, and that first node will be returned to the heap. 13 10 15 7 null head

35 Removing the Head IntNode
Here’s what the linked list looks like after the removal finishes. Here’s the final result. 10 15 7 null head

36 Summary 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 A quick summary . . .

37 THE END Presentation copyright 1999, Addison Wesley Longman,
For use with Data public classures and Other Objects Using Java by 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 (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics 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 welcome to use this presentation however they see fit, so long as this copyright notice remains intact. Feel free to send your ideas to: Michael Main THE END


Download ppt "Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common."

Similar presentations


Ads by Google