Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package cz.cvut.fel.pr1;
- /**
- *
- * @author
- */
- public class CircularArrayQueue implements Queue {
- String[] array;
- int headIndex = 0;
- int lowIndex = 0;
- public CircularArrayQueue() {
- array = new String[5];
- }
- public CircularArrayQueue(int capacity) {
- array = new String[capacity];
- }
- /**
- * It will count number of elements in CircularArrayQueue, not a size of the
- * structure
- *
- * @return the number of elements in CircularArrayQueue (int)
- */
- public int size() {
- int size;
- size = headIndex-lowIndex;
- return size;
- }
- /**
- * It will check if CircularArrayQueue is empty
- *
- * @return <code>true</code> if the CircularArrayQueue is empty and contains
- * no elements <code>false</code> otherwise (CircularArrayQueue contains
- * atleast one element).
- */
- public boolean isEmpty() {
- if (size() == 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * It will check if CircularArrayQueue is full
- *
- * @return <code>true</code> if the CircularArrayQueue is full
- * <code>false</code> otherwise.
- */
- public boolean isFull() {
- if (size() == array.length) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * This method will enqueue the String to CircularArrayQueue
- *
- * @param obj the String object that we want add to CircularArrayQueue
- * @return <code>true</code> if the obj param is added to CircularArrayQueue
- * <code>false</code> otherwise (if CircularArrayQueue is full or if we obj
- * param is null).
- */
- public boolean enqueue(String obj) {
- if (isFull() == true || obj == null) {
- return false;
- } else {
- array[headIndex] = obj;
- headIndex++;
- return true;
- }
- }
- /**
- * This method will dequeue the first String from CircularArrayQueue
- *
- * @return <code>String value</code> if the CircularArrayQueue is not empty
- * <code>null</code> otherwise (if CircularArrayQueue is empty).
- */
- public String dequeue() {
- if (isEmpty() == true){
- return null;
- } else {
- String halp = array[lowIndex];
- array[lowIndex] = null;
- return halp;
- }
- }
- /**
- * It will print all elements from CircularArrayQueue
- */
- public void printAllElements() {
- for (int i = lowIndex; i <= headIndex; i++){
- System.out.println(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement