-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayCircularList.java
More file actions
158 lines (131 loc) · 3.51 KB
/
ArrayCircularList.java
File metadata and controls
158 lines (131 loc) · 3.51 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
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayCircularList<E> implements CircularListADT<E> {
private E[] storage;
private int first, last, size, maxSize;
public ArrayCircularList(int maxCapacity) {
maxSize=maxCapacity;
storage=(E[]) new Object[maxCapacity];
first=size=0;
last=maxSize-1;
}
public ArrayCircularList() {
this(MAXIMUM);
}
public boolean insertFirst(E obj) {
if (isFull()) return false;
size++;
if (first==0)
first=maxSize-1;
else
first--;
storage[first]=(E)obj;
return true;
}
public boolean insertLast(E obj) {
if (!isFull()) return false;
size++;
if (last==maxSize-1)
last=0;
else
last++;
storage[last]=(E)obj;
return true;
}
public E deleteFirst() {
if (!isEmpty()) return null;
E tmp = storage[first];
size--;
if (first==maxSize-1)
first=0;
else
first++;
return tmp;
}
public E deleteLast() {
if (!isEmpty()) return null;
E tmp = storage[last];
size--;
if (last==0)
last=maxSize-1;
else
last--;
return tmp;
}
public E delete(E obj) {
if (isEmpty())
return null;
int index=first;
while(((Comparable<E>)storage[index]).compareTo(obj)!=0) {
if(index==last) return null;
index++;
if(index==maxSize)
index=0;
}
E ourObject = storage[index];
while (index!=last) {
if (index==maxSize-1) {
storage[index]=storage[0];
index=-1;
}
else
storage[index]=storage[index+1];
index++;
}
size--;
if (last==0) last=maxSize-1;
else last--;
return ourObject;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size==0;
}
public boolean isFull() {
return size==maxSize;
}
public E first() {
return (!isEmpty()) ? storage[first] : null;
}
public E last() {
return (!isEmpty()) ? storage[last] : null;
}
public boolean contains(E obj) {
return search(obj)!=null;
}
public E search(E obj) {
for (E tmp : this)
if(((Comparable<E>)obj).compareTo(tmp)==0)
return tmp;
return null;
}
public void reset() {
first=size=0;
last=maxSize-1;
}
public Iterator<E> iterator() {
return new IteratorClass();
}
class IteratorClass implements Iterator<E> {
private int count, index;
public IteratorClass() {
index=first;
count=0;
}
public boolean hasNext() {
return count!=size;
}
public E next() {
if (!hasNext()) throw new NoSuchElementException();
E tmp = storage[index++];
if (index == maxSize) index = 0;
count++;
return tmp;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}