2016-12-11 15 views
0

Okだから、配列を持つ循環キューを実装するのは簡単でした。ArrayListを使用して循環キューを実装する

ここでは、arraylistを使用して循環キューを実装しようとするコードがあります。問題は、私はデキューとエンキューメソッドを実装しても、これは私のarraylist(しかし、これは正常な配列で動作します)とは動作しません....

私はarraylistでこのコードを使用する場合私の質問はどのように私はaraylist(円形キューのArrayList)で動作するようにenqueと両端キュー方法を変更することができるということです

import java.util.*; 

public class People { 

    private final int DEFAULT_CAPACITY = 100; 
    private int front, rear, count; 
    private ArrayList<thePeople> people; 
    private int theMaxCapacity; 

    //----------------------------------------------------------------- 
    // Creates an empty queue using the specified capacity. 
    //----------------------------------------------------------------- 
    public People(int initialCapacity) { 
     front = rear = count = 0; 
     people = new ArrayList<thePeople>(Collections.nCopies(5, (thePeople) null)); 

    } 

    //----------------------------------------------------------------- 
    // Adds the specified element to the rear of the queue, expanding 
    // the capacity of the queue array if necessary. 
    //----------------------------------------------------------------- 
    public void enque(thePeople element) { 
     if (this.isFull()) { 
      System.out.println("Queue Full"); 

      System.exit(1); 
     } else { 
      people.set(rear, element); 
      rear = rear + 1; 
      if (rear == people.size()) { 
       rear = 0; 
      } 
      count++; 
     } 
    } 

    //----------------------------------------------------------------- 
    // Removes the element at the front of the queue and returns a 
    // reference to it. Throws an EmptyCollectionException if the 
    // queue is empty. 
    //----------------------------------------------------------------- 
    public thePeople dequeue() { 
     if (isEmpty()) { 
      System.out.println("Empty Queue"); 
     } 

     thePeople result = people.get(front); 
     people.set(front, null); 

     front = (front + 1) % people.size(); 

     count--; 

     return result; 
    } 

    //----------------------------------------------------------------- 
    // Returns true if this queue is empty and false otherwise. 
    //----------------------------------------------------------------- 
    public boolean isEmpty() { 
     return (count == 0); 
    } 

    //----------------------------------------------------------------- 
    // Returns the number of elements currently in this queue. 
    //----------------------------------------------------------------- 
    public int size() { 
     return count; 
    } 

    public boolean isFull() { 

     return count == people.size(); 
    } 

    public void mySimulation() { 
     Random rand1 = new Random(); 
     thePeople theM = null; 

     if (this.isFull()) { 
      this.people.remove(0); 
      System.out.println("Enqueueing..."); 
      this.enque(people.get(rand1.nextInt(people.size()))); 
      thePeople r1 = people.get(rear - 1); 
      System.out.println(people.toString()); 
      System.out.println(r1); 
      for (int e = 0; e < people.size(); e++) { 
       if (people.get(e) instanceof thePeople) { 
        System.out.println("G"); 
       } else { 
        System.out.println("D"); 
       } 
      } 

     } 

    } 

    //----------------------------------------------------------------- 
    // Returns a string representation of this queue. 
    //----------------------------------------------------------------- 
    @Override 
    public String toString() { 
     String result = ""; 
     int scan = 0; 

     while (scan < count) { 
      if (people.get(scan) != null) { 
       result += people.get(scan).toString() + "\n"; 
      } 
      scan++; 
     } 

     return result; 

    } 

    public static void main(String[] args) { 
     People Q1 = new People(25); 
     thePeople call1 = new thePeople("John King", "001 456 789"); 
     thePeople call2 = new thePeople("Michael Fish", "789 654 321"); 

     Q1.enque(call1); 
     Q1.enque(call2); 

     System.out.println(Q1.toString()); 
     ArrayList<thePeople> callerDetails = new ArrayList<>(Arrays.asList(call1, call2)); 
     Random rand = new Random(); 
     for (int z = 0; z <= 4; z++) { 
      Q1.enque(callerDetails.get(rand.nextInt(callerDetails.size()))); 

     } 
     System.out.println(Q1.toString()); 

    } 

} 

異なるpositionnsで誤って削除して追加しているように見えますか?

+1

