-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueByArray.java
More file actions
160 lines (128 loc) · 2.77 KB
/
QueueByArray.java
File metadata and controls
160 lines (128 loc) · 2.77 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
import java.util.*;
public class QueueByArray{
Queue<Integer> queue = new LinkedList<Integer>();
int[] arr = new int[5];
int size=0;
int pushi = 0;
int popi = 0;
public void push_queue(int v){
queue.offer(v);
}
public void push(int v){
if(size<arr.length){
arr[pushi] = v;
pushi = getIndex(pushi);
size++;
}else{
System.out.println("队列已满");
}
}
public int pop_queue(){
if(!queue.isEmpty()){
return queue.poll();
}
return -1;
}
public int pop(){
if(size>0){
int v = arr[popi];
arr[popi] = -9999;
popi = getIndex(popi);
size--;
return v;
}else{
return -1;
}
}
public int getIndex(int index){
return (index+1) >= arr.length ? 0 : index+1;
}
public void printpushi(){
System.out.println("pushi = "+pushi+"popi = "+popi);
}
public int getPopIndex(){
return popi;
}
public int[] getArray(){
return arr;
}
public Queue<Integer> getQueue(){
return queue;
}
public void RandomOption(int pushCount, int popCount,int push_count,BecomeQueueByArray queue){
while(true){
int r = (int)(Math.random()*9999);
if(r%2 == 0){
if(pushCount >0 ){
queue.push(r);
queue.push_queue(r);
pushCount--;
}
}else{
if(popCount >0){
queue.pop();
queue.pop_queue();
popCount--;
}
}
if(pushCount == 0 && popCount ==0) {
break;
}
}
for (int i=0;i<push_count;i++ ) {
int r = (int)(Math.random()*9999);
queue.push(r);
queue.push_queue(r);
}
}
public Boolean isTrue(BecomeQueueByArray queue){
Queue<Integer> _queue = queue.getQueue();
int[] arr= queue.getArray();
int _index=queue.getPopIndex();
int index= (_index == 0) ? arr.length-1: (_index);
System.out.println("");
for (int num : arr ) {
System.out.print(num+",");
}
System.out.println();
while(!_queue.isEmpty()){
int x = _queue.poll();
System.out.print(x+",");
if(arr[index] != x){
return false;
}
index = ((index + 1) >= arr.length) ? 0 : index+1;
}
return true;
}
public static void main(String args[]){
int count = 100;
while(count-- > 0){
BecomeQueueByArray queue = new BecomeQueueByArray();
queue.RandomOption(4,3,1,queue);
if(!queue.isTrue(queue)){
int[] arr= queue.getArray();
Queue<Integer> _queue = queue.getQueue();
System.out.println();
System.out.println("begin-----------");
for (int num:arr ) {
System.out.print(num+",");
}
System.out.println();
queue.printpushi();
System.out.println();
while(!_queue.isEmpty()){
System.out.print(_queue.poll()+",");
}
System.out.println();
System.out.println("end-----------");
break;
}
}
if(count > 0){
System.out.println("出错了...");
}else{
System.out.println("成功了...");
}
}
}