GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithm/src/datastructure/queue/impl/LoopQueue.java at main · duktig666/algorithm

Expand file tree

/

LoopQueue.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
175 lines (158 loc) · 4.44 KB
/

LoopQueue.java

Copy path

File metadata and controls

175 lines (158 loc) · 4.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package datastructure.queue.impl;
import datastructure.queue.Queue;
import java.util.Scanner;
/**
* description:循环队列
*
* @author RenShiWei
* Date: 2021/5/30 16:38
**/
public class LoopQueue<E> implements Queue<E> {
/** 存储元素 数组的长度(有效长度需要-1) */
private int maxSize;
/** 队列头 */
private int front;
/** 队列尾 */
private int rear;
/** 该数据用于存放数据,模拟队列 */
private E[] data;
/**
* 初始化环形队列
*
* @param arrMaxSize 初始队列容量
*/
@SuppressWarnings("unchecked")
public LoopQueue(int arrMaxSize) {
//循环队列需要有意识浪费一个空间
maxSize = arrMaxSize + 1;
data = (E[]) new Object[maxSize];
}
/**
* @return 是否队空
*/
@Override
public boolean isEmpty() {
return front == rear;
}
/**
* @return 是否队满
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* @return 队列的可承载元素个数
*/
public int getCapacity() {
return data.length - 1;
}
/**
* @return 队列元素个数
*/
@Override
public int getSize() {
return (rear + maxSize - front) % maxSize;
}
/**
* 队尾入队
*
* @param e 入队元素
*/
@Override
public void add(E e) {
if (isFull()) {
throw new IllegalArgumentException("队列已满,不能入队!");
}
data[rear] = e;
//rear指针后移一位
rear = (rear + 1) % maxSize;
}
/**
* 队首出队
*
* @return 出队元素
*/
@Override
public E poll() {
if (isEmpty()) {
throw new IllegalArgumentException("队列为空,不能出队!");
}
E temp = data[front];
//出队位置置null
data[front] = null;
//front指针后移一位
front = (front + 1) % maxSize;
return temp;
}
/**
* 获取队首元素
* 如果队空,返回null
*
* @return 队首元素
*/
@Override
public E peek() {
return data[front];
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Queue: size = %d , capacity = %d\n", getSize(), getCapacity()));
res.append("front [");
for (int i = front; i != rear; i = (i + 1) % data.length) {
res.append(data[i]);
if ((i + 1) % data.length != rear) {
res.append(", ");
}
}
res.append("] tail");
return res.toString();
}
/**
* 队列测试
*/
public static void main(String[] args) {
LoopQueue<Integer> queue = new LoopQueue<>(5);
Scanner sc = new Scanner(System.in);
char c;
boolean loop = true;
while (loop) {
System.out.println("s(toString):输出队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("p(poll):从队列取出数据");
System.out.println("h(getHead):查看队列头的数据");
System.out.println("n(isEmpty):是否队空");
System.out.println("f(isFull):是否队满");
c = sc.next().charAt(0);
switch (c) {
case 's':
System.out.println("当前队列:" + queue.toString());
break;
case 'e':
sc.close();
loop = false;
break;
case 'a':
System.out.println("请输入一个整数");
queue.add(sc.nextInt());
break;
case 'p':
System.out.printf("出队元素:%d\n", queue.poll());
break;
case 'h':
System.out.printf("队首元素:%d\n", queue.peek());
break;
case 'n':
System.out.println("队空:" + queue.isEmpty());
break;
case 'f':
System.out.println("队满:" + queue.isFull());
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.