双端队列

  1 public class ArrayDeque<E> 
2 {
3
4 private static final int MIN_INITIAL_CAPACITY = 16;
5
6 private E[] elements;
7
8 private int head;
9
10 private int tail;
11
12
13 public ArrayDeque()
14 {
15 elements = (E[]) new Object[MIN_INITIAL_CAPACITY];
16 }
17
18
19 public ArrayDeque(int initialCapacity)
20 {
21 //elements = (E[]) new Object[initialCapacity];
22 allocateElements(initialCapacity);
23 }
24
25
26 public void allocateElements(int numElements)
27 {
28 int init = MIN_INITIAL_CAPACITY;
29
30 // 返回一个2的N次方的整数
31 if (numElements > init) {
32 init = numElements;
33 init |= (init >>> 1);
34 init |= (init >>> 2);
35 init |= (init >>> 4);
36 init |= (init >>> 8);
37 init |= (init >>> 16);
38 init++;
39
40 if (init < 0) init >>>= 1;
41 }
42
43 elements = (E[]) new Object[init];
44 }
45
46
47 // 双倍扩容
48 public void doubleCapacity()
49 {
50 int position = head;
51 int length = elements.length;
52 int newPosition = length - position;
53 int newCapacity = length << 1;
54
55 E[] newArray = (E[]) new Object[newCapacity];
56
57 System.arraycopy(elements, position, newArray, 0, newPosition);
58 System.arraycopy(elements, 0, newArray, newPosition, position);
59
60 elements = newArray;
61
62 head = 0;
63 tail = length;
64 }
65
66
67 public void addFirst(E e)
68 {
69 if (e == null)
70 throw new NullPointerException();
71
72 head = (head - 1) & (elements.length - 1);
73 elements[head] = e;
74
75 if (head == tail)
76 doubleCapacity();
77 }
78
79
80 public void addLast(E e)
81 {
82 if (e == null)
83 throw new NullPointerException();
84
85 elements[tail] = e;
86 tail = (tail + 1) & (elements.length - 1);
87
88 if (tail == head)
89 doubleCapacity();
90 }
91
92
93 private E getFirst()
94 {
95 int index = head;
96 E value = elements[index];
97
98 if (value == null) return null;
99 elements[index] = null;
100 head = (index + 1) & (elements.length - 1);
101
102 return value;
103 }
104
105
106 private E getLast()
107 {
108 int index = (tail - 1) & (elements.length -1);
109 E value = elements[index];
110
111 if (value == null) return null;
112 elements[index] = null;
113 tail = index;
114 return value;
115 }
116
117
118 public E removeFirst()
119 {
120 E e = getFirst();
121
122 if (e == null)
123 throw new NoSuchElementException();
124
125 return e;
126 }
127
128
129 public E removeLast()
130 {
131 E e = getLast();
132
133 if (e == null)
134 throw new NoSuchElementException();
135
136 return e;
137 }
138
139
140 public int size()
141 {
142 System.out.println(tail - head);
143 System.out.println(elements.length - 1);
144 return (tail - head) & (elements.length - 1);
145 }
146
147
148 public boolean isEmpty()
149 {
150 return head == tail;
151 }
152
153
154 public boolean contains(E e)
155 {
156 if (e == null) return false;
157
158 // mask为2的N次方-1
159 int mask = elements.length - 1;
160 int i = head;
161
162 // 当i为element.length时mask & i = 0
163 E x;
164 while ((x = elements[i]) != null) {
165 if (x.equals(e)) return true;
166 i = (i + 1) & mask;
167 }
168
169 return false;
170 }
171
172
173 public static void main(String[] args)
174 {
175 ArrayDeque<Integer> queue = new ArrayDeque<Integer>(11);
176
177 queue.addFirst(3);
178 queue.addFirst(9);
179 queue.addFirst(6);
180
181 queue.addLast(9);
182 queue.addLast(10);
183 queue.addLast(7);
184
185 int f1 = queue.removeFirst();
186 int f2 = queue.removeFirst();
187
188 int l1 = queue.removeLast();
189 int l2 = queue.removeLast();
190
191 System.out.println(f2);
192 System.out.println(l2);
193 }
194
195 }



原文地址:https://www.cnblogs.com/rilley/p/2410052.html