2011-01-26 24 views
1

私の質問がこのウェブサイトに関連していない場合は申し訳ありません。キューの最後の要素を削除してください

キューの最後の要素を削除する必要があります。最初の要素を削除せずに取得する、Enqueue(要素) - キューの後ろに要素を挿入する、Dequeue() - 最初の要素を削除する、IsEmpty() - trueまたはキューが空であるかどうかはfalseです。 私を助けるために配列やキューを使用することはできません。要素の数は利用できません。

今、私はいくつかの解決策を考えましたが、現在の要素が最後の要素であるかどうかをどのように判断するのか分からないので、私は立ち往生するたびに解決します。

ありがとうございます。また、このタイプの質問のための適切な場所でない場合は、ごめんなさい。

+9

音が宿題のようです。あなたは[StackOverflow](http://stackoverflow.com)に投稿したいかもしれませんが、それに「宿題」というタグをつけて、あなたの試行の詳細や既に行った作業のサンプルを含めてください。 –

+1

item = q.Dequeue()to q.IsEmpty()? –

+1

これは間違いなくStack Overflowに関する質問ですが、Breは既に試したことのあるものをリストする方が良いでしょう。 –

答えて

4

私は現在の要素が最後の要素であるかどうかを知る方法がわからないので、私は立ち往生するたびに立ち往生します。

良い。あなたは "現在の"要素が最後であるかどうか分からないので、の "前の"要素を知っている最後のものです。

したがって、前の要素を保存することでこれを処理できるはずです。このような

2

何かが動作する:

パブリックキューremoveLast(キューQUEUE1){

Queue queue2 = new Queue(); 

while(!queue1.isEmpty()){ 
    Object o = queue1.dequeue(); 
    if(!queue1.isEmpty()){ 
    queue2.enqueue(o); 
    } 
} 
return queue2; 

}

+3

OPはその場所でそれを行わなければなりません。 "私を助けるために配列や待ち行列を使うことはできません" – Tester101

6

Justin Beal's solutionがより単純です。しかし、別のキューを作成せずに、それを実行できると思います。

object RemoveLast(Queue q) { 
    object first = q.Peek(); 
    object current = null; 
    while (true) { 
     current = q.Dequeue(); 
     if (q.Peek() == first) { 
      break; 
     } 
     q.Enqueue(current); 
    } 
    return current; 
} 
0

これまでの回答の中で最も有望なのはトリニダードのソリューションでした。キュー内の最初の要素/オブジェクトが一意である場合にのみ、このソリューションが完璧に動作すると思います。最初の要素/オブジェクトの重複が検出された場合は、最後の要素として正しい要素を与えることができません。

1

私は同じ要件を持っていましたので、Googleがここに到達した場合に他の人が使用する拡張メソッドを作成しました。

using System.Collections.Generic; 
using System.Linq; 

/// <summary> 
/// Extensions for <see cref="System.Collections.Generic.Queue{T}"/>. 
/// </summary> 
public static class QueueExtensionsStatic 
{ 
    /// <summary> 
    /// Removes the last item from the <see cref="System.Collections.Generic.Queue{T}"/>. 
    /// </summary> 
    /// <typeparam name="T">Type of object in <see cref="System.Collections.Generic.Queue{T}"/>.</typeparam> 
    /// <param name="q">Instance of <see cref="System.Collections.Generic.Queue{T}"/> to remove item from.</param> 
    /// <returns>Item removed from the <see cref="System.Collections.Generic.Queue{T}"/>.</returns> 
    public static T Pop<T>(this Queue<T> q) 
    { 
     for (var i = 1; i < q.Count; i++) 
      q.Enqueue(q.Dequeue()); 

     return q.Dequeue(); 
    } 

    /// <summary> 
    /// Removes the last <see cref="quantity"/> item(s) from the <see cref="System.Collections.Generic.Queue{T}"/>. 
    /// </summary> 
    /// <typeparam name="T">Type of object in <see cref="System.Collections.Generic.Queue{T}"/>.</typeparam> 
    /// <param name="q">Instance of <see cref="System.Collections.Generic.Queue{T}"/> to remove item from.</param> 
    /// <param name="quantity">Number of items to pop off the end of the <see cref="System.Collections.Generic.Queue{T}"/>.</param> 
    /// <returns>Item removed from the <see cref="System.Collections.Generic.Queue{T}"/>.</returns> 
    public static IEnumerable<T> Pop<T>(this Queue<T> q, int quantity) 
    { 
     for (var i = quantity; i < q.Count; i++) 
      q.Enqueue(q.Dequeue()); 

     var poppedItems = new List<T>(quantity); 
     for (int i = 0; i < quantity; i++) 
      poppedItems.Add(q.Dequeue()); 

     return poppedItems; 
    } 

    /// <summary> 
    /// Adds an item(<see cref="T"/>) to the start of the <see cref="System.Collections.Generic.Queue{T}"/>. 
    /// </summary> 
    /// <typeparam name="T">Type of object in <see cref="System.Collections.Generic.Queue{T}"/>.</typeparam> 
    /// <param name="q">Instance of <see cref="System.Collections.Generic.Queue{T}"/> to remove item from.</param> 
    public static void Push<T>(this Queue<T> q, T item) 
    { 
     q.Enqueue(item); 
     for (var i = 1; i < q.Count; i++) 
      q.Enqueue(q.Dequeue()); 
    } 

    /// <summary> 
    /// Adds items(<see cref="T"/>) to the start of the <see cref="System.Collections.Generic.Queue{T}"/>. 
    /// </summary> 
    /// <typeparam name="T">Type of object in <see cref="System.Collections.Generic.Queue{T}"/>.</typeparam> 
    /// <param name="q">Instance of <see cref="System.Collections.Generic.Queue{T}"/> to remove item from.</param> 
    /// <param name="items">List of items(<see cref="T"/>) to add to the <see cref="System.Collections.Generic.Queue{T}"/>.</param> 
    public static void Push<T>(this Queue<T> q, IEnumerable<T> items) 
    { 
     if (items == null || !items.Any()) return; 

     foreach (var item in items) 
      q.Enqueue(item); 

     for (var i = items.Count(); i < q.Count; i++) 
      q.Enqueue(q.Dequeue()); 
    } 
} 
関連する問題