of 44
8/10/2019 11 Linked Lists_JAVA 01
1/44
21/1
Linked Lists
Reading: Savitch, Chapter 10
8/10/2019 11 Linked Lists_JAVA 01
2/44
21/2
Objectives
To learn
what is a linked list
how to declare a linked list in JAVA
how to create and traverse a linked list
8/10/2019 11 Linked Lists_JAVA 01
3/44
21/3
Motivation
Write a program to store information about
a class of students (eg., names, results, etc)
Problems associated with use of an array
how big should the array be?
too small => ?
too big => ?
Other examples: address book, dictionary
8/10/2019 11 Linked Lists_JAVA 01
4/44
21/4
Dynamic structures
Grow and diminish dynamically based on
the program requirement.
8/10/2019 11 Linked Lists_JAVA 01
5/44
21/5
Linked List
What is a linked list?
a variable-length collection of objects (of
the same class). Each object is called a
node of the linked list. Each node
contains a reference to the next node.
8/10/2019 11 Linked Lists_JAVA 01
6/44
21/6
A linked list
head
data data data
8/10/2019 11 Linked Lists_JAVA 01
7/4421/7
Linked list declaration
class StudentNode {
private String name;
private int mark;
private StudentNode next;
public StudentNode(String _n, int _m)
{ name = _n; mark = _m;
next = null;}
... ...
}
class LinkedList {private StudentNode head = null;
... ...
}
Ahmed
67
Kim
84
head
version 1
8/10/2019 11 Linked Lists_JAVA 01
8/4421/8
Linked list declaration
Alternatively, theStudentNode
class can bedefined as an inner class ofLinkedList.
class LinkedList {
private StudentNode head = null;
... ...private class StudentNode {
private String name;
private int mark;
private StudentNode next;
public StudentNode(String _n, int _m) {
name = _n; mark = _m;
next = null;
}
... ... version 2}
}
8/10/2019 11 Linked Lists_JAVA 01
9/4421/9
A linked list
Ahmed
67
Sally
48
Kim
84
null
head
8/10/2019 11 Linked Lists_JAVA 01
10/44
21/10
Adding to a linked list
Ahmed
67
Sally
48
Kim
84
null
head
Add Pablo to list
8/10/2019 11 Linked Lists_JAVA 01
11/44
21/11
Adding to a linked list
Ahmed
67
Sally
48
Kim
84
head
Pablo
56
null
Add Pablo to list
8/10/2019 11 Linked Lists_JAVA 01
12/44
21/12
Deleting from a linked list
Ahmed
67
Sally
48
Kim
84
head
Pablo
56
null
Delete Sally from list
8/10/2019 11 Linked Lists_JAVA 01
13/44
21/13
Deleting from a linked list
Ahmed
67
Kim
84
head
Pablo
56
null
Delete Sally from list
8/10/2019 11 Linked Lists_JAVA 01
14/44
21/14
Accessing elements
Ahmed
67
head
Accessing the first element
head.mark
head.name
is this using version 1 or 2 ?
8/10/2019 11 Linked Lists_JAVA 01
15/44
21/15
Insertion at front of list
Ahmed
67
Kim
84
Pablo
56
head
NULL
Add a node for student called Janice whohas a mark of 89
8/10/2019 11 Linked Lists_JAVA 01
16/44
21/16
Insertion (ctd)
Design
create a new node
fill in its data fields
connect the node to the front of the list
change headso that it points to the newnode
8/10/2019 11 Linked Lists_JAVA 01
17/44
21/17
Insertion (ctd)
Ahmed
67
Kim
84
Pablo
56
head
null
Add a node for a student called Janice whohas a mark of 89
Janice
89
8/10/2019 11 Linked Lists_JAVA 01
18/44
21/18
Insertion (ctd)
Ahmed
67
Kim
84
Pablo
56
head
null
Add a node for a student called Janice whohas a mark of 89
Janice
89
8/10/2019 11 Linked Lists_JAVA 01
19/44
21/19
Insertion (ctd)
Ahmed
67
Kim
84
Pablo
56
head
null
Add a node for a student called Janice whohas a mark of 89
Janice
89
8/10/2019 11 Linked Lists_JAVA 01
20/44
21/20
Insertion (ctd)
Ahmed
67
Kim
84
Pablo
56
head
null
Janice
89
Add a node for a student called Janice whohas a mark of 89
8/10/2019 11 Linked Lists_JAVA 01
21/44
21/21
Insertion (ctd)
StudentNode p = new StudentNode
(Janice, 89);
p.next = head;
head = p;
JAVA source code
8/10/2019 11 Linked Lists_JAVA 01
22/44
21/22
Insertion (ctd)
StudentNode p = new StudentNode
(Janice, 89);
p.next = head;
head = p;
Ahmed
67
Kim
84
Pablo
56
head
null
8/10/2019 11 Linked Lists_JAVA 01
23/44
21/23
Insertion (ctd)
StudentNode p = new StudentNode
(Janice, 89);
p.next = head;
head = p;
Ahmed
67
Kim
84
Pablo
56
head
null
p
Janice
89
null
8/10/2019 11 Linked Lists_JAVA 01
24/44
21/24
Insertion (ctd)
StudentNode p = new StudentNode
(Janice, 89);
p.next = head;
head = p;
Ahmed
67
Kim
84
Pablo
56
head
NULL
p
Janice
89
8/10/2019 11 Linked Lists_JAVA 01
25/44
21/25
Insertion (ctd)
StudentNode p = new StudentNode
(Janice, 89);
p.next = head;
head = p;
Ahmed
67
Kim
84
Pablo
56
head
NULL
p
Janice
89
8/10/2019 11 Linked Lists_JAVA 01
26/44
21/26
Building a linked list
To create a linked list:
head = nullwhile more nodes
insert a node at front of list
8/10/2019 11 Linked Lists_JAVA 01
27/44
21/27
Printing a linked list
Ahmed
67
Kim
84
Pablo
56
head
null
8/10/2019 11 Linked Lists_JAVA 01
28/44
21/28
Printing a linked list (ctd)
start at the head of the list
while there is a node
print data of the node
move to the next node on the list
Pseudocode
8/10/2019 11 Linked Lists_JAVA 01
29/44
21/29
Printing a linked list (ctd)
StudentNode p = head;
while (p != null) {
System.out.println (p.name + : + p.mark);
p = p.next;
}
JAVA source code
8/10/2019 11 Linked Lists_JAVA 01
30/44
21/30
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + : +
p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
Printing a linked
list (ctd)
8/10/2019 11 Linked Lists_JAVA 01
31/44
21/31
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + : +
p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Printing a linked
list (ctd)
8/10/2019 11 Linked Lists_JAVA 01
32/44
21/32
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + : +
p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Printing a linked
list (ctd)
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
33/44
21/33
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + : +
p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Ahmed: 67
Printing a linkedlist (ctd)
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
34/44
21/34
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Ahmed: 67
Printing a linkedlist (ctd)
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
35/44
21/35
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Ahmed: 67
Printing a linkedlist (ctd)
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
36/44
21/36
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Ahmed: 67Kim: 84
Printing a linkedlist (ctd)
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
37/44
21/37
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Ahmed: 67Kim: 84
Printing a linkedlist (ctd)
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
38/44
21/38
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
Ahmed: 67Kim: 84
Printing a linkedlist (ctd)
8/10/2019 11 Linked Lists_JAVA 01
39/44
St d tN d h d
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
40/44
21/40
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
null
Ahmed: 67Kim: 84Pablo: 56
Printing a linkedlist (ctd)
St d tN d h d
Printing a linked
8/10/2019 11 Linked Lists_JAVA 01
41/44
21/41
StudentNode p = head;
while (p != null)
{
System.out.println (p.name + :
+ p.mark);
p = p.next;
}
Ahmed
67
Kim
84
Pablo
56
head
null
p
null
Ahmed: 67Kim: 84Pablo: 56
Printing a linkedlist (ctd)
8/10/2019 11 Linked Lists_JAVA 01
42/44
8/10/2019 11 Linked Lists_JAVA 01
43/44
21/43
Linked list example
Problem
write pseudocode to work out the average mark
of the students in the linked listconvert the pseudocode to JAVA code
8/10/2019 11 Linked Lists_JAVA 01
44/44
Calculate the average markStudentNode p = head;
int avMark = 0, numberSt = 0;
while (p != null) {
System.out.println (p.name + : + p.mark);
avMark = avMark + ____________;_____________;
p = p.next;
}
if (numberSt == 0)______________;
else
______________;