After reading this article, you will learn about the following.
- Queue & Stack Introduction
- Using Queues
- Example on Queues
- Using Stacks
- 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.
- Enqueue - Queue It adds an object to end of the queue
- 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”.
- class Messenger
- {
- static void Main(string[] args)
- {
- Buffer buf = new Buffer();
- buf.SendMessage(new Message("One"));
- buf.SendMessage(new Message("Two"));
- buf.ReceiveMessage();
- buf.SendMessage(new Message("Three"));
- buf.ReceiveMessage();
- buf.SendMessage(new Message("Four"));
- buf.ReceiveMessage();
- buf.ReceiveMessage();
- }
- }
The queue object places the messages in buffer by the Enqueue method.
- class Buffer
- {
- private Queue messageBuffer;
- public void SendMessage(Message m)
- {
- messageBuffer.Enqueue(m);
- Console.WriteLine(m.ToString());
- }
- public Buffer()
- {
- messageBuffer = new Queue();
- }
-
- }
When it is ready to receive the messages, it writes them to the console; in this case, by calling Dequeue method.
- public void ReceiveMessage()
- {
- Message m = (Message)messageBuffer.Dequeue();
- Console.WriteLine(m.ToString());
- }
Queues Full Code
- using System;
- using System.Collections;
-
- namespace QueueAndStacks
- {
- class Message
- {
- private string messageText;
- public Message(string s)
- {
- messageText = s;
- }
- public override string ToString()
- {
- return messageText;
- }
- }
- class Buffer
- {
- private Queue messageBuffer;
- public void SendMessage(Message m)
- {
- messageBuffer.Enqueue(m);
- Console.WriteLine(m.ToString());
- }
- public void ReceiveMessage()
- {
- Message m = (Message)messageBuffer.Dequeue();
- Console.WriteLine(m.ToString());
- }
- public Buffer()
- {
- messageBuffer = new Queue();
- }
-
- }
-
- class Messenger
- {
- static void Main(string[] args)
- {
- Buffer buf = new Buffer();
- buf.SendMessage(new Message("One"));
- buf.SendMessage(new Message("Two"));
- buf.ReceiveMessage();
- buf.SendMessage(new Message("Three"));
- buf.ReceiveMessage();
- buf.SendMessage(new Message("Four"));
- buf.ReceiveMessage();
- buf.ReceiveMessage();
- }
- }
- }
Using Stacks
There are three types of public methods in Stack class as described below
- Count - Gets the number of object contains in stack.
- Push - Inserts an object at the top of the stack.
- 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
- using System;
- using System.Collections;
-
- namespace QueueAndStacks
- {
- class Message
- {
- private string messageText;
- public Message(string s)
- {
- messageText = s;
- }
- public override string ToString()
- {
- return messageText;
- }
- }
- class Buffer
- {
- private Stack messageBuffer;
- public void SendMessage(Message m)
- {
- messageBuffer.Push(m);
- Console.WriteLine(m.ToString());
- }
- public void ReceiveMessage()
- {
- Message m = (Message)messageBuffer.Pop();
- Console.WriteLine(m.ToString());
- }
- public Buffer()
- {
- messageBuffer = new Stack();
- }
-
- }
-
- class Messenger
- {
- static void Main(string[] args)
- {
- Buffer buf = new Buffer();
- buf.SendMessage(new Message("One"));
- buf.SendMessage(new Message("Two"));
- buf.ReceiveMessage();
- buf.SendMessage(new Message("Three"));
- buf.ReceiveMessage();
- buf.SendMessage(new Message("Four"));
- buf.ReceiveMessage();
- buf.ReceiveMessage();
- }
- }
- }
Thanks for reading my article. Sharing is caring.