Queues and stacks

Queues and stacks are collection items that allow us to save data temporarily during the execution of the program. These collections are very similar to the other collections, such as lists, with the major difference being how elements are added and removed from the collection.

A queue is a first-in, first-out type of collection. It basically implies that elements are accessed from the queue in the same order they are added to the collection. When the items are accessed, they can also be removed in the same operation. A queue has three main operations:

  • Adding a new element to the queue: It's executed by the Enqueue method.
  • Removing an existing element from the queue: It's executed by the Dequeue method.
  • Peeking or retrieving the value of an element in the queue: It's executed by the Peek method.

This diagram shows how the queue works:

In the preceding diagram, the block indicates a queue collection. In the collection, we have added five elements: element A, element B, element C, element D, and element E. Element E was the first element added to the queue and it's at the front of the queue. Element A was the last element added to the queue and it's at the back of the queue. 

The green arrows indicate the indexes in the queue at which the different operations will take place. The addition of a new element will always take place at the back of the queue. Removal of an element will take place at the front of the queue.

Let's look at a code implementation to show how it's done programmatically. In the following code, we have created a queue collection object. We will then add elements to the object and execute different operations on it:

public static void QueueOperations()
{
Queue<string> que = new Queue<string>();
que.Enqueue("E");
que.Enqueue("D");
que.Enqueue("C");
que.Enqueue("B");
que.Enqueue("A");
int index = 0;
foreach(string s in que)
{
Console.WriteLine("Queue Element at index " + index + " is " + s);
index++;
}
Console.WriteLine("Queue Element at top of the queue is "
+ que.Peek());
que.Dequeue();
index = 0;
foreach (string s in que)
{
Console.WriteLine("Queue Element at index " + index + " is " + s);
index++;
}
}

When the preceding code is executed, we get the following output:

As illustrated by the queue diagram, when we add elements to the queue, they are always added to the end. Therefore, when we add the E, D, C, B, and A elements in the same order, A will always be at the back of the queue and E will be at the front of the queue. The indexes in the preceding output indicate the position in the queue where each respective element is present. 

A peek operation in a queue is always done on the front index, that is, the 0 index of the queue. A removal operation in a queue is always done on the front index, that is, the 0 index of the queue. Therefore in the preceding output, both of these operations are executed on the element E

Once these operations are done and we reiterate through the queue, we find that, owing to the remove operation, element E is no longer present in the queue. Now that we have an understanding of the queue, let's look at how a stack works in .NET Framework.

Just like a queue, the stack also provides temporary storage with the only difference being in terms of how the operations are executed on a stack. Stack follows the LIFO model, which implies that the last element added to the stack will be the first one to be removed. The following are the three main operations that we can do on a stack:

  • Add a new element to the stack. It's executed by the Push method.
  • Remove an existing element from the stack. It's executed by the Pop method.
  • Peek or retrieve the value of an element in the stack. It's executed by the Peek method.

The following diagram shows how the stack works:

In the diagram, the block indicates a stack collection. In the collection, we have added five elements: element A, element B, element C, element D, and element E. Element E was the first element added to the stack and it's at the front of the stack. Element A was the last element added to the stack and it's at the back of the stack. 

The green arrows indicate the indexes in the stack at which the different operations will take place. The addition of a new element will always take place at the bottom of the stack. The removal of an element will take place at the top of the stack. 

Let's look at a code implementation to show how it's done programmatically. In the following code, we have created a stack collection object. We will then add elements in the object and execute different operations on it:

public static void StackOperations()
{
Stack<string> sta = new Stack<string>();
sta.Push("E");
sta.Push("D");
sta.Push("C");
sta.Push("B");
sta.Push("A");
int index = 0;
foreach (string s in sta)
{
Console.WriteLine("Stack Element at index " + index + " is " + s);
index++;
}
Console.WriteLine("Stack Element at top of the stack is "
+ sta.Peek());
sta.Pop();
index = 0;
foreach (string s in sta)
{
Console.WriteLine("Stack Element at index " + index + " is " + s);
index++;
}
}

When the preceding code is executed, we get the following output:

A Peek operation on a stack is always done on the last element added on the stack. Element A was the last element to be added to the stack. Therefore, when a Peek operation is done on the stack, it gives an output of A. Similarly, when a Pop operation is executed on the stack, it removes the element A.

With this, we have gone through the different collections that are available in .NET. Each of these collection items has some properties that make them usable in some scenarios and not in others. These criteria need to be evaluated when we are trying to select a collection item to use.

In the next section, we will look at some of the criteria that help us to select the right collection item for each scenario.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.147.193