what is Stack and Queue in C#?
- The Tech Platform

- Dec 9, 2021
- 2 min read

Stack
Stack class represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items.
Insertion and deletion happen in a stack from one end only, i.e., the top.
Insert operation is known as ‘push’ operation.
Delete operation is known as ‘pop’ operation.
A pointer is used to access the list, it is known as ‘top’.
The ‘top’ points to the last element of the list.
It helps work with problems associated with recursion.
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

The following is the property of Stack class −
Count− Gets the number of elements in the stack.
The following are the methods of Stack class −
Queue
Queue collection class is a concept in C# that is included in the System.Collection namespace. The elements are stored in a QUEUE in FIFO. The first element added will be the first to go out like a queue of people outside a movie hall to buy tickets.
Insertion and deletion happen from two opposite ends of the list.
Insertion happens from the rear end.
Deletion happens from the front end.
Insert operation is also known as ‘enqueue’.
Delete operation is also known as ‘dequeue’.
Two pointers are used to access the list.
Front pointer points to first element that is inserted in the list, and still present.
The rear pointer points to the last inserted element of the queue.
It is used to solve problems that have sequential processing techniques.

It has two methods −
Enqueue() method to add values
Dequeue() method to retrieve values
1. Enqueue
Add items in the queue.
Queue q = new Queue(); q.Enqueue(“Two”); q.Enqueue(“One”);2. Dequeue
Return items from the queue.
Queue q = new Queue();
q.Enqueue(“Two”);
q.Enqueue(“One”);
// remove elements
while (q.Count > 0)
Console.WriteLine(q.Dequeue());Example:
The following is an example showing how to work with Stack class and its Push() and Pop() method −
using System;
using System.Collections;
namespace CollectionsApplication
{
class Program
{
static void Main(string[] args) {
Stack st = new Stack();
st.Push('A');
st.Push('B');
st.Push('C');
st.Push('D');
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
st.Push('P');
st.Push('Q');
Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Removing values....");
st.Pop();
st.Pop();
st.Pop();
Console.WriteLine("Current stack: ");
foreach (char c in st) {
Console.Write(c + " ");
}
}
}
}Source: tutorialpoint.com
Output:
Current stack:
D C B A
The next poppable value in stack: Q
Current stack:
Q P D C B A
Removing values....
Current stack:
C B ADifference Between Stack and Queue
The Tech Platform




Comments