-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture4_basic_link_list_manipulation_part2.java
More file actions
58 lines (55 loc) · 1.5 KB
/
lecture4_basic_link_list_manipulation_part2.java
File metadata and controls
58 lines (55 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.ObjectInputStream.GetField;
public class lecture4_basic_link_list_manipulation_part2 {
public static void main(String[] args) {
// starting of the node
Node head = null;
// creating the nodes first
Node n4 = new Node("40", null);
Node n3 = new Node("30", n4);
Node n2 = new Node("20", n3);
Node n1 = new Node("10", n2);
// assigning the head reference to the list
head = n1;
for(Node n = head; n!= null; n = n.next){
// do something
}
// int g = countNode(head);
// // System.out.println(g);
}
public int countNode(Node head){
int count = 0;
for(Node n = head; n!= null; n= n.next){
count++;
}
return count;
}
public Object get(Node head, int index){
int c = 0;
for(Node n = head; n!= null; n=n.next){
if(c==index){
return n.element;
}
c++;
}
return -1;
}
public Node nodeAt(Node head, int size, int index){
if(index<0 || index>= size){
return null;
}
Node n = head;
for(int i = 0; i< index; i++, n = n.next){
;
}
return n;
}
public void set(Node head, int index, Object elem){
int c=0;
for(Node n = head; n!= null; n=n.next){
if(c == index){
n.element = elem;
}
c++;
}
}
}