Deque(by Array) 구현 코드

한시삼십사분·2022년 1월 13일

자료구조 JAVA

목록 보기
12/16
post-thumbnail

Queue Interface

package Interface_form;

public interface Queue<E> {
	
	/**
	 * 
	 * @param data queue에 추가할 요소
	 * @return 정상적으로 추가되었을 때 true 반환
	 */
	boolean offer(E data);
	
	/**
	 * queue의 첫번 째 요소를 삭제하고 해당 요소를 반환
	 * @return queue의 첫번 째 요소
	 */
	E poll();
	
	/**
	 *	queue의 첫번 째 요소를 삭제하지 않고 반환
	 * @return Queue의 첫번 쨰 요소
	 */
	E peek();
}

ArrayDeque class

import java.util.NoSuchElementException;

import Interface_form.Queue;

public class ArrayDeque<E> implements Queue<E>{
	
	private static final int DEFAULT_CAPACITY = 64;
	
	private Object[] array;
	private int size;
	
	private int front;
	private int rear;
	
	public ArrayDeque() {
		this.array = new Object[DEFAULT_CAPACITY];
		this.size = 0;
		this.front = 0;
		this.rear = 0;
	}
	
	public ArrayDeque(int capacity) {
		this.array = new Object[capacity];
		this.size = 0;
		this.front = 0;
		this.rear = 0;
	}
	
	public void resize(int newCapacity) {
		int currCapacity = array.length;
		
		Object[] newArray = new Object[newCapacity];
		
		for(int i=1, j = front + 1; i <= size; i++, j++) {
			newArray[i] = array[j%currCapacity];
		}
		
		this.array = null;
		this.array = newArray;
		
		this.front = 0;
		this.rear = size;
	}
	
	//offer method
	public boolean offer(E data) {
		return offerLast(data);
	}
	
	public boolean offerLast(E data) {
		if((rear+1) % array.length == front) {
			resize(array.length * 2);
		}
		array[(rear+1)%array.length] = data;
		size++;
		
		return true;
	}
	
	public boolean offerFirst(E data) {
		if((rear+1) % array.length == front) {
			resize(array.length * 2);
		}
		array[front] = data;
		front = (front-1+array.length) % array.length;
		size++;
		
		return true;
	}
	
	
	// poll/remove method
	public E poll() {
		return pollFirst();
	}
	
	@SuppressWarnings("unchecked")
	public E pollFirst() {
		if(size == 0) {
			return null;
		}
		front = (front+1)%array.length;
		E element = (E) array[front];
		array[front] = 0;
		size--;
		
		if(size<(array.length/4) && array.length>DEFAULT_CAPACITY) {
			resize(Math.max(DEFAULT_CAPACITY, array.length/2));
		}
		
		return element;
	}
	
	public E remove() {
		return removeFirst(); 
	}
	
	public E removeFirst() {
		E element = pollFirst();
		if(element == null) {
			throw new NoSuchElementException();
		}
		return element;
	}
	
	@SuppressWarnings("unchecked")
	public E pollLast() {
		if(size == 0) {
			return null;
		}
		
		E element = (E) array[rear];
		array[rear] = null;
		rear = (rear-1+array.length) / array.length;
		size--;
		
		if(size<(array.length/4) && array.length>DEFAULT_CAPACITY) {
			resize(Math.max(DEFAULT_CAPACITY, array.length/2));
		}
		
		return element;
	}
	
	public E removeLast() {
		E element = pollLast();
		if(element == null) {
			throw new NoSuchElementException();
		}
		return element;
	}
	
	
	//peek/ element method
	public E peek() {
		return peekFirst();
	}
	
	@SuppressWarnings("unchecked")
	public E peekFirst() {
		if(size == 0) {
			return null;
		}
		E element = (E) array[(front+1) % array.length];
		return element;
	}
	
	@SuppressWarnings("unchecked")
	public E peekLast() {
		if(size == 0) {
			return null;
		}
		E element = (E) array[rear];
		return element;
	}
	
	public E element() {
		return getFirst();
	} 
	
	public E getFirst() {
		E element = peekFirst();
		if(element == null) {
			throw new NoSuchElementException();
		}
		return element;
	}
	
	public E getLast() {
		E element = peekLast();
		if(element == null) {
			throw new NoSuchElementException();
		}
		return element;
	}
	
	// size, isEmpty, contains, clear
	public int size() {
		return size;
	}
	
	public boolean isEmpty() {
		return size == 0;
	}
	
	public boolean contains(E data) {
		int start= (front+1) % array.length;
		for(int i=0, index= start;i<size; i++, index = (index+1)%array.length) {
			if(array[index].equals(data)) {
				return true;
			}
		}
		return false;
	}
	
	public void clear() {
		for(int i=0; i<size; i++) {
			array[i] = null;
		}
		size = 0;
		front = rear = 0;
	}
	
	
	
}

https://st-lab.tistory.com/161?category=856997
내가 직접 코딩해보고 사이트를 확인하여 수정하는 방법으로 구현을 진행했음

profile
인간은 망각의 동물이라지만 이건 너무한 거 아니냐고

0개의 댓글