-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLinkedlistImplementation.java
77 lines (61 loc) · 1.15 KB
/
LinkedlistImplementation.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class LinkedlistImplementation {
public class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
Node head;
boolean isEmpty() {
return head == null;
}
void add(int data) {
Node toAdd = new Node(data);
if(isEmpty()) {
head = toAdd;
return;
}
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = toAdd;
}
void printAll() {
if(isEmpty()){
System.out.println("Empty List");
return;
}
Node temp = head;
while(temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
void search(int data) {
if(isEmpty()) {
System.out.println("Empty List");
return;
}
Node temp = head;
while(temp != null) {
if(temp.data == data) {
System.out.println("Found");
return;
}
temp = temp.next;
}
System.out.println("Not Found");
}
public static void main(String[] args) {
LinkedlistImplementation ll = new LinkedlistImplementation();
ll.add(12);
ll.add(1);
ll.add(22);
ll.add(10);
ll.printAll();
ll.search(22);
}
}