chain.
It is a chain of elements.
Memory space is allocated for the object. The following statement creates a Student reference with a name of twoStudent. Student twoStudent; No new memory space created. The following statement initializes the pointer: twoStudent = oneStudent; Pointer twoStudent points to the same location as oneStudent.
firstStudent.nextStudent = secondStudent;
secondStudent.nextStudent = null;
null
firstStudent
secondStudent
firstStudent
current
temp
At the end of the loop, there are a total of 10 nodes created and the current pointer points to the last node in the linkedlist. nextstudent pointer of the last node points to null.
when i=0 i=1 i=8 i=9
null current
firstStudent current
} return firstStudent;
}
Data Structures Featuring Java Sridhar Radhakrishnan
10
null
firstStudent
After deletion, the firstStudent pointer will point to the node given by the dotted arrow if Smith is contained in the first node.
Smith
null
previous
current
Otherwise, after deletion,the nextStudent pointer corresponding to the previous node will point to the node pointed by current. nextstudent and is shown by a dotted arrow.
Data Structures Featuring Java Sridhar Radhakrishnan
11
null
firstStudent
Data Structures Featuring Java Sridhar Radhakrishnan
current
12
13
Exception Classes
import java.io.*; public class LinkedListException extends Exception { } public class LinkedListAddedNull extends LinkedListException { }
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Consider a case where we have an empty list and we want to add an object B to it.
Inserting B
31
B
_info is not equal to NULL.
32
Consider another case where we have two linked lists B and C. We want to add an object A to it.
B
object = C. So, object is not empty and no exceptions occur.
Data Structures Featuring Java Sridhar Radhakrishnan
33
Consider another case where we have two linked lists B and C. We want to add an object A to it.
34
B B
newlist
35
A B B
newlist
36
A B
newlist
37
A B B
Original list and new node
Empty list
C
Non-Empty list
Inserting A
B B
Original list
New node
A
Data Structures Featuring Java Sridhar Radhakrishnan
C
38
39
null
40
null
41
= C B C D
null
42
null
43
null
44
null
45
46
null
47
DATA FOUND!!!
C D
null
48
49
50
null
The doubly linked list can be traversed backwards and forwards from any node of the linked list using _previous and _next pointers, respectively.
51
They are useful whenever we need to process elements in a round robin fashion.For example, each node in a circularly linked list could correspond to a process(a program in execution) that is waiting for CPU time. Another variation of circularly linked list is the circularly linked doubly linked linked list. Here the _previous pointer of the first element points to the last element of the doubly linked list and the _next pointer of the last element points to the first element.
Data Structures Featuring Java Sridhar Radhakrishnan
52
Generalized lists
In the definition of linked lists, another field of data type called _sublist is added as a result of which we can see that each node of the linked list points to 2 linked lists: one using the _next pointer and other using the _sublist pointer
a b
null
c f
null null
null
( a ( de ( i )) b c ( fgh )
h
null null null
d
null
i
null
null
Parenthesis form of this generalized list: ( a ( de ( i )) b c ( fgh ) First level elements : a, b, c Second level elements: de,fgh and Third level elements : i
Data Structures Featuring Java Sridhar Radhakrishnan
53
Vectors can also be implemented using arrays as follows:Consider the following linked list:
a b c d
null
The above linked list can be represented in an array of objects as shown below:
0 1 2 3 4 5 6 _info null a c b null d null _next -1 3 5 2 -1 -1 -1
54
400K
2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
56
1000K
2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
57
1000K
P2
2000K
2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
58
1000K
P2
2000K
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
P3
59
1000K
P2
2000K
P3
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
60
1000K
P4
1700K
2000K
P3
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
61
1000K
P4
1700K
2000K
P3
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
62
1000K
1700K
P4
2000K
P3
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
63
900K 1000K
1700K
P4
2000K
P3
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
64
P5
900K 1000K
1700K
P4
2000K
P3
2300K 2560 k
Data Structures Featuring Java Sridhar Radhakrishnan
65
Partition class needs to be created for this purpose. public class Partition { int beginAddress; // beginning address of the partition int endAddress; // end address of the partition boolean hole; // hole is false if it is occupied by a // process, // true otherwise. int processID; // if hole is false, it contains the process // identifier of the process using the // partition, // otherwise -1 };
Data Structures Featuring Java Sridhar Radhakrishnan
66
2560
true
-1
null
67
1000
2560
false
1
true
-1
null
68
400
999
1000
1999
2000
2560
false
1
false
2
true
-1
null
69
400
999
1000
1999
2000
2299
2300
2560
false
1
false
2
false
3
true
-1
null
70
400
999
1000
1999
2000
2299
2300
2560
false
1
true
-1
false
3
true
-1
null
71
400
899
1000
1699
1700
1999
2000
2299
2300
2560
false
1
false
4
true
-1
false
3
true
-1
null
72
400
999
1000
1699
1700
1999
2000
2299
2300
2560
true
-1
false
4
true
-1
false
3
true
-1
null
73
400
899
900
999
1000
1699
1700
1999
2000
2299
2300
2560
false
5
true
-1
false
4
true
-1
false
3
true
-1
null
74