2016-07-31 82 views
0

Ich habe geübt, wie man Warteschlangen unter Verwendung von Arrays implementiert. Ich habe einfach implementiert, wie die Elemente in der Warteschlange in die Warteschlange eingereiht und aus der Warteschlange entfernt werden. Aber ich habe eine Ausnahme aufgetreten, während umgekehrt die Warteschlange mit Stapeln ImplementierungNullzeiger Ausnahme beim Umkehren einer Warteschlange unter Verwendung des Stapels

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; 
} 

} 

Das Fehlerprotokoll ist -

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) 

Antwort

0

Es gab ein Problem in Ihrer dequeue Methode: 1. Sie wurden vorne machen = 0 und versuchten, auf Front-1 zuzugreifen, was -1 war, als Folge wurde die Ausnahme ausgelöst.

  1. Stack.get wurde entfernt, da es nicht erforderlich war.

Korrigierter Arbeitscode.

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

danke viel .it hat funktioniert :) –