Repository navigation
Expand file tree
/
Copy pathQueue.java
More file actions
100 lines (87 loc) · 3.48 KB
/
Copy pathQueue.java
File metadata and controls
100 lines (87 loc) · 3.48 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
import java.util.Queue;
class MyQueue {
private int size;
private int rear;
private int front;
private int nItems;
private long[] queArr;
//------------------------------------------------ конструктор
public MyQueue(int size){
this.size = size;
queArr = new long[size];
rear = -1;
front = 0;
nItems = 0;
}
//------------------------------------------------ вставка элемента в конец очереди
public void insert(long v){
if(rear == size-1) //циклический перенос
rear = -1;
if(isFull()) //проверка на переполение очереди
System.out.println("Опередь переполнена!");
else {
queArr[++rear] = v;
nItems++;
}
}
//------------------------------------------------ извлечение элемента из начала очереди
public long remove(){
long temp = queArr[front++];
if(front == size) //циклический перенос
front = 0;
if(isEmpty()) { //проверка на отсутсвие элементов
System.out.println("");
System.out.print("Опередь пуста! Количество элементов в очереди = ");
return 0;
}
else {
nItems--;
return temp;
}
}
//------------------------------------------------ true, если массив пуст.
public boolean isEmpty(){ //версия с счётчиком элементов
return nItems==0;
}
/*
public boolean isEmpty(){ //версия без счёткика элементов.
return (rear+1 == front || (front+size-1 == rear));
}
*/
//------------------------------------------------ true, если массив полон.
public boolean isFull(){ //версия с счётчиком элементов
return nItems==size;
}
/*
public boolean isFull(){ //версия без счёткика элементов.
return (rear+2 == front || (front+size-2 == rear));
}
*/
//------------------------------------------------ количество заполненных элементов.
/* public int size(){ //версия с счётчиком элементов
return nItems;
}
*/
}
////////////////////////////////////////////////////////////////////////////////////////////
class QueueApp{
public static void main(String[] args){
MyQueue theQueue = new MyQueue(5); //очередь из 5 ячеек
theQueue.insert(10); //вставка 4х элементов
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove(); //извлечение 3х элементов
theQueue.remove();
theQueue.insert(50); //вставка ещё 4х элементов с циклическим переносом
theQueue.insert(60);
theQueue.insert(70);
// theQueue.insert(80);
while (!theQueue.isEmpty()){ //извлечение и вывод всех элементов
long n = theQueue.remove();
System.out.print(n);
System.out.print(" ");
}
System.out.println();
}
}