How To Use Queue And Stacks In C#

After reading this article, you will learn about the following.

  1. Queue & Stack Introduction
  2. Using Queues
  3. Example on Queues
  4. Using Stacks
  5. Example on Stacks

Introduction to Queue & Stack

Queue and Stack are collection objects in the System.Collection namespace. The Queue class tracks objects on First-in & First-Out basis, while the Stack class tracks objects on First-in & Last-out basis. By using public methods of both Queue & Stacks classes, we can move objects to different locations.

Using Queues

There are two public methods in Queue class, described as below.

  1. Enqueue - Queue It adds an object to end of the queue
  2. Dequeue - Queue Removes and adds the object at the beginning of queue

Example on Queue

The following example shows how to use queue object to handle messages.

The messenger class, in this code, calls the sendMessage method to send messages in the sequence “One”,”Two”,”Three”. 

  1. class Messenger  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Buffer buf = new Buffer();  
  6.             buf.SendMessage(new Message("One"));  
  7.             buf.SendMessage(new Message("Two"));  
  8.             buf.ReceiveMessage();  
  9.             buf.SendMessage(new Message("Three"));  
  10.             buf.ReceiveMessage();  
  11.             buf.SendMessage(new Message("Four"));  
  12.             buf.ReceiveMessage();  
  13.             buf.ReceiveMessage();  
  14.         }  
  15.     }   

The queue object places the messages in buffer by the Enqueue method.

  1. class Buffer  
  2.     {  
  3.         private Queue messageBuffer;  
  4.         public void SendMessage(Message m)  
  5.         {  
  6.             messageBuffer.Enqueue(m);  
  7.             Console.WriteLine(m.ToString());  
  8.         }  
  9.         public Buffer()  
  10.         {  
  11.             messageBuffer = new Queue();  
  12.         }  
  13.   
  14.     }  

When it is ready to receive the messages, it writes them to the console; in this case, by calling Dequeue method.

  1. public void ReceiveMessage()  
  2.         {  
  3.             Message m = (Message)messageBuffer.Dequeue();  
  4.             Console.WriteLine(m.ToString());  
  5.         }  

Queues Full Code

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace QueueAndStacks  
  5. {  
  6.     class Message  
  7.     {  
  8.         private string messageText;  
  9.         public Message(string s)  
  10.         {  
  11.             messageText = s;  
  12.         }  
  13.         public override string ToString()  
  14.         {  
  15.             return messageText;  
  16.         }  
  17.     }  
  18.     class Buffer  
  19.     {  
  20.         private Queue messageBuffer;  
  21.         public void SendMessage(Message m)  
  22.         {  
  23.             messageBuffer.Enqueue(m);  
  24.             Console.WriteLine(m.ToString());  
  25.         }  
  26.         public void ReceiveMessage()  
  27.         {  
  28.             Message m = (Message)messageBuffer.Dequeue();  
  29.             Console.WriteLine(m.ToString());  
  30.         }  
  31.         public Buffer()  
  32.         {  
  33.             messageBuffer = new Queue();  
  34.         }  
  35.   
  36.     }  
  37.   
  38.     class Messenger  
  39.     {  
  40.         static void Main(string[] args)  
  41.         {  
  42.             Buffer buf = new Buffer();  
  43.             buf.SendMessage(new Message("One"));  
  44.             buf.SendMessage(new Message("Two"));  
  45.             buf.ReceiveMessage();  
  46.             buf.SendMessage(new Message("Three"));  
  47.             buf.ReceiveMessage();  
  48.             buf.SendMessage(new Message("Four"));  
  49.             buf.ReceiveMessage();  
  50.             buf.ReceiveMessage();  
  51.         }  
  52.     }  
  53. }  

Using Stacks

There are three types of public methods in Stack class as described below

  1. Count  - Gets the number of object contains in stack.
  2. Push - Inserts an object at the top of the stack.
  3. Pop - Removes and returns the object at the top of stack

Examples

Below code uses a Stack object instead of a Queue object, note that messages are added by using Push and remove by using Pop.

Stacks Full code

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace QueueAndStacks  
  5. {  
  6.     class Message  
  7.     {  
  8.         private string messageText;  
  9.         public Message(string s)  
  10.         {  
  11.             messageText = s;  
  12.         }  
  13.         public override string ToString()  
  14.         {  
  15.             return messageText;  
  16.         }  
  17.     }  
  18.     class Buffer  
  19.     {  
  20.         private Stack messageBuffer;  
  21.         public void SendMessage(Message m)  
  22.         {  
  23.             messageBuffer.Push(m);  
  24.             Console.WriteLine(m.ToString());  
  25.         }  
  26.         public void ReceiveMessage()  
  27.         {  
  28.             Message m = (Message)messageBuffer.Pop();  
  29.             Console.WriteLine(m.ToString());  
  30.         }  
  31.         public Buffer()  
  32.         {  
  33.             messageBuffer = new Stack();  
  34.         }  
  35.   
  36.     }  
  37.   
  38.     class Messenger  
  39.     {  
  40.         static void Main(string[] args)  
  41.         {  
  42.             Buffer buf = new Buffer();  
  43.             buf.SendMessage(new Message("One"));  
  44.             buf.SendMessage(new Message("Two"));  
  45.             buf.ReceiveMessage();  
  46.             buf.SendMessage(new Message("Three"));  
  47.             buf.ReceiveMessage();  
  48.             buf.SendMessage(new Message("Four"));  
  49.             buf.ReceiveMessage();  
  50.             buf.ReceiveMessage();  
  51.         }  
  52.     }  
  53. }   

Thanks for reading my article. Sharing is caring.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now