-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdequeue.java
More file actions
129 lines (119 loc) · 3.57 KB
/
dequeue.java
File metadata and controls
129 lines (119 loc) · 3.57 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
import java.io.*;
public class dequeue
{
Node start,end;
int size;
dequeue(){
start=null;
end=null;
size=0;
}
void insertAtFront(int d){
Node nptr=new Node (d,null);
if (start==null){
start=nptr;
end=nptr;
size++;
}
else{
nptr.setLink(start);
start=nptr;
size++;
}
}
void insertAtRear(int d){
Node nptr=new Node (d,null);
if (start==null){
start=nptr;
end=nptr;
size++;
}
else{
end.setLink(nptr);
end=nptr;
size++;
}
}
void deleteAtFront(){
if(start==null){
System.out.println("No nodes to be deleted");
}
else{
start=start.getLink();
size--;
}
}
void deleteAtRear(){
if (start==null){
System.out.println("No nodes to be deleted");
}
else{
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 display(){
Node ptr=start;
while(ptr!=null)
{
System.out.println(ptr.getData());
ptr=ptr.getLink();
}
}
public static void main (String args[]) throws IOException{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
dequeue q=new dequeue ();
int choice=0;
do{
System.out.println("1.To insert at START");
System.out.println("2.To insert at END");
System.out.println("3.To delete at START");
System.out.println("4.To delete at END");
System.out.println("5.To DISPLAY");
System.out.println("6.EXIT");
choice=Integer.parseInt(br.readLine());
switch(choice){
case 1:
System.out.println("To insert value of element");
int ele=Integer.parseInt(br.readLine());
q.insertAtFront(ele);
break;
case 2:
System.out.println("To insert value of element");
ele=Integer.parseInt(br.readLine());
q.insertAtRear(ele);
break;
case 3:
q.deleteAtFront();
break;
case 4:
q.deleteAtRear();
break;
case 5:
q.display();
break;
case 6:
System.out.println("Exit program");
System.exit(0);
}
}while(choice!=6);
}
}
/*
👋 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
*/