Working with Queues and Stacks

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

Apart from Hashtables, queues and stacks are probably the most common Collection classes. With this article I will explain the ins and outs of queues and stacks.

Again with the Collections?

Yes. After you have seen how versatile the Collections Namespace is, you’ll never again cringe with fear when you encounter it, and you will – many times!

What is a Queue?

When you go to a bank or a grocery store you have to stand in a queue for service. The first person in the queue is the first person helped. The last person in the queue is the last person helped. Agreed? Now, the Queue class works the same way. It is what is called a FIFO ( First in, First out ) list.

As this MSDN article mentions: Queues are useful for storing messages in the order they were received for sequential processing. This class implements a queue as a circular array. Objects stored in a Queue are inserted at one end and removed from the other.

What is a Stack?

A Stack is the opposite of a Queue. Where a Queue is a FIFO list, a Stack represents a LIFO (Last in, First out ) list. This analogy is the same as a stack of books. The first book you place on your desk, will remain at the bottom, whereas the last book that you have placed on the stack, will most likely be used / discarded first.

Our design

MSDN IEnumerable

Let us add this functionality. Add the following procedure to your code.

VB.NET :

    Public Sub DisplayValues(ByVal MyQueueCollection As IEnumerable, ByVal MyStackCollection As IEnumerable)

        Dim MyQueueEnumerator As IEnumerator = MyQueueCollection.GetEnumerator() 'Get list(s) to iterate through
        Dim MyStackEnumerator As IEnumerator = MyStackCollection.GetEnumerator()

        While MyQueueEnumerator.MoveNext() 'Move to next item
            ListBox1.Items.Add(MyQueueEnumerator.Current.ToString())

        End While

        While MyStackEnumerator.MoveNext()
            ListBox2.Items.Add(MyStackEnumerator.Current.ToString()) 'Display current item
        End While


    End Sub

C# :

        public void DisplayValues(IEnumerable MyQueueCollection, IEnumerable MyStackCollection)
        {
            IEnumerator MyQueueEnumerator = MyQueueCollection.GetEnumerator(); //Get list(s) to iterate through
            IEnumerator MyStackEnumerator = MyStackCollection.GetEnumerator();

            while (MyQueueEnumerator.MoveNext()) //Move to next item
            {
                ListBox1.Items.Add(MyQueueEnumerator.Current.ToString());

            }

            while (MyStackEnumerator.MoveNext())
            {
                ListBox2.Items.Add(MyStackEnumerator.Current.ToString()); //Display current item
            }

        }

Finally, add the call to this procedure inside Button3’s Click event.

VB.NET :

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        DisplayValues(qMyQueue, sMyStack)
    End Sub

C# :

        private void Button3_Click(object sender, EventArgs e)
        {
            DisplayValues(qMyQueue, sMyStack);
        }

When the Show Values button is clicked, it would resemble Figure 2:

Values of both lists
Figure 2Values of both lists

Not so complicated now is it? Hopefully not. I am attaching working samples of these exercises for you, so play around with them and experiment – that is one of the best ways to learn.

Conclusion

Thanks for reading this article. I hope you have enjoyed it and benefited from it. Do not be scared of Collection anymore, they’re quite easy! Until next time, cheers!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read