-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_linked_list.java
More file actions
106 lines (97 loc) · 1.63 KB
/
single_linked_list.java
File metadata and controls
106 lines (97 loc) · 1.63 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.*;
//Java program to simulate the Sinly Linked List.
class list
{
static class Node
{
int data;
Node next;
Node(int data)
{
this.data = data;
next = null;
}
}
Node root,tail;
list()
{
this.root = null;
}
void insert(int data)
{
Node nn = new Node(data);
if(this.root==null)
this.root = nn;
else
{
this.tail.next = nn;
}
this.tail=nn;
}
void print()
{
list.Node temp = this.root;
while(temp!=null)
{
System.out.println(temp.data);
temp = temp.next;
}
}
void delete()
{
Integer ele;
System.out.print("Enter the element to delete:");
Scanner sc = new Scanner(System.in);
ele = sc.nextInt();
list.Node temp = this.root;
list.Node cn= this.root;
if(ele==root.data)
this.root = this.root.next;
else
{
while(temp.data!=ele)
{
cn = temp;
temp = temp.next;
}
cn.next = temp.next;
}
sc.close();
}
void find()
{
Integer ele;
System.out.print("Enter the element to find:");
Scanner sc = new Scanner(System.in);
ele = sc.nextInt();
list.Node temp = this.root;
while(temp!=null)
{
if(ele==temp.data)
{
System.out.println("Element "+ele+" found!");
break;
}
temp = temp.next;
}
if(temp==null)
System.out.print("Element not found!\n");
}
}
class main
{
public static void main(String args[])
{
list lis = new list();
lis.insert(1);
lis.insert(2);
lis.insert(3);
lis.insert(4);
lis.print();
lis.find();
lis.find();
lis.find();
lis.delete();
lis.print();
}
}