-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedlist.java
191 lines (183 loc) · 3.37 KB
/
linkedlist.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.util.*;
class Node{
Node next;
int data;
public Node(int d){
this.data=d;
}
public Node(){}
}
class ll extends Node{
Node head;
Node tail;
void insertEnd(int data)
{
Node new1 = new Node(data);
if(head==null)
{
head=new1;
tail=new1;
head.next=null;
}
else{
tail.next=new1;
tail=new1;
tail.next=null;
}
System.out.println(data+" gets inserted at end!");
}
void insertBeg(int data)
{
Node new1 = new Node(data);
if(head==null)
{
head=new1;
tail=new1;
head.next=null;
}
else{
new1.next=head;
head=new1;
}
System.out.println(data+" gets inserted at beginning!");
}
void insertbw(int data,int pos)
{
int i=1;
Node t = head;
Node new1=new Node(data);
if(pos<=size())
{
while(i<pos-1)
{
t=t.next;
i++;
}
new1.next=t.next;
t.next=new1;
t=null;
}
else{
System.out.println("Enter the correct position!");
}
}
void display()
{
Node r1=head;
if(head==null)
System.out.println("List is Empty");
else{
do{
System.out.print(r1.data+" -> ");
r1=r1.next;
}while(r1!=null);
}
System.out.println();
}
void delete(int num)
{
Node r=head;
if(r==null)
{
System.out.println("empty list");
return;
}
while(r.next!=null)
{
if(r.next.data==num)
{
r.next=r.next.next;
System.out.println(num +" gets deleted !");
break;
}
r=r.next;
}
}
int size()
{
int count=0;
Node t = head;
if(t==null)
return count;
do{
count+=1;
t=t.next;
}while(t!=null);
return count;
}
String search(int d)
{
Node t = head;
String ans = "Not Found";
do{
if(t.data==d)
{
ans="Found";
break;
}
t=t.next;
}while(t!=null);
return ans;
}
}
public class linkedlist{
public static void main(String[] args)
{
ll n = new ll();
Scanner c = new Scanner(System.in);
while(true)
{
System.out.println("------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("Linked list: \n== 1.insert beginning == 2.insert end == 3.insert between == 4.delete == 5.display == 6.search == 7.size == 8.exit ==");
System.out.println("------------------------------------------------------------------------------------------------------------------------------------");
int choice = c.nextInt();
switch(choice)
{
case 1:
{
System.out.println("enter the value ");
n.insertBeg(c.nextInt());
break;
}
case 2:
{
System.out.println("enter the value ");
n.insertEnd(c.nextInt());
break;
}
case 3:
{
System.out.println("enter the value and position");
n.insertbw(c.nextInt(),c.nextInt());
break;
}
case 4:
{
System.out.println("enter the data to be deleted");
n.delete(c.nextInt());
break;
}
case 5:
{
n.display();
break;
}
case 6:
{
System.out.println("enter the value to be searched");
System.out.println("\nThe value is "+n.search(c.nextInt()));
break;
}
case 7:
{
System.out.println("\nThe size is : "+n.size());
break;
}
case 8:
{
return;
}
}
}
}
}