File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ public class SearchNode {
2+ public class Node {
3+ int data ;
4+ Node next ;
5+ public Node (int data ) {
6+ this .data = data ;
7+ }
8+ }
9+
10+ public Node head = null ;
11+ public Node tail = null ;
12+
13+ public void add (int data ){
14+ Node newNode = new Node (data );
15+ if (head == null ) {
16+ head = newNode ;
17+ tail = newNode ;
18+ newNode .next = head ;
19+ }
20+ else {
21+ tail .next = newNode ;
22+ tail = newNode ;
23+ tail .next = head ;
24+ }
25+ }
26+
27+ public void search (int element ) {
28+ Node current = head ;
29+ int i = 1 ;
30+ boolean flag = false ;
31+ if (head == null ) {
32+ System .out .println ("List is empty" );
33+ }
34+ else {
35+ do {
36+ if (current .data == element ) {
37+ flag = true ;
38+ break ;
39+ }
40+ current = current .next ;
41+ i ++;
42+ }while (current != head );
43+ if (flag )
44+ System .out .println ("Element is present in the list at the position : " + i );
45+ else
46+ System .out .println ("Element is not present in the list" );
47+ }
48+ }
49+
50+ public static void main (String [] args ) {
51+ SearchNode cl = new SearchNode ();
52+ cl .add (1 );
53+ cl .add (2 );
54+ cl .add (3 );
55+ cl .add (4 );
56+ cl .search (2 );
57+ cl .search (7 );
58+ }
59+ }
You can’t perform that action at this time.
0 commit comments