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.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
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 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
CSC212 Data Structure - Section BC
Using a Stack Chapter 6 introduces the stack data type.
CSC212 Data Structure - Section AB
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Using a Stack Chapter 6 introduces the stack data type.
Recursive Thinking Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Complete Binary Trees Chapter 9 introduces trees.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

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 4.2-4.3). 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. 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

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

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

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

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

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

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

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

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

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

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

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.

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...

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

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

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

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

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

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.

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.

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.

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

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

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

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

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

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

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

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.

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.

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

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

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

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

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 . . .

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 main@colorado.edu THE END