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 {
- String [] pole;
- int head =0;
- int end =0;
- public CircularArrayQueue() {
- pole = new String [5];
- }
- public CircularArrayQueue(int capacity) {
- pole = new String [capacity];
- }
- @Override
- public int size() {
- int size=0;
- for (String x : pole ){
- if (x!=null){
- size++;
- }
- }
- return size;
- }
- @Override
- public boolean isEmpty() {
- if (pole[head]==null){
- return true;
- }
- return false;
- }
- @Override
- public boolean isFull() {
- if(head-end==0 && pole[head]!=null ){
- return true;
- }
- return false;
- }
- @Override
- public boolean enqueue(String obj) {
- if(this.isFull() || obj==null){
- return false;
- }
- if (this.isEmpty()){
- pole[head]=obj;
- }
- pole[end]=obj;
- if(end==pole.length-1){
- end=0;
- } else{
- end++;
- }
- return true;
- }
- @Override
- public String dequeue() {
- if (!this.isEmpty()){
- String deq=pole[head];
- pole[head]=null;
- if(head==pole.length-1){
- head=0;
- }else {
- head++;
- }
- return deq;
- }
- return null;
- }
- @Override
- public void printAllElements() {
- for(String x : pole ){
- if(x!=null){
- System.out.println(x);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement