- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_linked_list.java
More file actions
Latest commit
86 lines (74 loc) · 1.75 KB
/
Copy pathbasic_linked_list.java
File metadata and controls
86 lines (74 loc) · 1.75 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//basic linkeed-list program
publicclassLinkedlist
{
Nodehead;
staticclassNode
{
intdata;
Nodenext; //next is by default initialized by null
Node(intd) //parameterized constructor to initialize vlaues of Node class data members
{
data=d;
next=null;
}
}
//function to print the linked list.
voidprint()
{
Noden= head;
while(n!=null)
{
System.out.print(n.data + " ");
n=n.next;
}
}
publicstaticvoidmain(Stringargs[])
{
Linkedlistls=newLinkedlist();
ls.head=newNode(1);
Nodetwo = newNode(2);
Nodethree = newNode(3);
//here basically the three nodes have been made in which each points to null ls.head is the first one
ls.head.next = two;
two.next=three; //next pointer of the second node now points to the third node
ls.print();
}
}
//practice
publicclassLinkedlist
{
Nodehead;
staticclassNode
{
intdata;
Nodenext;
Node(intd)
{
data=d;
next=null;
}
}
voidprint()
{
Noden =head;
while(n!=null)
{
System.out.print(n.data+" ");
n= n.next;
}
}
publicstaticvoidmain(Stringargs[])
{
Linkedlistls= newLinkedlist();
ls.head=newNode(1);
Nodeone = newNode(23);
Nodetwo = newNode(87);
Nodethree = newNode(76);
Nodefour = newNode(100);
ls.head.next=one;
one.next=two;
two.next=three;
three.next=four;
ls.print();
}
}