Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import queue.QueueInterface;
- /*
- * 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.
- */
- /**
- *
- * @author Student
- * @param <T>
- */
- public class Queue <T> implements QueueInterface <T> {
- int head;
- int tail;
- int size;
- T[] queue;
- int capacity;
- boolean full;
- //int last;
- public Queue(int capacity){
- queue = (T[]) new Object[capacity];
- this.size = 0;
- this.head = 0;
- this.tail = -1;
- this.capacity = capacity;
- }
- @Override
- public void enqueue(T t) {
- this.tail++;
- if (tail == capacity && head!=0){
- this.tail = (tail)%capacity;
- }
- if(!isFull()){
- queue[tail] = t;
- }
- else{
- expand();
- enqueue(t);
- }
- this.size++;
- }
- @Override
- public T dequeue() {
- if(head!=capacity-1){
- this.head = head++;
- this.size = size--;
- }
- else{
- this.head = 0;
- }
- return queue[head];
- }
- @Override
- public T getFront() {
- return this.queue[head];
- }
- public boolean isFull(){
- if(capacity == size){
- full = true;
- }
- return full;
- }
- public void expand(){
- this.capacity = capacity*2;
- this.queue = Arrays.copyOf(queue, capacity);
- }
- /**
- *
- * @return
- */
- @Override
- public int getLength(){
- return this.size;
- }
- }
Add Comment
Please, Sign In to add comment