Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 lists. Linked Lists in Action CHAPTER 4 Data Structures and Other Objects

2 l l 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 Lists

3 data link 7 l l 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 Lists data 10

4 l l Each IntNode also contains a link which refers to another IntNode. data 15 data 7 public class IntNode { private int data; private IntNode link;... } Declarations for Linked Lists data 10 link null link

5 Declarations for Linked Lists l l A program can keep track of the front node by using a variable such as head in this example. l l Notice that head is not an IntNode -- it is a reference to an IntNode. data link 10 data link 15 data link 7 null head

6 Declarations for Linked Lists l l A program can keep track of the front node by using an IntNode reference variable such as head. l l 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

7 Inserting an IntNode at the Front We want to add a new entry, 13, to the front of the linked list shown here. 10 1515 7 null head

8 Inserting an IntNode at the Front ¶ ¶Create a new node... 10 1515 7 null head

9 Inserting an IntNode at the Front ¶ ¶Create a new node... · ·Place the data in the new node's data field. 10 1515 7 null head 13

10 Inserting an IntNode at the Front 10 1515 7 null head 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.

11 Inserting an IntNode at the Front 10 1515 7 null head 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.

12 Inserting an IntNode at the Front 10 1515 7 null head 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);

13 public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Inserting an IntNode at the Front

14 public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Inserting an IntNode at the Front Does the constructor work correctly for the first node on a new list ?

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

16 public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Inserting an IntNode at the Front head null head = new IntNode(13, head); 13 null

17 public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Inserting an IntNode at the Front head 13 null head = new IntNode(13, head);

18 public IntNode(int initialData, IntNode initialLink) { data = initialEntry; link = initialLink; } Inserting an IntNode at the Front head 13 null When the statement finishes, the linked list has one node, containing 13. head = new IntNode(13, head);

19 Caution! l l Always make sure that your linked list methods work correctly with an empty list. EMPTY LIST

20 Pseudocode for Inserting IntNodes l l IntNodes are often inserted at places other than the front of a linked list. l l There is a general pseudocode that you can follow for any insertion function...

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);

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.

23 Pseudocode for Inserting IntNodes 15 1010 7 null head ·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 In this example, the new node will be the second node previous

24 Pseudocode for Inserting IntNodes 15 1010 7 null head ·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 link which is in the node previous Look at the link which is in the node previous

25 Pseudocode for Inserting IntNodes 15 1010 7 null head ·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 This link is called previous.link What is the name of this link ? previous

26 Pseudocode for Inserting IntNodes 15 1010 7 null head Ë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 previous.link refers to the head of a small linked list, with 10 and 7 previous

27 Pseudocode for Inserting IntNodes 15 1010 7 null head ·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 must be inserted at the front of this small linked list. The new node must be inserted at the front of this small linked list. 13 Write one Java statement which will do the insertion. previous

28 Pseudocode for Inserting IntNodes 15 1010 7 null head ·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. previous previous.link = new IntNode(newEntry, previous.link);

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. pExecute the step: previous.link = new IntNode(newEntry, previous.link);

30 Pseudocode for Inserting IntNodes l 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.

31 Pseudocode for Removing IntNodes l l IntNodes often need to be removed from a linked list. l l 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. l l We’ll look at the technique for removing a node from the front of a linked list.

32 Removing the Head IntNode 10 1515 7 null head 13 head = head.link; Draw the change that this statement will make to the linked list.

33 Removing the Head IntNode 10 1515 7 null head 13 head = head.link;

34 Removing the Head IntNode head = head.link; 10 1515 7 null head 13

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

36 l l It is easy to insert or remove a node at the front of a list. l l You also need a technique for inserting or removing a node elsewhere Summary

37 T HE E ND Presentation copyright 1999, Addison Wesley Longman, For use with Data Structures 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.


Download ppt "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."

Similar presentations


Ads by Google