Linked Lists in Action Chapter 5 introduces the
- Slides: 37
Linked Lists in Action Chapter 5 introduces the oftenused data public classure of linked lists. This presentation shows how to implement the most common operations on linked lists. CHAPTER 4 Data public classures and Other Objects
Declarations for Linked Lists For this presentation, each node in the linked list is a class, as shown here. public class Int. Node { private int data; private Int. Node link; . . . } 10 15 data link 7 data null link
Declarations for Linked Lists The data portion of each node is an int. 15 public class Int. Node { private int data; private Int. Node link; . . . } 10 data link 7 data null link
Declarations for Linked Lists Each Int. Node also contains a link which refers to another Int. Node. public class Int. Node { private int data; private Int. Node link; . . . } 10 15 data link 7 data null link
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 Int. Node -- it is a reference 10 data to an Int. Node. link head 15 data link 7 data null link
Declarations for Linked Lists A program can keep track of the front node by using an Int. Node reference variable such as head. Notice that head is not an Int. Node -- it is a reference to an Int. Node. We represent the empty list by storing null in the head reference. null head
Inserting an Int. Node at the Front We want to add a new entry, 13, to the front of the linked list shown here. 15 10 7 null head
Inserting an Int. Node at the Front Create a new node. . . 15 10 7 null head
Inserting an Int. Node at the Front Create a new node. . . Place the data in the new node's data field. 13 15 10 7 null head
Inserting an Int. Node 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 10 7 null head
Inserting an Int. Node 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 10 7 null head
Inserting an Int. Node at the Front head = new Int. Node(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 10 7 null head
Inserting an Int. Node at the Front public Int. Node(int initial. Data, Int. Node initial. Link) { data = initial. Entry; link = initial. Link; }
Inserting an Int. Node at the Front public Int. Node(int initial. Data, Int. Node initial. Link) { data = initial. Entry; Does the constructor link = initial. Link; } work correctly for the first node on a new list ?
Inserting an Int. Node at the Front public Int. Node(int initial. Data, Int. Node initial. Link) { data = initial. Entry; Suppose head is null link = initial. Link; } and we execute the assignment shown here: head = new Int. Node(13, head); null head
Inserting an Int. Node at the Front public Int. Node(int initial. Data, Int. Node initial. Link) { data = initial. Entry; link = initial. Link; } head = new Int. Node(13, head); null head 13 null
Inserting an Int. Node at the Front public Int. Node(int initial. Data, Int. Node initial. Link) { data = initial. Entry; link = initial. Link; } head = new Int. Node(13, head); 13 head null
Inserting an Int. Node at the Front public Int. Node(int initial. Data, Int. Node initial. Link) { data = initial. Entry; When the statement link = initial. Link; finishes, the linked list } has one node, containing 13. head = new Int. Node(13, head); 13 head null
Caution! Always make sure that your linked list methods work correctly with an empty list. EMPTY LIST
Pseudocode for Inserting Int. Nodes 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. . .
Pseudocode for Inserting Int. Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: head = new Int. Node(new. Entry, head);
Pseudocode for Inserting Int. Nodes 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.
Pseudocode for Inserting Int. Nodes 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 previous 10 15 7 null head
Pseudocode for Inserting Int. Nodes 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 What is the name of this link? previous 10 15 7 null head
Pseudocode for Inserting Int. Nodes 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 What is the name of this link ? previous 10 15 7 null head
Pseudocode for Inserting Int. Nodes 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 10 15 7 null head
Pseudocode for Inserting Int. Nodes 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. Write one Java statement which will do the insertion. previous 10 15 7 null head
Pseudocode for Inserting Int. Nodes 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 previous. link = new Int. Node(new. Entry, previous. link); previous 10 15 Write one Java statement which will do the insertion. 7 null head
Pseudocode for Inserting Int. Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step: head = new Int. Node(new. Entry, 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 Int. Node(new. Entry, previous. link);
Pseudocode for Inserting Int. Nodes 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 add. Node. After in the linked list toolkit of Section 4. 2.
Pseudocode for Removing Int. Nodes 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.
Removing the Head Int. Node head = head. link; Draw the change that this statement will make to the linked list. 13 head 10 15 7 null
Removing the Head Int. Node head = head. link; 13 head 10 15 7 null
Removing the Head Int. Node head = head. link; 13 head 10 15 7 null
Removing the Head Int. Node Here’s what the linked list looks like after the removal finishes. 10 head 15 7 null
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
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, 3 G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, Tech. Pool 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. THE END
- Singly linked list vs doubly linked list
- Singly linked list vs doubly linked list
- List adalah
- The lion and the mouse rising action
- Exposition short story
- Cat morgan
- Language processing system in compiler design
- Stalls
- Jeep/eagle introduces a new product
- Use of semicolon in a list
- Lesson 3 lists practice
- Spelling shed
- Swst spelling lists
- K for words
- Edinburgh resource lists
- Python plot list of lists
- Lists of tuples python
- Cons in lisp
- Java types of lists
- Spelling city words their way
- Read wrote inc
- Empty list prolog
- Parallel list
- Python list functions
- Inca sun temple of cuzco ap world history
- Wish lists year
- Political lists new
- Qri word lists
- Words without vowels
- Wish lists year
- "new bookmarking lists 2018"
- Https://www.diigo.com/
- Political wish lists
- Ei wirds
- Ucl library reading lists
- Strengths lists
- Blockly lists
- Chapter 22 genetics and genetically linked diseases