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