Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.cvut.fel.pjv;
- /**
- * Implementation of the {@link Queue} backed by fixed size array.
- */
- public class CircularArrayQueue implements Queue {
- private int size;
- private String[] buffer;
- private int lastUse;
- /**
- * Creates the queue with capacity set to the value of 5.
- */
- public CircularArrayQueue() {
- size = 0;
- buffer = new String[5];
- lastUse = 0;
- }
- /**
- * Creates the queue with given {@code capacity}. The capacity represents maximal number of elements that the
- * queue is able to store.
- * @param capacity of the queue
- */
- public CircularArrayQueue(int capacity) {
- size = 0;
- buffer = new String[capacity];
- }
- public int size() {
- return lastUse;
- }
- public boolean isEmpty() {
- if (lastUse == 0){
- return true;
- }
- return false;
- }
- public boolean isFull() {
- return lastUse == buffer.length;
- }
- public boolean enqueue(String obj) {
- if (!isFull() && obj != ""){
- buffer[lastUse] = obj;
- lastUse++;
- }
- return false;
- }
- public String dequeue() {
- if (!isEmpty()){
- String head = buffer[0];
- for (int i =0; i < buffer.length-1;i++){
- buffer[i] = buffer[i+1];
- }
- lastUse--;
- return head;
- }
- return null;
- }
- public void printAllElements() {
- for (int i = 0; i< buffer.length; i++){
- if (buffer[i] != null){
- System.out.println(buffer[i]);
- }
- else {
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement