-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_linked_list.java
More file actions
86 lines (74 loc) · 1.75 KB
/
basic_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
public class Linkedlist
{
Node head;
static class Node
{
int data;
Node next; //next is by default initialized by null
Node(int d) //parameterized constructor to initialize vlaues of Node class data members
{
data=d;
next=null;
}
}
//function to print the linked list.
void print()
{
Node n= head;
while(n!=null)
{
System.out.print(n.data + " ");
n=n.next;
}
}
public static void main(String args[])
{
Linkedlist ls=new Linkedlist();
ls.head=new Node(1);
Node two = new Node(2);
Node three = new Node(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
public class Linkedlist
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
}
void print()
{
Node n =head;
while(n!=null)
{
System.out.print(n.data+" ");
n= n.next;
}
}
public static void main(String args[])
{
Linkedlist ls= new Linkedlist();
ls.head=new Node(1);
Node one = new Node(23);
Node two = new Node(87);
Node three = new Node(76);
Node four = new Node(100);
ls.head.next=one;
one.next=two;
two.next=three;
three.next=four;
ls.print();
}
}