Stack and Queue enumeration order

后端 未结 3 1413
南旧
南旧 2020-12-20 12:03

I know that List enumerator guarantees the enumeration order and respects last sort operation, I know that the Dictionary and HashSet

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 12:40

    A Queue is a First-In-First-Out (FIFO) collection (says so right in the documentation). That means the enumerator gives you the items in the order that they were added.

    A Stack is a Last-In-First-Out (LIFO) collection. That means the enumerator gives you the items in reverse order of how they were added.

    Stacks and Queues are very standard Computer Science constructs, so they really can't be re-purposed without serious backlash. When you look at the examples for the GetEnumerator() functions, it clearly documents the order of enumeration:

    Stack Enumeration:

        Stack numbers = new Stack();
        numbers.Push("one");
        numbers.Push("two");
        numbers.Push("three");
        numbers.Push("four");
        numbers.Push("five");
    
        // A stack can be enumerated without disturbing its contents.
        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }
    
        /* This code example produces the following output:
    
         five
         four
         three
         two
         one
    
        */
    

    Queue Enumeration:

        Queue numbers = new Queue();
        numbers.Enqueue("one");
        numbers.Enqueue("two");
        numbers.Enqueue("three");
        numbers.Enqueue("four");
        numbers.Enqueue("five");
    
        // A queue can be enumerated without disturbing its contents.
        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }
    
        /* This code example produces the following output:
    
         one
         two
         three
         four
         five
    
        */
    

    Again, with basic computer science definitions, An enumerator or iterator must present the elements in the natural order for a collection. Specific collection types have a defined order.

    Caveat

    Please note that while the enumeration process does reflect the natural order of the FIFO and LIFO collections (ref), this is not how Queues (ref) and Stacks (ref) are intended to be used. They are intended to be used with the Enqueue()/Dequeue()and Push()/Pop()/Peek() interactions. Microsoft includes the enumerator to keep everything consistent with the base ICollection interface, and keeps the enumerator in the collection's natural order.

    The purpose of a Queue is to provide a pipeline of work that can be processed in order. The purpose of a Stack is to provide a way of returning to a previous context when the local work is done. They are intended to work on one item at a time. Iterating over the collection with an enumerator kind of side steps that whole purpose and does not remove items from the queue/stack. It's essentially a peek at all the items that are there.

提交回复
热议问题