-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec58_linked_list.java
56 lines (50 loc) · 1.37 KB
/
lec58_linked_list.java
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
package shreya.java;
public class lec58_linked_list {
public static void displayr(node head){
if(head==null)return;
System.out.println(head.data);
displayr(head.next);
}
public static void display(node head){
node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
public static int length(node head) {
int count = 0;
while (head != null) {
count++;
head = head.next;
}
return count;
}
public static class node{
int data; //value
node next; //address of the nxt node
node(int data){ //constructor
this.data =data;
}
}
public static void main(String[] args) {
node a = new node(5);
node b = new node(4);
node c = new node(7);
node d = new node(8);
a.next=b;
b.next=c;
c.next =d;
System.out.println(a.next.data);
System.out.println(a.next.next.data);
node temp =a;
while (temp!=null){
System.out.println(temp.data+" ");
temp = temp.next;
}
System.out.println(length(a));
display(a);
displayr(a);
System.out.println( length(a));
}
}