AQueue - enqueue(element) } else{ end++ if(end ==...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 public class AQueue<T>{ private T[] arr private int front private int end private int count p public static class MyException extends RuntimeException{ public MyException(){ super() } public MyException(String message){ super(message) } } AQueue(int size){ arr = (T[])new Object[size] front = 0 end = -1 count = 0 } public void enqueue(T element){ if(count == arr.length){ T[] temp = (T[])new Object[arr.length*2] for(int i = 0 i < arr.length i++){ if(i+front >= arr.length){ temp[i] = arr[i-front-1] } else temp[i] = arr[i+front] } front = 0 end = arr.length - 1 arr = temp
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: enqueue(element) } else{ end++ if(end == arr.length){ end = 0 } arr[end] = element count++ } } public T dequeue(){ if(isEmpty()){ throw new MyException(&quot;The Queue is empty!&quot;) } Sheet1 Page 2 else{ if(front == arr.length-1){ front = end = 0 count--return arr[arr.length-1] } else{ front++ T temp = arr[front-1] arr[front-1] = null count--return temp } } } public boolean isEmpty(){ return (count == 0) } public void printArray(){ for(int i = 0 i&lt;arr.length i++){ System.out.print(arr[i]+&quot; &quot;) } } }...
View Full Document

Page1 / 2

AQueue - enqueue(element) } else{ end++ if(end ==...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online