-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakingLinkedList.java
More file actions
59 lines (47 loc) · 1.95 KB
/
Copy pathmakingLinkedList.java
File metadata and controls
59 lines (47 loc) · 1.95 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
59
//Question: All about the introduction of Linked List . And creating our first Linked List .
// INTRO: Linked list is a Data Structure where some random memory locations are selected . Every memory locations are called nodes .
// Nodes stores some value and the address of the next node .
// IN CONCLUTION: Nodes are "Objects" which have two "Has part" (val & next address) . [C language main jo kam "Structure" karta hai]
// So we have to create a Node class.
package LinkedList;
public class makingLinkedList {
public static class Node { // Making new class named "Node"
int val; // Has part (Data)
Node next; // Has part (Next node reference)
}
public static void main(String[] args) {
// Making 4 new "Node Objects" (a, b, c, d)
Node a = new Node();
Node b = new Node();
Node c = new Node();
Node d = new Node();
// Assigning values and linking
a.val = 10;
a.next = b;
b.val = 20;
b.next = c;
c.val = 30;
c.next = d;
d.val = 40;
d.next = null;
// Printing the values and linking
System.out.print(a.val + " ");
System.out.println(a.next);
System.out.print(b.val + " ");
System.out.println(b.next);
System.out.print(c.val + " ");
System.out.println(c.next);
System.out.print(d.val + " ");
System.out.println(d.next);
// Printing individual addresses
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
// Printing the values of all nodes with the help of first node (a)
System.out.println(a.val);
System.out.println(a.next.val); // b
System.out.println(a.next.next.val); // c
System.out.println(a.next.next.next.val); // d
}
}