-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathOfferStack.java
executable file
·54 lines (53 loc) · 1.46 KB
/
OfferStack.java
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
package ca.mcmaster.offer;
public class OfferStack {
private int capacity;
private Integer[] arr;
public void display(){
for(int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public OfferStack(int capacity){
this.capacity = capacity;
arr = new Integer[capacity * 3 + 3];
arr[0] = 1;
arr[1 + capacity] = 2 + capacity;
arr[2 + capacity * 2] = 3 + 2 * capacity;
}
public void push(Integer v, int index){
int writeIndex = arr[index + index * capacity];
if(writeIndex >= (index + 1) * (1 + capacity) )
throw new ArrayIndexOutOfBoundsException();
arr[writeIndex] = v;
arr[index + index * capacity] = ++writeIndex;
}
public Integer pop(int index){
int readIndex = arr[index + index * capacity] - 1;
Integer v = arr[readIndex];
arr[readIndex] = null;
if(v == null)
throw new ArrayIndexOutOfBoundsException();
arr[index + index * capacity] = readIndex;
return v;
}
public static void main(String[] args) {
OfferStack stack = new OfferStack(5);
stack.push(1, 0);
stack.push(2, 0);
stack.push(3, 0);
stack.push(4, 0);
stack.push(5, 0);
stack.push(5, 0);
// stack.display();
stack.push(2, 1);
stack.push(1, 1);
// System.out.println(stack.pop(0));
// System.out.println(stack.pop(0));
System.out.println(stack.pop(0));
// stack.display();
System.out.println(stack.pop(0));
System.out.println(stack.pop(0));
System.out.println(stack.pop(1));
System.out.println(stack.pop(1));
}
}