s+ }" ^! j2 h9 M7 ] /**5 y- n; F0 N6 n" S& m" Z
* 获取队列大小& z0 g# A% N- r2 @. C
* @return 队列大小8 }9 g0 ~$ `* Y- w! _5 k3 D3 ^7 G1 [, t- Z
*/ , r4 k2 U9 y) }. @ int getMaxSize();$ @9 D" R7 I; r! e* l0 ]# c
+ L4 J. Q/ t1 R3 f. h& B
/** - ?" {3 I1 n* B c' I! Q2 A * 入队 " @0 U$ L R& t+ K6 M/ r# a * @param object 入队元素 0 P+ L: H+ ?- U# r! N' D3 v */2 }; D! M& q) K" S
void push(Object object); * [7 `% S3 v# F5 | * r g$ P, @- t: t0 H# ?0 T H) Q# G /** " X! K) u7 x7 w$ \/ a6 |+ C * 出队6 K: O: c% a/ y* Z: E* f
* @return 出栈元素 8 s& R; \$ u* w" \' W: d* _+ f$ J */ ) Q+ _9 {6 a5 I H% `, e Object pull();8 L# Q- [$ F- j* G; b% h/ A$ O
# \ K# h5 j. w7 n
/** : a5 r9 c6 z" X * 获取元素个数! j# D+ E7 c8 x3 U: f& K
* @return 元素个数% d* V4 G/ {+ c: }9 K0 G
*/ ( B2 f9 |8 Q- S% u. C int getElementCount();: M; H; L8 J1 O
. g; z+ o2 b- @$ P( F
/** 6 ~( u* d9 F1 H) K3 O * 获取队头元素- q9 u* V3 G$ l7 r% s h
* @return 队头元素0 W. ~1 M' }* z/ n2 t
*/ # m0 F1 F" {3 `8 l Object getFront(); 1 j5 O! [- E7 r8 W) I5 P$ _+ X 7 b& L0 P0 W. ^; Y& d$ @, h9 R. E /**' d; P1 A2 l8 [8 w! S
* 获取队尾元素 ( D7 Q) J d7 T * @return 队尾元素( ^8 m; L. q: \1 ^/ f3 Z" a5 [
*/ # L4 S2 r# j) W) ?1 O. f Object getRear(); " ~( P$ O, }+ K Q) ^ 4 V* T7 i6 \& W /**7 z* ]6 \4 d I/ p/ J6 K
* 遍历队列的元素 I0 y j5 x8 d# N */, r7 p8 [& C- r9 D
void traverse(); " M6 ]) f- u$ S( z9 t+ |" g0 q% ^) z0 u}( B( L: Z1 b9 ?( [) M) L 2、队列的接口实现 8 B2 q* [( K' p3 _0 Y6 [/**" K7 j. i. l6 l' V6 K0 I
* 队列的接口实现4 D% A! _) l# [2 H
*8 y1 Y, N" p- g: K9 Q7 {8 K7 m, K
* @author zhuhuix ; b0 n0 e4 s0 N* p5 _ N7 Y * @date 2020-05-012 Q$ W; S, j) c! z
*/ . S1 ?) X* q9 Apublic class QueueImpl implements Queue { ) W# _/ T! B6 V8 f( f6 R9 W " m& N% r: t7 Q protected Object[] element;8 E) Y" o% s3 [7 [! l9 l5 E
% X7 }5 _7 ^2 D protected int elementCount;( v* P: J+ u O% r
6 C4 t3 n$ K5 L0 Z2 C3 U! {- Y! y
//队头) |; A- x+ z3 \! {; G, J$ G
private int front;# E. t+ Z3 [# \# S8 e
/ Q; N% v' G% d //队尾0 |: K& n7 n$ a7 Q" @ [) ^" ~& p5 p
private int rear; & q# ]4 b; A5 ~- m 2 G: S9 t4 m: @) R private int defaultSize = 16;4 F# s4 j7 Q$ n, Y
( R: ^+ q& s$ n private int maxSize;8 U% {8 _, O6 k5 H: w% c1 H8 K
& s! E/ Q% Q9 C QueueImpl() { ; r1 l _( a. b$ l/ u* { m7 h* t element = new Object[defaultSize]; 7 i+ j/ m e# D" T. w. Q/ e) @ maxSize = defaultSize;4 G- j; `( T6 Z
front = 0;3 x6 q" Y! |) S: X7 m& F, b
rear = -1; 8 {8 z w* }/ s6 y4 F }! h! y8 p, s! k3 E J
& C/ }/ s$ N7 w" A) f6 q! p5 T3 Z" C
QueueImpl(int size) { , v, J; F% S9 T4 j* ? element = new Object[size];7 U9 k4 e, r S( x6 D; j; T! x, ]5 J5 b
maxSize = size; * x4 \$ x! o& ]# ^$ |3 C front = 0; " x# m+ R1 s3 S& Z% m8 a rear = -1; ( A1 J' V `1 j. O7 j3 `( d" |/ W7 u( q } . U9 w% j# }- E2 x+ C' y5 j3 j1 A. s$ h0 Z& O4 o! j# V- D7 N* D
@Override 3 X8 G+ u; l7 f+ S0 x public int getMaxSize() {- S& n, q$ I* h/ L- n
return maxSize; " X j8 r5 i3 C5 I# H4 D* P2 t }; g# d3 K0 o {4 j- {' w8 o K1 G
* z5 ^- y/ f& B$ `5 y3 w' m
@Override " o8 f/ P. g( h) t* j, J0 ^7 ^ public void push(Object object) { 3 S4 ~& o; B$ ^* T7 k //如果元素个数已经达到数组的最大个数,则进行扩容- f" p% f% a* Z! {
if (elementCount == maxSize) {/ U% o$ D" ], ^1 f- w( K
throw new ArrayIndexOutOfBoundsException("队列已满,请先进行出队");+ g3 E# ]! v9 Q3 s3 @" b
}# |8 z2 r- A! D$ T4 f. G
element[++rear] = object;5 M6 g, \0 J# m8 X
if (rear == element.length) {& l5 G, y" F. U
rear = -1; ; ~& G) B& F! |9 L } 6 e% Q5 }* J8 O6 {% f) a. R' [* s elementCount++; + S; d; ~$ E& r# I* J8 G7 ^: ? }" x1 q5 L$ c/ R. Q B: w! G
! T- j) P7 x" F8 _4 b- { @Override6 D) C) r6 R9 ^( P$ ]0 Y& ^
public Object pull() {8 Y; @" B& c& X3 V6 y
if (elementCount == 0) { # O5 `) g4 Z: c9 P6 _# ]1 }% E throw new ArrayIndexOutOfBoundsException("队列中无元素"); 8 i+ n( \5 I. e' k: @. P. c } ) N& E7 i+ i. I' X7 E) p4 s Object object = element[front]; , x# L, M( j1 t. R; W8 H' E' n: d element[front] = null;+ ?' j/ S2 F) e( V% f; j
front++; - {( C2 J/ f9 T elementCount--; 4 f4 ]5 o" N4 K8 B" M, [ //队列清空,队头队尾恢复初始值 * k& k& g: D5 l- R% C2 C, C if (elementCount == 0) {) W) \, a; Y6 S$ a2 E" M$ Q
front = 0; ' z) `4 x8 x: n rear = -1; ! a$ H; c5 I% C }) p* b: I2 f! ~; Y! D) d
return object;3 i2 c" C2 v- X/ V: g9 p0 t
} 6 c, \' q* q( u* [6 |( a8 `6 _) b2 H4 X% o2 f
@Override 7 k& p0 K( k6 }" w. o; A public int getElementCount() { . u/ g$ A0 v v5 @- z' g4 d: p return elementCount; ) {" r. W2 o2 E3 i" h7 a }# F& {. q' z7 s7 P' T' s+ b: h
N8 R" R0 i) `4 f4 L
@Override ?2 Y% k8 }9 }: h7 |6 m public Object getFront() {& P. S3 q. H0 Q3 B# W3 t+ b! ~' N
if (elementCount == 0) { * E& m6 k6 i% c2 g System.out.print("队头无元素");' c$ D5 Z/ {. G* \1 Q/ R
return null;6 c" m* B M. N5 e5 Q' c7 P
} + u5 T! e0 n2 ?: o! R8 d return element[front];2 j7 L" W+ ^& ]* y
} ; Y7 d6 O& F6 [1 L% B7 Q 0 @$ p, P+ N0 `$ m6 L, Z @Override {8 y. i! u% H7 U, W, I. [ public Object getRear() { % c+ Y4 M" ?1 [4 R3 O if (elementCount == 0) { ! i! O1 ?9 _" w Q. Z System.out.print("队尾无元素");) Y0 c3 _& I c$ O6 E' j g& _
return null; , V. c6 R1 m/ d+ [% _% M+ h" j& N }; `& j5 F2 ]5 g& E1 v2 }* C
return element[rear]; 5 ~/ q) Y8 d j6 L } 8 Q/ x9 A. U8 `5 ]3 l4 M0 S" t" x' w9 N0 B- j
@Override $ ]- B( k$ E$ b# p( h0 u public void traverse() {' g! D0 _- L8 g) {- |' @
if (elementCount == 0) {) {+ g7 X3 Y" r9 V
return;7 t* {0 E, w7 n, t+ _) ?6 y' H; E+ z
} 8 s3 O/ a, p! F for (int i = front; i <= rear; i++) {1 _- ~; T" K* M; U9 x
System.out.print(element + ","); - ~# H8 n1 U; M: J }) V: }6 N& ^! m
System.out.println(); ' V2 q a0 r' B- F& y }! \3 u5 i$ \* l4 E# n% [! i
} / x. [! B' l% R2 b, U+ |' p1 C4 y" a8 h: G# c6 b
( r6 [ @. K! Q5 s! N; E 3、队列的测试 n& C8 B) A: I& p* W0 l
public class QueueTest { / M7 M9 b# G& ~/ V7 I" H' [- ^ public static void main(String[] args) { ( z4 P6 d; D: ?% _" A z2 ^8 A% E Queue queue = new QueueImpl(); ' a" a) i+ v" W" f0 U% Y) J# i
//获取队列大小 6 x) n4 T/ P/ A, H! F" L3 ~$ ~ System.out.println("队列中最大可放置元素:" + queue.getMaxSize());% k7 N; T) p, }$ L6 X. ^2 e) ~
/ ]' X% U: S7 @+ a4 X8 N9 } //第一次入队列:压入1-15 7 P, d& n! o: V, Z- W for (int i = 0; i < 16; i++) {, u- G; n- X8 F( f5 t' v
queue.push(i);7 `$ p6 [; y7 k F0 [( Z
} ) y) f5 `! j( i; w0 t% x* K# k System.out.println("第一次入队后元素个数为:" + queue.getElementCount());6 I. w+ M, ?( e3 ?& i* K& X
queue.traverse(); + s" l" t$ f( Z8 ]# U# [( v% Q System.out.println("队头:"+queue.getFront()+" 队尾:"+queue.getRear());5 m/ f5 B/ s& }8 ^& e/ i1 @: ?
, _% ]# N- R; ?3 n
//第一次出队:取出0-15 ' X: F7 s. N8 V for (int i = 0; i < 16; i++) {8 A* D" n' ^/ g+ r
queue.pull(); f* G |2 z4 E# I$ w7 h& Y- o }- B5 Y8 X. R' F) n' j1 }2 n
System.out.println("第一次出队后元素个数为:" + queue.getElementCount()); % C1 P$ O$ `8 h- X" e2 a# Z6 ` queue.traverse(); 3 \ S4 p0 [& q( f3 S8 O G7 o ] System.out.println("队头:"+queue.getFront()+" 队尾:"+queue.getRear()); _& G$ E. l/ Q. ~( i" e w; w3 Q: o! |: T9 p$ }) _3 q
. {7 \- \% d5 a1 v( V: q //第二次入队列:压入16,31 6 p4 o i1 c. y$ K* c; x8 i% M for (int i = 16; i < 32; i++) {* G6 h8 F: C+ \% m
queue.push(i); $ A) \( j W; R1 Y) D } % P( F) I' _/ w8 e6 ^- z9 Q System.out.println("第二次入队后元素个数为:" + queue.getElementCount());4 x! B$ m: O y& L- a' s' Y
queue.traverse(); ' K! i4 j8 b+ P( B; F# V4 H9 L5 {) f" a- H# } ^6 Z9 B R8 p
8 Y1 g2 K) h. Q0 `8 o3 F5 |0 {
//第二次出队:取出16-319 E/ o! T9 K8 z* j& d: @
for (int i = 0; i < 16; i++) { ! Q8 W8 U8 w) x8 t6 ~ queue.pull(); 9 w' x7 J5 @1 ~8 ^: n } # w* ^; D' b7 n: Q) d. ~ System.out.println("第二次出队后元素个数为:" + queue.getElementCount());/ e3 \) ?! ^' ?+ g
queue.traverse();2 u1 S! l- a+ q& y