-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoublyC.java
204 lines (167 loc) · 5.21 KB
/
DoublyC.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
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.util.*;
/* Static inputs of dll are commented at the end of code
if required please insert them in the code to verify
this below code is for implementation any number of operations */
class Doublyll{
public Node head;
class Node{
int data;
Node prev;
Node next;
}
public Doublyll(){
head=null;
}
public Node search(int data){
Node temp=head;
while(temp!=null&& temp.data!=data){
temp=temp.next;
}
return temp;
}
public boolean insert(int data,Node ptr,int n){
Node newNode = new Node();
newNode.data=data;
newNode.prev=null;
newNode.next=null;
if(newNode==null)
return false;
if(ptr==null){
Node temp;
if(head!=null&&n==0){
temp=head;
head=newNode;
newNode.next=temp;
newNode.prev=temp.prev;
}
else if(head!=null&&n==1){
temp=head.prev;
temp.next=newNode;
newNode.next=head;
newNode.prev=temp;
}
else{
head=newNode;
head.next=head;
head.prev=head;
}
}
else{
newNode.next=ptr.next;
ptr.next=newNode;
newNode.prev=ptr;
(newNode.next).prev=newNode;
}
return true;
}
public void display(){
Node temp=head;
System.out.println(head.data);
temp=temp.next;
while(temp.next!=head){
System.out.println(temp.data);
temp=temp.next;
}
}
public int Delete(int data){
Node temp=head;
if(temp==null)
return -1;
while(temp!=null&&temp.data!=data){
temp=temp.next;
}
if(temp==null)
return -1;
else{
if(temp.prev!=temp){
(temp.prev).next=temp.next;
(temp.next).prev=temp.prev;}
else
head=null;
display();
return 1;
}
}
public int ret(Node hea){
return hea.data;
}
public int ret1(Node h){
return (h.prev).data;
}
}
public class DoublyC{
public static void main(String []args){
Doublyll d= new Doublyll();
boolean temp=true;
int choice;
int data;
int n;
Scanner s= new Scanner(System.in);
while(temp){
System.out.println("1 to insert");
System.out.println("2 to delete");
System.out.println("3 to display");
System.out.println("4 to exit");
choice=s.nextInt();
switch(choice){
case 1:
data=s.nextInt();
System.out.println("if you want to insert after something then eneter 1 then data point : ");
choice=s.nextInt();
if(choice==1){
choice=s.nextInt();
if(d.insert(data, d.search(choice), 0))
d.display();
}
else{
System.out.println("Enter 0 if starting insert else 1");
n=s.nextInt();
if(n==0){
if(d.insert(data, null, n))
d.display();
}
else{
if(d.insert(data, null, 1))
d.display();
}
}
break;
case 2:
System.out.println("enetr 1 to delete at starting 2 to end and 3 to delete specific");
choice=s.nextInt();
if(choice==1){
d.Delete(d.ret(d.head));
}
else if(choice==2){
d.Delete(d.ret1(d.head));
}
else{
choice=s.nextInt();
d.Delete(choice);
}
d.display();
break;
case 3:
d.display();
break;
case 4:
temp=false;
break;
default:
System.out.println("Enter a proper choice");
}
}
}
}
/*d.insert(66,null,0);
d.insert(33,null,0);
d.insert(4,null,0);
d.insert(59,null,0);
d.insert(9,null,0);
d.insert(8,null,0);
d.insert(1,null,0);
d.insert(5,null,0);
d.insert(2,null,0);
d.Delete(2);
d.Delete(8);
*/