-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinearSearchInLinkedList.java
77 lines (70 loc) · 1.63 KB
/
LinearSearchInLinkedList.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
//Write a program to search an element with in linked list using linear search
class Node{
private int data;
private Node next;
public Node(int data){
this.data = data;
next = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
class Imp{
private Node head;
private int size;
public Imp(){
head = null;
size = 0;
}
public boolean isEmpty(){
return size==0;
}
public void addLast(int element){
Node node = new Node(element);
if(!isEmpty()){
Node temp = head;
while(temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(node);
}else{
head = node;
}
size++;
}
public boolean LinearSearch(int element){
boolean res = false;
if(!isEmpty()){
Node temp = head;
while(temp!=null){
if(temp.getData() == element){
res = true;
break;
}
temp = temp.getNext();
}
}else{
System.out.println("List is Empty!");
}
return res;
}
}
public class LinearSearchInLinkedlist {
public static void main(String[] args) {
Imp list = new Imp();
list.addLast(25);
list.addLast(35);
list.addLast(45);
System.out.println(list.LinearSearch(35));
}
}