Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package circArray;
- import java.util.NoSuchElementException;
- public class CircularArrayQueue implements MyQueue {
- private int[] circularArray;
- private int noElements;
- private int head;
- private int tail;
- public CircularArrayQueue(){
- circularArray = new int[10];
- noElements = 0;
- head = 0;
- tail = 0;
- }
- @Override
- public void enqueue(int in) {
- int n = circularArray.length;
- if(noElements == n){
- increaseArrayCap();
- }
- circularArray[tail] = in;
- noElements++;
- tail++;
- if(tail == n && noElements <= n){
- tail = 0;
- }
- }
- @Override
- public int dequeue() throws NoSuchElementException {
- int n = circularArray.length;
- int removed = circularArray[head];
- noElements --;
- head++;
- if(head == n && noElements <= n){
- head = 0;
- }
- return removed;
- }
- @Override
- public int noItems() {
- return noElements;
- }
- @Override
- public boolean isEmpty() {
- if(noElements == 0){
- return true;
- }
- else{
- return false;
- }
- }
- public int getCapacity(){
- int capacity = circularArray.length - noElements;
- return capacity;
- }
- private void increaseArrayCap(){
- int n = circularArray.length;
- int[] biggerArray = new int[2*n];
- for(int i = 0; i < n; i++){
- biggerArray[i] = circularArray[i];
- }
- circularArray = biggerArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment