searching in a linked list (iterative approach)
public class itrSearch {
public class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public void addFirst(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public void print() {
Node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
public int itrSearch(int key) {
Node temp = head;
int i = 0;
while (temp != null) {
temp = temp.next;
i++;
if (temp.data == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Linkedlist ll = new Linkedlist();
ll.addFirst(1);
ll.addFirst(2);
ll.addFirst(3);
ll.addFirst(4);
ll.print();
System.out.println("Element found at index:" + ll.itrSearch(3));
}
}
Comments
Post a Comment