-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdd.java
More file actions
299 lines (276 loc) · 10.7 KB
/
Copy pathdd.java
File metadata and controls
299 lines (276 loc) · 10.7 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package lab6;
import java.math.*;
//doubly linked double ended linked list
public class dd
{
public static void main(String[] args)
{
DoublyLinkedList mylist = new DoublyLinkedList();
BigInteger p =new BigInteger("8347852664524685394671539");
// BigInteger g =new BigInteger("1725419847553");
//BigInteger gxmodp =new BigInteger("8347852664524685394671539");
BigInteger l =new BigInteger("1");
//BigInteger c1 =new BigInteger("2030735454253481748649532");
//BigInteger c2 =new BigInteger("1340127105753313239218903");
// BigInteger Pk =new BigInteger("0");
for(BigInteger i =new BigInteger("0");i!=p; i.add(l))
{
mylist.insertLast(i);
}
// mylist.insertFirst(15);
//mylist.insertFirst(5);
//Link fst = mylist.deleteFirst();
//System.out.println(fst.dData);
//Link f = new Link(18);
//Link g = new Link(8);
// f.next=g;
//f.previous=g;
// g.next=f;
// g.previous=f;
// System.out.println(f.next.dData);
//mylist.deleteFirst();
// mylist.deleteFirst();
// System.out.println(mylist.isEmpty());
/* DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(22); // insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11); // insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayForward(); // display list forward
theList.displayBackward(); // display list backward
theList.deleteFirst(); // delete first item
theList.deleteLast(); // delete last item
theList.deleteKey(11); // delete item with key 11
theList.displayForward(); // display list forward
theList.insertAfter(22, 77); // insert 77 after 22
theList.insertAfter(33, 88); // insert 88 after 33
theList.displayForward(); // display list forward*/
} // end main()
// end class DoublyLinkedApp
////////////////////////////////////////////////////////////////
}
// doublyLinked.java
// demonstrates doubly-linked list
// to run this program: C>java DoublyLinkedApp
////////////////////////////////////////////////////////////////
class Link
{
public BigInteger dData; // data item
public Link next; // next link in list
public Link previous; // previous link in list
// -------------------------------------------------------------
public Link(BigInteger d) // constructor
{
dData = d;
next=null;
previous=null;
}
// -------------------------------------------------------------
public void displayLink() // display this link
{
System.out.print(dData + " ");
}
// -------------------------------------------------------------
} // end class Link
///////////////////////////////////////////////////////////////
class DoublyLinkedList
{
private Link first; // ref to first item
private Link last; // ref to last item
//-------------------------------------------------------------
public DoublyLinkedList() // constructor
{
first = null; // no items on list yet
last = null;
}
//-------------------------------------------------------------
public boolean isEmpty() // true if no links
{
return first==null;
}
//-------------------------------------------------------------
public void insertFirst(BigInteger dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
{
last = newLink; // newLink <-- last
}
else
{
first.previous = newLink; // newLink <-- old first
}
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
//-------------------------------------------------------------
public void insertLast(BigInteger dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
{
last.next = newLink; // old last --> newLink
newLink.previous = last; // old last <-- newLink
}
last = newLink; // newLink <-- last
}
//-------------------------------------------------------------
public Link deleteFirst() // delete first link
{ // (assumes non-empty list)
Link temp = first;
if(first.next == null) // if only one item
last = null; // null <-- last
else
first.next.previous = null; // null <-- old next
first = first.next; // first --> old next
return temp;
}
//-------------------------------------------------------------
public Link deleteLast() // delete last link
{ // (assumes non-empty list)
Link temp = last;
if(first.next == null) // if only one item
first = null; // first --> null
else
last.previous.next = null; // old previous --> null
last = last.previous; // old previous <-- last
return temp;
}
//-------------------------------------------------------------
public Link findLast() // delete last link
{ // (assumes non-empty list)
Link temp = last;
/* if(first.next == null) // if only one item
first = null; // first --> null
else
last.previous.next = null; // old previous --> null
last = last.previous;*/ // old previous <-- last
return temp;
} // insert dd just after key
public boolean insertAfter(BigInteger key, BigInteger dd)
{ // (assumes non-empty list)
Link current = first; // start at beginning
while(current.dData != key) // until match is found,
{
current = current.next; // move to next link
if(current == null)
return false; // didn't find it
}
Link newLink = new Link(dd); // make new link
if(current==last) // if last link,
{
newLink.next = null; // newLink --> null
last = newLink; // newLink <-- last
}
else // not last link,
{
newLink.next = current.next; // newLink --> old next
// newLink <-- old next
current.next.previous = newLink;
}
newLink.previous = current; // old current <-- newLink
current.next = newLink; // old current --> newLink
return true; // found it, did insertion
}
//-------------------------------------------------------------
public Link deleteKey(BigInteger key) // delete item w/ given key
{ // (assumes non-empty list)
Link current = first; // start at beginning
while(current.dData != key) // until match is found,
{
current = current.next; // move to next link
if(current == null)
return null; // didn't find it
}
if(current==first) // found it; first item?
first = current.next; // first --> old next
else // not first
// old previous --> old next
current.previous.next = current.next;
if(current==last) // last item?
last = current.previous; // old previous <-- last
else // not last
// old previous <-- old next
current.next.previous = current.previous;
return current; // return value
}
public boolean findKey(BigInteger key) // delete item w/ given key
{ // (assumes non-empty list)
Link current = first; // start at beginning
while(!current.dData.equals(key)) // until match is found,
{
BigInteger i = current.dData;
current = current.next; // move to next link
if(current == null)
return false; // didn't find it
}
/*if(current==first) // found it; first item?
first = current.next; // first --> old next
else // not first
// old previous --> old next
current.previous.next = current.next;
if(current==last) // last item?
last = current.previous; // old previous <-- last
else // not last
// old previous <-- old next
current.next.previous = current.previous;*/
return true; // return value
}
public BigInteger foundKey(BigInteger key) // delete item w/ given key
{
BigInteger count =new BigInteger("0");
BigInteger inc =new BigInteger("1"); // (assumes non-empty list)
Link current = first; // start at beginning
while(!current.dData.equals(key)) // until match is found,
{
current = current.next;
count=count.add(inc); // move to next link
if(current == null)
{
BigInteger a= new BigInteger("-1");
return a;
}// didn't find it
}
/*if(current==first) // found it; first item?
first = current.next; // first --> old next
else // not first
// old previous --> old next
current.previous.next = current.next;
if(current==last) // last item?
last = current.previous; // old previous <-- last
else // not last
// old previous <-- old next
current.next.previous = current.previous;*/
return count; // return value
}
//-------------------------------------------------------------
public void displayForward()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // display data
current = current.next; // move to next link
}
System.out.println("");
}
//-------------------------------------------------------------
public void displayBackward()
{
System.out.print("List (last-->first): ");
Link current = last; // start at end
while(current != null) // until start of list,
{
current.displayLink(); // display data
current = current.previous; // move to previous link
}
System.out.println("");
}
//-------------------------------------------------------------
} // end class DoublyLinkedList
////////////////////////////////////////////////////////////////