0
Java에서 양단 큐 또는 양단 큐를 작성해야합니다. 그러나 deque 구현에서 정의 된 메소드 중 일부를 사용할 때, 데모 또는 드라이버 프로그램에서 주어진 메소드가 deque 구현 클래스 인 유형에 대해 정의되지 않았다는 오류가 발생합니다. 따라서 toString() 및 size() 메서드는 오류를 표시하지 않지만 대기열 및 대기열에서 제거하는 것과 같은 모든 오류는 발생합니다. 이것은 바보 같은 실수가되어야한다는 것을 알고 있지만 이해를 도와주세요. 감사.Java의 deque 구현에서 내 유형에 대한 메소드가 정의되지 않았습니다.
이 양단 큐의import java.util.ArrayDeque;
public class DequeDemo {
public static void main(String[] args) {
//create an object for the Deque class
ArrayDeque<Integer> cad = new ArrayDeque<Integer>();
/*Adding integers to the deque at rear*/
cad.enqueueRear(40);
cad.dequeueRear(22);
cad.enqueueFront(-4);
cad.dequeueFront(16);
System.out.println(cad.first());
System.out.println(cad.last());
}
}
구현 :
public class ArrayDeque<T> implements DequeADT<T> {
private final int DEFAULT_CAPACITY = 10;
private int front, rear, count;
private T[] deque;
//creates an empty deque using default capacity
public ArrayDeque() {
front = rear = count = 0;
deque = (T[]) (new Object[DEFAULT_CAPACITY]);
}
//creates an empty deque using specified capacity
public ArrayDeque (int initialCapacity) {
front = rear = count = 0;
deque = (T[])(new Object[initialCapacity]);
}
//adds an element to the rear of the deque
public void enqueueRear(T element) {
if(size() == deque.length) {
System.out.println("The deque is full.");
return;
}
deque[rear] = element;
rear = (rear + 1)%deque.length;
count++;
}
//adds an element to the front of the deque
public void enqueueFront(T element) {
if(size() == deque.length) {
System.out.println("The deque is full.");
return;
}
if (front == 0)
front = deque.length - 1;
else
front = (front -1)% deque.length;
deque[front] = element;
count++;
}
/*removes an element at the front of the deque and returns
* a reference to it*/
public T dequeueFront() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("deque");
T result = deque[front];
deque[front] = null;
front = (front + 1)%deque.length;
count--;
return result;
}
/*removes an element at the rear of the deque and returns
* a reference to it*/
public T dequeueRear() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("deque");
T result = deque[rear -1];
deque[rear -1] = null;
rear = (rear -1)% deque.length;
count--;
return result;
}
//returns an element at the front of the deque
public T first() {
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = deque[front];
return result;
}
//returns the element the deque rear
public T last() {
if (isEmpty())
throw new EmptyCollectionException("queue");
T result = deque[rear -1];
return result;
}
//returns true if deque is empty
public boolean isEmpty() {
return (count==0);
}
//returns the number of elements in the deque
public int size() {
return count;
}
//returns a string representation of the deque
public String toString() {
String queueElements = "";
for(int i=front, j=0;j<count;i=(i+1)%deque.length, j++) {
queueElements = queueElements + deque[i].toString() + " ";
}
return queueElements;
}
}
제거'수입 java.util.ArrayDeque 대신 다음을 수행해야합니다
을 다음을 수행하십시오 'ArrayDeque' 클래스를 테스트 할 때, @ElliottFrisch가 언급 한 것의 위에'java.util' –
을 호출합니다. 주된 메소드 내에서,'dequeueRear'를 호출 할 때 인자를 넘겨 주려고합니다. 그것 메소드의 서명은 ->'dequeueRear()'입니다. –
@ElliottFrisch 그 덕분에! 너무 많이 고마워요 :) –