Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StaticQueue {
- private int[] arr;
- private int count;
- public StaticQueue() {
- arr = new int[5];
- count = 0;
- }
- public void offer(int element) throws Exception {
- add(count, element);
- }
- private void add(int index, int element) throws Exception {
- if(index > count || index < 0) {
- throw new Exception();
- }
- if(count + 1 == arr.length) {
- int[] extendetArr = new int[arr.length * 2];
- System.arraycopy(arr, 0, extendetArr, 0, arr.length);
- arr = extendetArr;
- }
- count++;
- arr[index] = element;
- }
- public int poll() {
- if(count > 0) {
- int polledElement = arr[0];
- int[] extendArr = new int[arr.length];
- System.arraycopy(arr, 1, extendArr, 0, arr.length - 1);
- arr = extendArr;
- count--;
- return polledElement;
- } else {
- return -1;
- }
- }
- public void clear() {
- count = 0;
- this.arr = new int[5];
- }
- public boolean contains(int element) {
- for(int i = 0; i < arr.length; i++) {
- if(arr[i] == element) {
- return true;
- }
- }
- return false;
- }
- public int peek() {
- if(count > 0)
- return arr[0];
- return -1;
- }
- public int size() {
- return this.count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement