Created
June 14, 2018 08:25
-
-
Save Sisekelo/f88921e8c573b1aa301760834515202e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package Queue; | |
| import java.util.Arrays; | |
| /** | |
| * | |
| * @author DELL | |
| * @param <T> | |
| */ | |
| public class Queue<T> implements QueueInterface<T> { | |
| private T[] array; | |
| public int head = 0; | |
| public int tail = 0; | |
| public int tracker = 0; | |
| //WHAT DOES THIS DO, WHAT ARE GENERICS | |
| public Queue() { | |
| this.array = (T[]) new Object[10]; | |
| } | |
| @Override | |
| public void enqueue(T t) { | |
| //if it is not full | |
| if(isFull() == false){ | |
| //if tail is more than the head but less than the length of the array or its the start of the array | |
| if((tail > head && tail < array.length) || (tail == 0 && head == 0)){ | |
| //add value to tail | |
| array[tail] = t; | |
| tail++; | |
| } | |
| //if it has reached the end of the array | |
| else if(tail >= array.length && head > 0){ | |
| tail = 0; | |
| array[tail] = t; | |
| tail++; | |
| } | |
| } | |
| else{ | |
| //if it is full, increase size of array | |
| T[] temp = (T[]) new Object[(array.length)*2]; | |
| //loops through array to add the old vx1alues | |
| //if it started from 0 | |
| if(tail > head){ | |
| //copy everything as is | |
| System.arraycopy(array, 0, temp, 0, array.length); | |
| array = temp; | |
| } | |
| else{ | |
| int lastIndex = 0; | |
| for(int i = head ; i < array.length ; i++){ | |
| temp[i] = array[i]; | |
| i++; | |
| lastIndex = i; | |
| } | |
| for(int j = 0 ; j < head ; j++){ | |
| lastIndex++; | |
| temp[lastIndex] = array[j]; | |
| tail = lastIndex++; | |
| } | |
| array = temp; | |
| array[tail] = t; | |
| tail++; | |
| } | |
| } | |
| } | |
| @Override | |
| public T dequeue() { | |
| //check if it is empty | |
| if(tracker == 0){ | |
| System.out.println("Sorry, this queue is empty!"); | |
| return null; | |
| } | |
| else{ | |
| //if head+1 is more than the length of array | |
| if(head++ > array.length && tail < head){ | |
| int oldhead = head; | |
| head = 0; | |
| tracker--; | |
| return array[oldhead]; | |
| } | |
| else{ | |
| //find head remove head | |
| //then move head to be equal to the next | |
| //tracker -1 | |
| int oldhead = head; | |
| head++; | |
| tracker--; | |
| return array[oldhead]; | |
| } | |
| } | |
| } | |
| @Override | |
| public T peek() { | |
| return array[head]; | |
| } | |
| @Override | |
| public boolean empty() { | |
| return tracker == 0; | |
| } | |
| @Override | |
| public boolean isFull(){ | |
| return tracker != 0 && tail == head; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package Queue; | |
| /** | |
| * | |
| * @author DELL | |
| * @param <T> | |
| */ | |
| public interface QueueInterface <T> { | |
| //adds object to the queue, doesnt return anything | |
| void enqueue(T t); | |
| //removes head of queue | |
| T dequeue(); | |
| //checks what object is on top of queue | |
| T peek(); | |
| //checks if queue is empty, returns boolean | |
| boolean empty(); | |
| boolean isFull(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment