-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleArray.java
More file actions
155 lines (134 loc) · 3.38 KB
/
CycleArray.java
File metadata and controls
155 lines (134 loc) · 3.38 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
public class CycleArray<T> {
private T[] arr;
private int start;
private int end;
private int size;
private int count;
public CycleArray() {
this(1);
}
@SuppressWarnings("unchecked")
public CycleArray(int size) {
this.size = size;
this.count = 0;
this.arr = (T[]) new Object[size];
this.start = 0;
this.end = 0;
}
@SuppressWarnings("unchecked")
private void resize(int newSize) {
T[] newArr = (T[]) new Object[newSize];
for (int i = 0; i < count; i++) {
newArr[i] = arr[(start + i) % size];
}
arr = newArr;
start = 0;
end = count;
size = newSize;
}
private boolean isFull() {
return size == count;
}
public void addFirst(T val) {
if (isFull()) {
resize(size * 2);
}
start = (start - 1 + size) % size;
arr[start] = val;
count++;
}
private boolean isEmpty() {
return count == 0;
}
public T removeFirst() {
if (isEmpty()) {
throw new IllegalStateException("Array is empty");
}
T res = arr[start];
arr[start] = null;
start = (start + 1) % size;
count--;
if (count > 0 && count <= size / 4) {
resize(size / 2);
}
return res;
}
public void addLast(T val) {
if (isFull()) {
resize(size * 2);
}
arr[end] = val;
end = (end + 1) % size;
count++;
}
public T removeLast() {
if (isEmpty()) {
throw new IllegalStateException("Array is empty");
}
end = (end - 1 + size) % size;
T res = arr[end];
arr[end] = null;
count--;
if (count > 0 && count <= size / 4) {
resize(size / 2);
}
return res;
}
public T getFirst() {
if (isEmpty()) {
throw new IllegalStateException("Array is empty");
}
return arr[start % size];
}
public T getLast() {
if (isEmpty()) {
throw new IllegalStateException("Array is empty");
}
return arr[(end - 1 + size) % size];
}
public int size() {
return count;
}
public void print() {
for (int i = 0; i < count; i++) {
System.out.print(arr[(start + i) % size] + " ");
}
System.out.println();
}
public static void main(String[] args) {
CycleArray<Integer> cycleArray = new CycleArray<>(3);
// 测试添加元素
cycleArray.addLast(1);
cycleArray.addLast(2);
cycleArray.addLast(3);
System.out.println("After adding 3 elements: ");
cycleArray.print();
// 测试添加到头部
cycleArray.addFirst(0);
System.out.println("After adding 0 at the head: ");
cycleArray.print();
// 测试删除尾部元素
cycleArray.removeLast();
System.out.println("After removing the last element: ");
cycleArray.print();
// 测试删除头部元素
cycleArray.removeFirst();
System.out.println("After removing the first element: ");
cycleArray.print();
// 测试获取头部和尾部元素
System.out.println("First element: " + cycleArray.getFirst());
System.out.println("Last element: " + cycleArray.getLast());
// 测试数组扩容
cycleArray.addLast(4);
cycleArray.addLast(5);
cycleArray.addLast(6);
System.out.println("After adding more elements to trigger resize: ");
cycleArray.print();
// 测试数组缩容
cycleArray.removeFirst();
cycleArray.removeFirst();
cycleArray.removeFirst();
System.out.println("After removing elements to trigger resize: ");
cycleArray.print();
}
}