Stacks and Queues in C#
Stacks and Queues in C#
Welcome to an exciting exploration of two fundamental data structures: Stacks and Queues! Remember, data structures store and organize data in a manner that is structured and efficient. Stacks and Queues are akin to stacking plates and standing in line, respectively. Intriguing, isn't it? Let's dive in!
Stacks: Last In, First Out (LIFO)
A Stack adheres to the "Last In, First Out" or LIFO principle. It's like a pile of plates where the last plate added is the first one to be removed. C# uses the Stack<T> class to create a stack, with Push used for insert (push), and Pop used for remove (pop, return the element that has been removed).
Let's explore this using a pile of plates.
The last plate added was removed first, demonstrating the LIFO property of a stack.
Queues: First In, First Out (FIFO)
A Queue represents the "First In, First Out" or FIFO principle, much like waiting in line at the grocery store. In C#, we can use the Queue<T> class to achieve this behavior, using Enqueue for enqueue (insert at the end) and Dequeue for dequeue (remove from the front, return the element that has been removed).
Let's examine this through a queue of people.
Here, Person 1, the first to join the queue, left before Person 2, demonstrating the FIFO property of a queue.
