-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue.java
More file actions
55 lines (45 loc) · 1.22 KB
/
Queue.java
File metadata and controls
55 lines (45 loc) · 1.22 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
import java.util.*;
import java.util.Stack;
public class Queue<T> {
public ArrayList list;
public Queue() {
// инициализация внутреннего хранилища очереди
list = new ArrayList();
}
public void enqueue(T item) {
// вставка в хвост
list.add(item);
}
public T dequeue() {
// выдача из головы
if (list.size() > 0) {
T type = (T) list.get(0);
list.remove(0);
return type;
} else
return null; // null если очередь пустая
}
public T peek(){
if (list.size() > 0){
T type = (T) list.get(0);
return type;
}else
return null;
}
public int size() {
return list.size();
// размер очереди
}
public Stack task4(Stack stack, Stack stack1) {
while (stack.size() > 0) {
stack1.push(stack.pop());
}
return stack1;
}
public Queue rotation(Queue queue, int cycle) {
for (int i = 0; i < cycle; i++) {
queue.enqueue(queue.dequeue());
}
return queue;
}
}