mnogoQueues

 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
import java.util.ArrayList;
import java.util.LinkedList;
public class MyQueue {
private int capacity;
private ArrayList<LinkedList<String>> queue;
public MyQueue(int capacity) {
this.capacity = capacity;
queue = new ArrayList<LinkedList<String>>();
for (int i = 0; i <= capacity; i++)
queue.add(new LinkedList<String>());
}
public void put(String value) {
//LinkedList<String> elem =
queue.get(value.length()).add(value);
//elem.add(value);
//queue.add(value.length(), elem);
}
public String get() {
for (int i = 0; i <= capacity; i++) {
if (!queue.get(i).isEmpty()) {
return queue.get(i).removeFirst();
}
}
return "Queue is empty";
}
public int length() {
System.out.println("Максимальная длина слова: " + queue.size());
int count = 0;
for (int i = 0; i <= capacity; i++) {
count += queue.get(i).size();
}
return count;
}
}