-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathlinkdlist.java
More file actions
44 lines (39 loc) · 975 Bytes
/
linkdlist.java
File metadata and controls
44 lines (39 loc) · 975 Bytes
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
import java.util.*;
public class linkdlist {
Node head;
static class Node {
int data;
Node next;
}
public void add(int data) {
Node newnode = new Node();
newnode.data = data;
newnode.next = null;
if (head == null) {
head = newnode;
} else {
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = newnode;
}
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
linkdlist list = new linkdlist();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.printList();
}
}