Linked List in Java
Linked List in Java
Jun-Oct 2013
CSC 248
Linked Lists
Linked Lists
What is a linked lists?
CSC248
Jun-Oct 2013
Cont
Structure of a node
CSC248
Jun-Oct 2013
CSC248
Jun-Oct 2013
Insertion
p = head.link;//p points to the node with info 65
newNode = new LinkedListNode();//create the object newNode
newNode.info = 50;//store 50 in the object newNode
CSC248
Jun-Oct 2013
Insertion
//insert newNode after p
newNode.link = p.link;
p.link = newNode;
Insertion
//insert newNode after p
newNode.link = p.link;
p.link = newNode;
head
45
65
34
76
newNode
50
10
CSC248
Jun-Oct 2013
Deletion
p = head.link;
p.link = p.link.link;
Node to be deleted is 34
11
Deletion
p = head.link;
head
q = p.link;
45
65
p
34
76
p.link = q.link;
q = null;
head
45
65
34
76
45
65
76
34
p
q
CSC248
Jun-Oct 2013
fforward
d a new node
d is always
l
inserted
d at the
h end
d off the
h
linked list
backward a new node is always inserted at the beginning of
the linked list
13
14
CSC248
Jun-Oct 2013
Read
R
d and
d store a number
b in
i num
Create the object newNode
Copy the value of num into the info field of newNode
Initialize the link field of newNode to null
If first is null, the list is empty; make first and last point to
newNode
else the list is not empty
set last so that it points to the actual last node in the list
15
16
CSC248
Jun-Oct 2013
17
18
CSC248
Jun-Oct 2013
19
20
10
CSC248
Jun-Oct 2013
first
2
newNode
first
2
newNode
first
15
newNode
21
15
newNode
head
34
24
15
newNode
22
11
CSC248
Jun-Oct 2013
The logical
Th
l i l sequence off the
h iitems iin the
h structure is
i
decoupled from any physical ordering in memory
23
24
12
CSC248
Jun-Oct 2013
represents
null
a1
a2
a3
a4
25
next
x1
x2
x3
x4
prev
backward traversal
13
CSC248
Jun-Oct 2013
Every node:
27
x1
x2
...
xn
28
14
CSC248
Jun-Oct 2013
Cont
Can visit all the list elements from any starting point
29
15
CSC248
Jun-Oct 2013
Basic Operations
31
32
16
CSC248
Jun-Oct 2013
Cont
numList
head
33
Cont
new LinkedList();
numList addLast("1");
numList.addLast(
1 );
numList.addLast(2");
numList.addLast(3");
numList.addLast(4");
head
represents
null
1
34
17
CSC248
Jun-Oct 2013
Cont
head
represents
null
4
35
Cont
36
18
CSC248
Jun-Oct 2013
Cont
Delete a node
numList.remove(2); // from the nth node
head
37
Cont
Delete a node
numList.removeFirst();//from the beginning
head
38
19
CSC248
Jun-Oct 2013
Cont
Delete a node
numList.removeLast(); // from the end
head
39
Cont
40
20
CSC248
Jun-Oct 2013
Cont
System.out.println(
System
out println(Get
Get info from last node: "
+ (numList.size() - 1));
System.out.println(Get info from last node: "
+numList.getLast());
41
Cont
System.out.println(numList.getLast());
42
21
CSC248
Jun-Oct 2013
Cont
System.out.println(numList.set(2,"5"));
43
Exercise 1
tail
44
22
CSC248
Jun-Oct 2013
Exercise 2
45
Answer
head
tail
head
66
10
tail
head
66
10
68
tail
46
23
CSC248
Jun-Oct 2013
Answer
head
10
10
68
tail
head
68
tail
head
tail
47
Exercise 3
public class Number
{
pri ate int n
private
n;
public Number(){ n=0; }
public Number(int n) { this.n=n; }
public int getN() { return n; }
}
48
24
CSC248
Jun-Oct 2013
Exercise 3(cont)
49
Exercise 4
50
25
CSC248
Jun-Oct 2013
End of Chapter
Puan Yusnita Sokman
51
26