A common concept most software developers have to deal with on a daily basis is working with data and managing data so that it can be worked on. Typically developers will conceptually need to temporarily store data while processing it and this will typically require some sort of data structure , which is a mechanism to organise data for management and storage format to enable efficient access and modification. At its core a data structure is a collection of data values and the relationships among them and the functions and operations that be applied to the data.
Different types of data structures are suited to different applications and some are suited to to highly specific types of tasks. The most commonly found data structures most software developers will be familiar with are Queues and Stacks.
What is a Stack ?
A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, commonly referred to as the top. Typically a stack data structures follows the LIFO (Last In First Out) principle
What is the LIFO principle
It is a method for handling data structures where the first element is processed last and the last element is processed first. The insertion of an element into stack is called push operation, and deletion of an element from the stack is called pop operation.
C# Stack<T>
To make use of Stacks in C# use the generic Stack<T>
collection, which is useful useful to store temporary data in LIFO style, and you might want to delete an element after retrieving its value.
Create Stack
You can create an object of the Stack<T>
by specifying a type parameter for the type of elements it can store.
// Create an int Stack Stack<int> myStack = new Stack<int>(); // Create a string stack Stack<string> myStack = new Stack<string>(); // create a double stack Stack<double> myStack = new Stack<double>(); Stack<CustomClass> myStack = new Stack<CustomClass>();
Methods
Method | Description |
Push(T) | Inserts an item at the top of the stack |
Peek() | Returns top item from the stack |
Pop() | Removes and returns items from the top of the stack |
Contains(T) | Checks whether an item exists in the stack or not |
Clear() | Removes all items from the stack |
Properties
Property | Description |
Count | Returns the total count of elements in the stack |
class Program { static void Main(string[] args) { // Create and Add items to a stack Stack<int> stack = new Stack<int>(); for (int i = 0; i < 5; i++) stack.Push(i); Console.Write("Elements of stack: "); stack.ToList().ForEach(x => Console.Write($"{x.ToString()} ")); Console.WriteLine(Environment.NewLine); Console.WriteLine("Going to Pop element off stack"); Console.WriteLine($"removed element {stack.Pop().ToString()}"); Console.Write("Elements of Stack: "); stack.ToList().ForEach(x => Console.Write($"{x.ToString()} ")); Console.WriteLine(Environment.NewLine); Console.WriteLine("View the head of stack"); Console.WriteLine($"head of stack- {stack.Peek().ToString()}"); Console.WriteLine(Environment.NewLine); Console.WriteLine($"Size of stack- {stack.Count}"); Console.WriteLine($"Add element to stack"); stack.Push(5); Console.Write("Elements of Stack: "); stack.ToList().ForEach(x => Console.Write($"{x.ToString()} ")); Console.WriteLine(Environment.NewLine); } }
The output of the above code will appear as follows
Elements of stack: 4 3 2 1 0 Going to Pop element off stack removed element 4 Elements of Stack: 3 2 1 0 View the head of stack head of stack- 3 Size of stack- 4 Add element to stack Elements of Stack: 5 3 2 1 0
What is a Queue ?
A queue is a linear data structure in which elements can be inserted only from one side of the list called rear, and the elements can be deleted only from the other side called the front. The queue data structure follows the FIFO (First In First Out) principle.
What is FIFO principle
It is a method for handling data structures where the first element is processed first and the newest element is processed last.
C# Queue<T>
To make use of Queues in It is recommended to use the generic Queue<T>
collection.
/ Create an int Stack Queue<int> queue = new Queue<int>(); // Create a string stack Queue<string> queue = new Queue<string>(); // create a double stack Queue<double> queue = new Queue<double>(); Queue<CustomClass> queue = new Queue<CustomClass>();
Methods
Method | Description |
Enqueue(T) | Adds an item into the queue. |
Dequeue() | Returns an item from the beginning of the queue and removes it from the queue. |
Peek(T) | Returns an first item from the queue without removing it. |
Contains(T) | Checks whether an item is in the queue or not |
Clear() | Removes all the items from the queue |
Property
Property | Description |
Count | Returns the total count of elements in the Queue |
class Program { static void Main(string[] args) { Queue<int> queue = new Queue<int>(); // Adds elements {0, 1, 2, 3, 4} to queue for (int i = 0; i < 5; i++) queue.Enqueue(i); // Print out Elements of the Queue Console.Write("Elements of Queue: "); queue.ToList().ForEach(x => Console.Write($"{x.ToString()} ")); Console.WriteLine(Environment.NewLine); // To remove the head of queue. // In this the oldest element '0' will be removed Console.WriteLine("Going to Dequeue element"); Console.WriteLine($"removed element {queue.Dequeue().ToString()}"); Console.Write("Elements of Queue: "); queue.ToList().ForEach(x => Console.Write($"{x.ToString()} ")); Console.WriteLine(Environment.NewLine); Console.WriteLine("View the head of queue"); Console.WriteLine($"head of queue- {queue.Peek().ToString()}"); Console.WriteLine(Environment.NewLine); Console.WriteLine($"Size of queue- {queue.Count}"); //Add another item to queue Console.WriteLine("Add new element to queue"); queue.Enqueue(5); Console.Write("Elements of Queue: "); queue.ToList().ForEach(x => Console.Write($"{x.ToString()} ")); Console.WriteLine(Environment.NewLine); } }
The output of the above code will appear as follows
Elements of Queue: 0 1 2 3 4 Going to Dequeue element removed element 0 Elements of Queue: 1 2 3 4 View the head of queue head of queue- 1 Size of queue- 4 Add new element to queue Elements of Queue: 1 2 3 4 5
Differences between Stack and Queue data structures
Stack | Queue |
LIFO Principle | FIFO Principle |
Insert and Remove at TOP only | Insert at the Rear and Remove from the Front |
PUSH Operation to insert | Enqueue operation to insert |
POP operation to delete | Dequeue operation to delete |
Only one pointer to access the list, pointing to last element in the list | Two pointers to access the list. Pointing to the First element in the list and to the last inserted element |
used in solving problems works on recursion. | used in solving problems having sequential processing |
- What is this Directory.Packages.props file all about? - January 25, 2024
- How to add Tailwind CSS to Blazor website - November 20, 2023
- How to deploy a Blazor site to Netlify - November 17, 2023