Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Airbnb-QueueLinkedList-2/3
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static class QueueWithFixedArray { private int fixedSize; private int count; private int head; private int tail; private List<Object> headList; private List<Object> tailList; public QueueWithFixedArray(int fixedSize) { this.fixedSize = fixedSize; this.count = 0; this.head = 0; this.tail = 0; this.headList = new ArrayList<>(); this.tailList = this.headList; } public void offer(int num) { if (tail == fixedSize - 1) { List<Object> newList = new ArrayList<>(); newList.add(num); tailList.add(newList); tailList = (List<Object>) tailList.get(tail); tail = 0; } else { tailList.add(num); } count++; tail++; } public Integer poll() { if (count == 0) { return null; } int num = (int) headList.get(head); head++; count--; if (head == fixedSize - 1) { List<Object> newList = (List<Object>) headList.get(head); headList.clear(); headList = newList; head = 0; } return num; } public int size() { return count; } } public static void main(String args[]) { QueueWithFixedArray queue = new QueueWithFixedArray(3); queue.offer(1); queue.offer(2); queue.offer(3); queue.offer(4); queue.offer(5); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); } }
run
|
edit
|
history
|
help
0
add static
JAVA # EN UZUN CÜMLEYİ EKRANA YAZDIRMA
Central Inteligence Agency
Basic_Structure
Black Jack by Craig
Smallest Multiple of N with Zeros and Ones
rextester.java1
2A
JAVA # Rastgele 15 sayıdan oluşan bir dizinin EK-EB ve Ortalamasını bulan Java Kodu
AVL-GO-FLAT