-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
233 lines (223 loc) · 6.22 KB
/
LinkedList.java
File metadata and controls
233 lines (223 loc) · 6.22 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.io.*;
public class LinkedList
{
int size;
Node start,end;
LinkedList(){
start=null;
end=null;
size=0;
}
void insertAtStart(int d){
Node nptr=new Node (d,null);
if (start==null){
start=nptr;
end=start;
size++;
}
else{
nptr.setLink(start);
start= nptr;
size++;
}
}
void insertAtEnd(int d){
Node nptr=new Node (d,null);
if (end==null){
start=nptr;
end=nptr;
size++;
}
else{
end.setLink(nptr);
end=nptr;
size++;
}
}
void insertAtPos(int d,int pos){
Node nptr=new Node (d,null);
Node ptr=start;
for(int i=1;i<=size;i++){
if(i==pos){
Node tmp=ptr.getLink();
ptr.setLink(nptr);
nptr.setLink(tmp);
size++;
break;
}
ptr=ptr.getLink();
}
}
void deleteAtStart(){
if(start==null){
System.out.println("No node is present to delete");
}
else{
start=start.getLink();
size--;
}
}
void deleteAtEnd(){
Node ptr=start;
Node tmp=start;
for (int i=1;i< size;i++){
tmp=ptr;
ptr=ptr.getLink();
}
end=tmp;
tmp.setLink(null);
size--;
}
void deleteAtPos(int pos){
Node ptr=start;
for (int i=1;i<=size;i++){
if(i==pos-1){
Node tmp=ptr.getLink();
Node nptr=tmp.getLink();
ptr.setLink(nptr);
size--;
break;
}
ptr=ptr.getLink();
}
}
/* int sumNodes(){
int sum=0;
Node ptr=start;
for(int i=0;i<size;i++){
sum=sum+ptr.getData();
ptr=ptr.getLink();
}
return sum;
}
*/
/*calculating sum using recursion */
int sum(Node ptr){
if(ptr==null){
return 0;
}
else{
int d=ptr.getData();
return d + sum(ptr.getLink());
}
}
int sumCall(){
int sum=sum(start);
return sum;
}
int countOdd(Node ptr){
int d=0;
if(ptr!=null){
if(((ptr.getData())%2)!=0){
d=1;
}
else{
d=0;
}
return d +countOdd(ptr.getLink());
}
return d;
}
void OddCall(){
int f=countOdd(start);
System.out.println(f);
}
//end of calculating sum using recursion
int count (Node ptr){
if(ptr==null){
return 0;
}
else{
int d=1;
return d + count(ptr.getLink());
}
}
void countCall(){
int count=count(start);
System.out.println("The number of nodes are:\t"+count);
}
/* void display(){
Node ptr=start;
while(ptr!=null)
{
System.out.println(ptr.getData());
ptr=ptr.getLink();
}
}
*/
void print (Node ptr){
if (ptr==null){
System.out.print(" ");
}
else{
System.out.println(ptr.getData());
print(ptr.getLink());
}
}
void printCall(){
print(start);
}
public static void main (String args[]) throws IOException{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
int choice,data;
LinkedList ll=new LinkedList ();
do{
System.out.println("Enter 1:insert at Start, 2:insert at end, 3:insert at position, 4:delete at start, 5:delete at end, 6:delete at positon, 7:display, 8:sum of nodes, 9:count Nodes,10:exit");
choice=Integer.parseInt(br.readLine());
switch(choice){
case 1:
System.out.println("Enter data to store");
data=Integer.parseInt(br.readLine());
ll.insertAtStart(data);
break;
case 2:
System.out.println("Enter data to store");
data=Integer.parseInt(br.readLine());
ll.insertAtEnd(data);
break;
case 3:
System.out.println("Enter data to store");
data=Integer.parseInt(br.readLine());
System.out.println("Enter position");
int pos=Integer.parseInt(br.readLine());
ll.insertAtPos(data,pos);
break;
case 4:
ll.deleteAtStart();
break;
case 5:
ll.deleteAtEnd();
break;
case 6:
System.out.println("Enter position for deletion");
pos=Integer.parseInt(br.readLine());
ll.deleteAtPos(pos);
break;
case 7:
ll.printCall();
break;
case 8:
int s=ll.sumCall();
System.out.println("The sum of nodes is:\t"+s);
break;
case 9:
ll.OddCall();
break;
case 10:
System.out.println("Exit Program");
System.exit(0);
}
}while(choice !=10);
}
}
/*
👋 Hi, I’m @aarushinair — Aarushi Nair (she/her)
🎓 CS Engineer | AI Researcher | Software Engineer | DEI Professional
💡 Interests: AI/ML/DL, Responsible Tech, Innovative Technologies, Ethics in AI
🌍 Advocate for Women in Tech | Community & Events Manager @AnitaB.org India
🎙️ Speaker | Content Creator | STEM Mentor
📫 Let’s connect: https://www.linkedin.com/in/aarushinair/
📹 YouTube: Code with Aarushi → https://www.youtube.com/channel/UCKj5T1ELHCmkGKujkpqtl7Q
🐦 Twitter/X: https://x.com/aarushinair_
📁 Portfolio, projects & talks: https://github.com/aarushinair
*/