円形キューが壊れて実装するためのArrayListを使用しての基本的な考え方。これが学校の授業であれば、それは愚かなことです。申し訳ありませんが、あなたはそれをする必要があります。これが本当のアプリケーションの場合は、 'ArrayDeque'を使います。 – Gene

答えて

1

私はあなたのコードにいくつかの変更を加えました。

import java.util.*; 

public class People { 

    private final int DEFAULT_CAPACITY = 100; 
    private int front, rear, count; 
    private ArrayList<thePeople> people; 
    private int theMaxCapacity; 

    //----------------------------------------------------------------- 
    // Creates an empty queue using the specified capacity. 
    //----------------------------------------------------------------- 
    public People(int initialCapacity) { 
     front = rear = count = 0; 
     people = new ArrayList<thePeople>(); 

    } 

    //----------------------------------------------------------------- 
    // Adds the specified element to the rear of the queue, expanding 
    // the capacity of the queue array if necessary. 
    //----------------------------------------------------------------- 
    public void enque(thePeople element) { 
     if (this.isFull()) { 
      System.out.println("Queue Full"); 
      System.exit(1); 
     } else { 
      people.add(element);   
     } 
    } 

    //----------------------------------------------------------------- 
    // Removes the element at the front of the queue and returns a 
    // reference to it. Throws an EmptyCollectionException if the 
    // queue is empty. 
    //----------------------------------------------------------------- 
    public thePeople dequeue() { 
     if (isEmpty()) { 
      System.out.println("Empty Queue"); 
     } 

     thePeople result = people.get(0); 
     people.remove(0);      

     return result; 
    } 

    //----------------------------------------------------------------- 
    // Returns true if this queue is empty and false otherwise. 
    //----------------------------------------------------------------- 
    public boolean isEmpty() { 
     return (people.size() == 0); 
    } 

    //----------------------------------------------------------------- 
    // Returns the number of elements currently in this queue. 
    //----------------------------------------------------------------- 
    public int size() { 
     return people.size(); 
    } 

    public boolean isFull() { 

     return people.size() == DEFAULT_CAPACITY; 
    } 

    public void mySimulation() { 
     Random rand1 = new Random(); 
     thePeople theM = null; 

     if (this.isFull()) { 
      this.people.remove(0); 
      System.out.println("Enqueueing..."); 
      this.enque(people.get(rand1.nextInt(people.size()))); 
      thePeople r1 = people.get(rear - 1); 
      System.out.println(people.toString()); 
      System.out.println(r1); 
      for (int e = 0; e < people.size(); e++) { 
       if (people.get(e) instanceof thePeople) { 
        System.out.println("G"); 
       } else { 
        System.out.println("D"); 
       } 
      } 

     } 

    } 

    //----------------------------------------------------------------- 
    // Returns a string representation of this queue. 
    //----------------------------------------------------------------- 
    @Override 
    public String toString() { 
     String result = ""; 
     int scan = 0; 

     while (scan < count) { 
      if (people.get(scan) != null) { 
       result += people.get(scan).toString() + "\n"; 
      } 
      scan++; 
     } 

     return result; 

    } 

    public static void main(String[] args) { 
     People Q1 = new People(25); 
     thePeople call1 = new thePeople("John King", "001 456 789"); 
     thePeople call2 = new thePeople("Michael Fish", "789 654 321"); 

     Q1.enque(call1); 
     Q1.enque(call2); 

     System.out.println(Q1.toString()); 
     ArrayList<thePeople> callerDetails = new ArrayList<>(Arrays.asList(call1, call2)); 
     Random rand = new Random(); 
     for (int z = 0; z <= 4; z++) { 
      Q1.enque(callerDetails.get(rand.nextInt(callerDetails.size()))); 

     } 
     System.out.println(Q1.toString()); 

    } 

} 
+1

私はこれがうまくいくと思います。 dequeの場合と同じように、ヘルパーメソッドをインスタンスコードに使用しない場合は同じです...? – blueGOLD

+1

私は答えを編集しました、デキュー時に要素を削除するのを忘れました。 – Shay

+1

この回答は大丈夫ですが、rear =(rear + 1)%queue.lengthを追加するなど、「arrayListを使用して循環キューを実装する」ため、ラップアラウンドは処理されません。とfront =(front + 1)%queue.length; .....? – blueGOLD

関連する問題