Linked Lists Based on D S Malik Java
Linked Lists Based on D. S. Malik, Java Programming: Program Design Including Data Structures
Linked Lists s Linked list s List of items, called nodes s The order of the nodes is determined by the address, called the link, stored in each node s Every node (except the last node) contains the address of the next node s Components of a node s data/info: stores the relevant information s link: stores the address of the next node 2
Linked Lists s head or first s Holds the address of the first node in the list s The info part on the node can be either a value of a primitive type or a reference to an object s Class Node s Represents nodes on a list s It has two instance variables s info (of type int, but it can be any other type) s link (of type Node) 3
Linked Lists s Class Node public class Node { public int info; public Node link; } s Notice that instance variables of the class Node are declared as public 4
Linked List: Some Properties s Consider the following linked list 5
Linked List: Some Properties s Now consider the statement current = head; 6
Linked List: Some Properties s Now consider the statement current = current. link; 7
Linked List: Some Properties 8
Traversing a Linked List s Basic operations of a linked list that require the link to be traversed s Search the list for an item s Insert an item in the list s Delete an item from the list s You cannot use head to traverse the list s Why? You would lose the nodes of the list. s Use another reference variable of the same type as head: current 9
Traversing a Linked List s The following code traverses the list current = head; while (current != null){ //Do something - process current = current. link; } s Example: The following code outputs the data /in each node current = head; while (current != null){ System. out. println(current. info + “ “); current = current. link; } 10
Item Insertion and Deletion s Consider the following definition of a node public class Node { public int info; public Node link; } s And the following variable declaration Node head, p, q, new. Node; 11
Insertion s Consider the following linked list s We want to create a new node with info 50 and insert it after p 12
Insertion s The following statements create and store 50 in the info field of a new node new. Node = new Node(); new. Node. info = 50; //create new. Node //store 50 in the new node 13
Insertion (continued) s The following statements insert the node in the linked list at the required place new. Node. link = p. link; p. link = new. Node; s The sequence of statements to insert the node is very important s If you reverse the sequence of the statements, you will not get the desired result 14
Insertion new. Node. link = p. link; p. link = new. Node; List after the statement new. Node. link = p. link; executes List after the statement p. link = new. Node; executes 15
Insertion s Using two reference variables, we can simplify the code somewhat s Consider the following List with reference variables p and q 16
Insertion s The following statements insert new. Node between p and q new. Node. link = q; p. link = new. Node; or p. link = new. Node; new. Node. link = q; s The order in which these statements execute does not matter 17
Insertion p. link = new. Node; new. Node. link = q; List after the statement p. link = new. Node; executes List after the statement new. Node. link = q; executes 18
Deletion s Consider the following linked list s We want to delete node with info 34 19
Deletion s The following statement removes the node from the list p. link = p. link List after the statement p. link = p. link; executes 20
Deletion s Previous statement removed the node s However, the memory may still be occupied by this node s System’s automatic garbage collector reclaims memory occupied by unreferenced nodes s Could use System. gc(); to manually run the garbage collector 21
Deletion s Using two reference variables, you can simplify the code somewhat s Consider the following statements q = p. link; p. link = q. link; q = null; System. gc(); 22
Deletion (continued) q = p. link; p. link = q. link; List after the statement q = p. link; executes List after the statement p. link = q. link; executes 23
Building a Linked List s You can build a list in two ways: forward or backward s Forward manner s A new node is always inserted at the end of the linked list s Backward manner s A new node is always inserted at the beginning of the linked list 24
Building a Linked List Forward s A new node is always inserted at the end of the linked list s You need three reference variables s One to point to the front of the list s Cannot be moved without destroying the list s One to point to the last node of the list s One to create the new node 25
Building a Linked List Forward Node build. List. Forward(){ Node first, new. Node, last; int num; System. out. println(“Enter integers (999 to stop): ”); num = input. next. Int(); //priming read first = null; while (num != 999) { new. Node = new Node(); new. Node. info = num; new. Node. link = null; if (first == null) { //empty list first = new. Node; last = new. Node; } else { last. link = new. Node; last = new. Node; } num = input. next. Int(); //next read } return first; } 26
Building a Linked List Backward s A new node is always inserted at the beginning of the linked list s You only need two reference variables s One to point to the front of the list s Changes each time a new node is inserted s One to create the new node 27
Building a Linked List Backward Node build. List. Backward(){ Node first, new. Node; int num; System. out. println (“Enter integers (999 to stop): ”); num = input. next. Int(); //priming read first = null; while (num != 999) { new. Node = new Node(); //create a node new. Node. info = num; //store the data in new. Node. link = first; //put new. Node at the beginning first = new. Node; //update the head of the list num = input. next. Int(); //next read } return first; } 28
Linked List as an ADT UML class diagram of the interface Linked. List. ADT 29
Linked List as an ADT s There are two types of linked lists: sorted (ordered) and unsorted (unordered) s The algorithms to implement some of the operations differ for sorted and unsorted lists s Therefore, define the Linked. List. Class as an abstract class s Linked. List. Class has two derived classes s Unordered. Linked. List s Ordered. Linked. List 30
Structure of Linked List Nodes s Each node of a linked list must keep track of the data as well as the next node in the list s The node has two instance variables s The class Linked. List. Node is defined as an inner class of Linked. List. Class s Simplify operations such as insert and delete s Linked. List. Node is defined as protected and generic 31
Structure of Linked List Nodes UML class diagram of the class Linked. List. Node and the outer-inner class relationship 32
Instance Variables of the Class Linked. List. Class s Instance variables protected Linked. List. Node<T> first; //variable to store the address of the first node protected Linked. List. Node<T> last; //variable to store the address of the last node protected int count; //variable to store the number of nodes in the list 33
Linked List Iterators UML class diagram of the class Linked. List. Iterator and the outer-inner class relationship NOTE: An iterator is an object that produces each element of a collection one element at a time 34
class Linked. List. Class UML class diagram of the class Linked. List. Class 35
class Linked. List. Class s Definition of the class Linked. List. Class public abstract class Linked. List. Class<T> implements Linked. List. ADT<T> { //Place //Place the definition of the class Linked. List. Node<T> here. the definition of the class Linked. List. Iterator<T> here. instance variables here the definition of the nonabstract methods here the definition of the abstract methods here. } 36
Unordered Linked List UML class diagram of the class Unordered. Linked. List and the inheritance hierarchy 37
Ordered Linked Lists UML class diagram of the class Ordered. Linked. List and the inheritance hierarchy 38
Double Linked Lists s Linked list in which every node has a next pointer and a back pointer s A double linked list can be traversed in either direction public class Double. Linked. List. Node<T> implements Cloneable{ T info; Double. Linked. List. Node<T> next; Double. Linked. List. Node<T> back; . . . } 39
Circular Linked Lists s A linked list in which the last node points to the first node s It is convenient to make first point to the last node Circular linked list with more than one node 40
- Slides: 40