2016-07-31 2 views
0

배열을 사용하여 큐를 구현하는 방법을 연습하고있었습니다. 대기열에있는 요소를 큐에 넣고 큐에서 빼는 방법을 쉽게 구현했습니다. 큐 사용하여 스택null 포인터 스택을 사용하여 큐를 뒤집을 때 예외가 발생했습니다.

의 역을 구현하면서하지만 예외가 발생했을
public class QueueImpl { 
private int capacity; 
int queueArr[]; 
int front = 0; 
int rear = -1; 
int currentSize = 0; 
QueueImpl(int queueSize){ 
    this.capacity=queueSize; 
    queueArr=new int[this.capacity]; 
} 

public void enqueue(int data){ 
    if(isQueueFull()){ 
     System.out.println("Overflow"); 
     return; 
    } 
    else{ 
     rear=rear+1; 
     if(rear==capacity-1) 
     { 
      rear=0; 
     } 
     queueArr[rear]=data; 
     currentSize++; 
     System.out.println("Element " + data+ " is pushed to Queue !"); 
    } 

} 


public int dequeue(){ 
    if(isQueueEmpty()){ 
     System.out.println("UnderFlow"); 
    } 
    else{ 
     front=front+1; 
     if(front == capacity-1){ 
      System.out.println("Pop operation done ! removed: "+queueArr[front-1]); 
      front = 0; 
     } else { 
      System.out.println("Pop operation done ! removed: "+queueArr[front-1]); 
     } 
     currentSize--; 
    } 
    return queueArr[front-1]; 

} 
private boolean isQueueEmpty() { 
    boolean status=false; 
    if(currentSize==0){ 
     status=true; 
    } 
    return status; 
} 

private boolean isQueueFull() { 
    boolean status=false; 
    if(currentSize==capacity){ 
     status=true; 
    } 
    return status; 
} 

public static void main(String arg[]) { 
    QueueImpl queueImpl=new QueueImpl(5); 
    queueImpl.enqueue(5); 
    queueImpl.enqueue(2); 
    queueImpl.enqueue(9); 
    queueImpl.enqueue(1); 
// queueImpl.dequeue(); 
    queueImpl.printQueue(queueImpl); 
    queueImpl.reverse(queueImpl); 
} 

private void printQueue(QueueImpl queueImpl) { 
System.out.println(queueImpl.toString());  
} 
@Override 
    public String toString() { 
     return "Queue [front=" + front + ", rear=" + rear + ", size=" + currentSize 
       + ", queue=" + Arrays.toString(queueArr) + "]"; 
    } 
private QueueImpl reverse(QueueImpl queueImpl) throws ArrayIndexOutOfBoundsException { 
    int i=0; 
    Stack<Integer> stack=new Stack<Integer>(); 
    while(!queueImpl.isQueueEmpty()){ 
     stack.push(queueImpl.dequeue()); 
    } 
    while(!stack.isEmpty()){ 
     stack.get(i); 
     i++; 
    } 
    while(!stack.isEmpty()){ 
     queueImpl.enqueue(stack.pop()); 
    } 
    return queueImpl; 
} 

} 

오류 로그입니다 -

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
at com.tcs.QueueUsingAraay.QueueImpl.dequeue(QueueImpl.java:51) 
at com.tcs.QueueUsingAraay.QueueImpl.reverse(QueueImpl.java:93) 
at com.tcs.QueueUsingAraay.QueueImpl.main(QueueImpl.java:78) 

답변

0

당신의 디큐 방법에 문제가 발생했습니다 : 1. 앞 = 0을했다가 결과는 예외가 throw 된 -1 인 front-1에 액세스하려고 시도했습니다.

  1. 필요하지 않았으므로 stack.get을 제거했습니다.

수정 된 작업 코드.

public class QueueImpl { 
     private int capacity; 
     int queueArr[]; 
     int front = 0; 
     int rear = -1; 
     int currentSize = 0; 

     QueueImpl(int queueSize) { 
      this.capacity = queueSize; 
      queueArr = new int[this.capacity]; 
     } 

     public void enqueue(int data) { 
      if (isQueueFull()) { 
       System.out.println("Overflow"); 
       return; 
      } else { 
       rear = rear + 1; 
       if (rear == capacity - 1) { 
        rear = 0; 
       } 
       queueArr[rear] = data; 
       currentSize++; 
       System.out.println("Element " + data + " is pushed to Queue !"); 
      } 

     } 

     public int dequeue() { 
      int element=-1; 
      if (isQueueEmpty()) { 
       System.out.println("UnderFlow"); 
      } else { 
       element = queueArr[front]; 
       front=front+1; 
       if (front == capacity - 1) { 
        System.out.println("Pop operation done ! removed: " 
          + queueArr[front - 1]); 
        front = 0; 
       } else { 
        System.out.println("Pop operation done ! removed: " 
          + queueArr[front - 1]); 
       } 
       currentSize--; 
      } 
      return element; 

     } 

     private boolean isQueueEmpty() { 
      boolean status = false; 
      if (currentSize == 0) { 
       status = true; 
      } 
      return status; 
     } 

     private boolean isQueueFull() { 
      boolean status = false; 
      if (currentSize == capacity) { 
       status = true; 
      } 
      return status; 
     } 

     public static void main(String arg[]) { 
      QueueImpl queueImpl = new QueueImpl(5); 
      queueImpl.enqueue(5); 
      queueImpl.enqueue(2); 
      queueImpl.enqueue(9); 
      queueImpl.enqueue(1); 

      queueImpl.printQueue(queueImpl); 
      queueImpl.reverse(queueImpl); 
      queueImpl.printQueue(queueImpl); 
     } 

     private void printQueue(QueueImpl queueImpl) { 
      System.out.println(queueImpl.toString()); 
     } 

     @Override 
     public String toString() { 
      return "Queue [front=" + front + ", rear=" + rear + ", size=" 
        + currentSize + ", queue=" + Arrays.toString(queueArr) + "]"; 
     } 

     private QueueImpl reverse(QueueImpl queueImpl) 
       throws ArrayIndexOutOfBoundsException { 
      Stack<Integer> stack = new Stack<Integer>(); 
      while (!queueImpl.isQueueEmpty()) { 
       stack.push(queueImpl.dequeue()); 
      } 
      while (!stack.isEmpty()) { 
       queueImpl.enqueue(stack.pop()); 
      } 
      return queueImpl; 
     } 

    } 
+0

많이 고맙습니다. –