-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA1List.java
More file actions
218 lines (204 loc) · 4.78 KB
/
Copy pathA1List.java
File metadata and controls
218 lines (204 loc) · 4.78 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
// Implements Dictionary using Doubly Linked List (DLL)
// Implement the following functions using the specifications provided in the class List
public class A1List extends List {
private A1List next; // Next Node
private A1List prev; // Previous Node
public A1List(int address, int size, int key) {
super(address, size, key);
}
public A1List(){
super(-1,-1,-1);
// This acts as a head Sentinel
A1List tailSentinel = new A1List(-1,-1,-1); // Intiate the tail sentinel
this.next = tailSentinel;
tailSentinel.prev = this;
}
public A1List Insert(int address, int size, int key)
{
A1List a1=new A1List(address,size,key);
if (this.key==-1&&this.next==null){
//System.err.println("Insertion after tail");
return null;
}
if (this==null){
A1List a=new A1List();
A1List a2= a.next;
a.next=a1;
a1.prev=a;
a1.next=a2;
a2.prev=a1;
return a1;
}
a1.prev=this;
a1.next=this.next;
A1List a2=this.next;
a2.prev=a1;
this.next=a1;
return a1;
}
public boolean Delete(Dictionary d)
{
if (d==null){
return false;
}
boolean t=false;
A1List a=this;
//A1List head=this.getFirst();
//A1List a=head.getNext();
while(a!=null&&!(a.next==null&&a.key==-1)){
if (a.key==d.key&&a.address==d.address&&a.size==d.size){
if(this==a){
if (this.next.next==null) {
this.key=this.next.key;
this.address=this.next.address;
this.size=this.next.size;
this.next.prev=null;
this.next=null;
return true;
}
else{
this.key=this.next.key;
this.address=this.next.address;
this.size=this.next.size;
a=a.getNext();
}
}
if (a.next==null){
a.prev.next=null;
return true;
}
if (a.prev==null){
a.next.prev=null;
}
A1List a1=a.next;
A1List a2=a.prev;
a2.next=a1;
a1.prev=a2;
return true;
}
a=a.getNext();
}
while(a!=null&&!(a.prev==null&&a.key==-1)){
if (a.key==d.key&&a.address==d.address&&a.size==d.size){
if(this==a){
if (this.next.next==null) {
this.key=this.next.key;
this.address=this.next.address;
this.size=this.next.size;
this.next.prev=null;
this.next=null;
return true;
}
else{
this.key=this.next.key;
this.address=this.next.address;
this.size=this.next.size;
a=a.getNext();
}
}
if (a.next==null){
a.prev.next=null;
return true;
}
if (a.prev==null){
a.next.prev=null;
}
A1List a1=a.next;
A1List a2=a.prev;
a2.next=a1;
a1.prev=a2;
return true;
}
a=a.prev;
}
return t;
}
public A1List Find(int k, boolean exact)
{
A1List a=this.getFirst();
//a=a.getNext();
while(a!=null&&!(a.next==null&&a.key==-1)){
if ((a.key==k&&exact==true)||(a.key>=k&&exact==false)){
return a;
}
a=a.getNext();
}
return null;
}
public A1List getFirst()
{
A1List a=this;
A1List c=a;
while(a.prev!=null){
a=a.prev;
if(a==c){
//System.err.println("cycle present");
return null;
}
}
if (a.key==-1){
return a.next;
}
//System.err.println("First node not headsentinal");
return null;
}
public A1List getNext()
{
if (this!=null){
return this.next;
}
return null;
}
public boolean sanity()
{
//A1List head = this.getFirst();
//head=head.prev;
if (this==null){
return true;
}
A1List a=this;
A1List c=a;
while(a!=null&&c!=null&&c.prev!=null){
a=a.prev;
c=c.prev.prev;
if(a==c){
//System.err.println("cycle present");
return false;
}
}
a=this;
while(a.prev!=null){
if(a.prev.next!=a){
return false;
}
a=a.prev;
}
if (a.key!=-1){
return false;
}
a=this;
c=a;
while(a!=null&&c!=null&&c.next!=null){
a=a.next;
c=c.next.next;
if(a==c){
return false;
}
}
a=this;
while(a.next!=null){
if (a.next.prev!=a){
//System.err.println("Deque has branches");
return false;
}
a=a.next;
}
if (a.key!=-1){
//System.out.println("tail sentinel not last node");
return false;
}
return true;
}
//return true;
//}
}