2011-04-10 4 views
1

これまでのところ、このすべては(少なくとも私が笑そう願って)ないだから私は(まず第一に出て)通常のキューの思考を遵守、私の質問が何であるかキュークラス/のisEmptyメソッド

class Queue 
{ 

    private int front = 0; //Creates front and back int holders and an array 
    private int back = -1; 
    private int[] anArray; 

    public Queue(int size) // constructor to get the Queue size 
    { 
     anArray = new int[size]; 
    } 

    public bool IsFull 
    { 
     get // gets to see if the Queue is full (I assume I did this right, It's full if the back of the queue is equal to -1 of an array) 
     { 
      return back == anArray.Length - 1; 
     } 
    } 

    public bool IsEmpty 
    { 
     get // It's empty if the back is -1; I think this is where I messed up, I think that when it checks to see if it's empty it also doesn't check if it's empty when I have dequeued the other numbers (or write them off). Would I make something like "Return front == anArray.Length -1;" ? That would check to see when the front (The part being written to console first) hits the end of the array? 
     { 
      return back == -1; 
     } 
    } 

    public void Enqueue(int valueEnqueue) 
    { // Method to Enqueue the variables into the array 

     if (IsFull) 
     { 
      //do nothing 
     } 
     else 
     { 
      back = back + 1; 
      anArray[back] = valueEnqueue; 

     } 
    } 

    public int Dequeue() 
    { // Method to dequeue the variables put in 
     if (IsEmpty) 
     { 
      //do nothing 
      return 1010101010; 
     } 
     else 
     { 
      int dequeue = anArray[front]; 
      front = front + 1; 
      return dequeue; 
     } 
    } 

を推測する方法私はそれを停止するのですか?私は、範囲外のエラーのインデックスを取得し続けます。

+0

140列の行はあまり読み込めません。それを分解してください。 –

+0

これはどのように読めないのですか?私は行などをスキップしました。 – Nogg

+0

これはクローズではありません、これはキューよりもスタックのようです。必要なヘルプを得るには、[宿題]タグを使用する必要があります。 –

答えて

0

front変数に制限のないデキュー機能でIndexOutOfRange例外が発生していると思われますが、呼び出しごとにインクリメントし続け、最終的には配列の長さを超えます。

通常、キュー構造は循環バッファとして実装されます。実装の詳細については、ここをクリックしてください。

http://en.wikipedia.org/wiki/Circular_buffer

+0

私は擬似コードの中にいるのです。私は巨大なサークル。私はちょうどこれを操作する方法について混乱しているのですか? – Nogg

+0

@ノッグでは、コードは循環バッファを実装していません。なぜなら、フロント/バックは配列の先頭に到達するときに配列の先頭に折り返さなければならないからです。私がリンクしているウィキペディアの記事を見てください。 –

+0

IsFullメソッドで何かを変更して、カウンタがいっぱいになったときにそのカウンタを終了する必要がありますか? – Nogg

1

あなたは車輪の再発明しようとしていますか?

なぜ使用しないか:system.collections.queue

http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx

そして、あなたはただそうしたい場合は、system.collections.queueにリフレクターを試してみて、内側のですかを参照してください。

+1

私はそれが宿題であると推測しています。 –

+0

@タイラー:+1。そうそうだ。 –

+0

@あなたのもと、私の元の投稿のコメントをチェックしてください。 – Nogg

0

あなたは有限の配列を持ち、無限の容量が必要です。配列はこのための最良のコンテナではありません。Listコンテナのように、キューを実装するために別のものを使用する必要があります。

Enqueue(item) { list.Add(item); } 
Dequeue() 
{ 
    if(!IsEmpty) 
    { 
    var item = list[0]; 
    list.Remove(item); 
    return item; 
    } 
    return null; 
} 
IsFull{ get { return false; } } 
IsEmpty{ get { return list.Count == 0; } 
関連する問題