Documente Academic
Documente Profesional
Documente Cultură
Based on D.S. Malik, Java Programming: Program Design Including Data Structures
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
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)
Linked Lists
Class Node
public class Node { public int info; public Node link; }
Notice that instance variables of the class Node are declared as public
Example: The following code outputs the data /in each node
current = head; while (current != null){ System.out.println(current.info + ); current = current.link; }
10
11
Insertion
Consider the following linked list
12
Insertion
The following statements create and store 50 in the info field of a new node
newNode = new Node(); newNode.info = 50; //create newNode //store 50 in the new node
13
Insertion (continued)
The following statements insert the node in the linked list at the required place
newNode.link = p.link; p.link = newNode;
14
Insertion
newNode.link = p.link; p.link = newNode;
Insertion
Using two reference variables, we can simplify the code somewhat Consider the following
16
Insertion
The following statements insert newNode between p and q
newNode.link = q; p.link = newNode;
or
p.link = newNode; newNode.link = q;
17
Insertion
p.link = newNode; newNode.link = q;
Deletion
Consider the following linked list
19
Deletion
20
Deletion
Previous statement removed the node
However, the memory may still be occupied by this node
Systems automatic garbage collector reclaims memory occupied by unreferenced nodes Could use System.gc(); to manually run the garbage collector
21
Deletion
Using two reference variables, you can simplify the code somewhat 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;
23
Backward manner
A new node is always inserted at the beginning of the linked list
24
One to point to the last node of the list One to create the new node
25
26
27
28
29
30
31
UML class diagram of the class LinkedListNode and the outer-inner class relationship
32
33
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
class LinkedListClass
class LinkedListClass
Definition of the class LinkedListClass
public abstract class LinkedListClass<T> implements LinkedListADT<T> {
//Place //Place //Place //Place //Place the definition of the class LinkedListNode<T> here. the definition of the class LinkedListIterator<T> here. instance variables here the definition of the nonabstract methods here the definition of the abstract methods here.
36
UML class diagram of the class UnorderedLinkedList and the inheritance hierarchy
37
UML class diagram of the class OrderedLinkedList and the inheritance hierarchy
38
39
40