Queues and Stacks - potatoscript/csharp GitHub Wiki
A Queue is like a line of people waiting for something, such as at a ticket counter or a roller coaster ride. The first person to get in line is the first person to get served. This concept is called FIFO (First In, First Out).
In a Queue, the first item you add is the first item you remove.
- Order of Processing: A Queue is great when you need to process items in the same order they arrive.
- Like a Line: It's perfect for situations where things happen one after the other, such as printing tasks or requests being processed.
In C#, we can use the Queue<T>
class to create a Queue.
Hereβs how to create a simple Queue:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Creating a Queue to store strings
Queue<string> line = new Queue<string>();
// Adding items to the Queue (people joining the line)
line.Enqueue("Alice");
line.Enqueue("Bob");
line.Enqueue("Charlie");
// Displaying the contents of the Queue
Console.WriteLine("People in line:");
foreach (var person in line)
{
Console.WriteLine(person); // Output: Alice, Bob, Charlie
}
// Removing the first person from the line (the first person gets served)
string firstPerson = line.Dequeue();
Console.WriteLine($"{firstPerson} has been served!"); // Output: Alice has been served!
// Displaying the Queue after removing one person
Console.WriteLine("People left in line:");
foreach (var person in line)
{
Console.WriteLine(person); // Output: Bob, Charlie
}
}
}
-
Enqueue()
: Adds an item to the end of the Queue. -
Dequeue()
: Removes the item from the front of the Queue (the first item). -
Peek()
: Returns the item at the front without removing it. -
Count
: Returns the number of items in the Queue.
If you want to see who is next in line without removing them, you can use Peek()
.
Queue<string> line = new Queue<string>();
line.Enqueue("Alice");
line.Enqueue("Bob");
line.Enqueue("Charlie");
// Peek at the first person in line without removing them
Console.WriteLine("Next in line: " + line.Peek()); // Output: Alice
A Stack is like a stack of plates or books. The last plate or book you put on the stack is the first one you take off. This is called LIFO (Last In, First Out).
In a Stack, the last item you add is the first item you remove.
- Reverse Order Processing: A Stack is perfect when you need to process items in the reverse order they were added.
- Like a Pile: Think of a Stack as a pile of platesβyou add plates on top, and when it's time to use a plate, you take the one on top.
In C#, you can use the Stack<T>
class to create a Stack.
Hereβs how to create a simple Stack:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Creating a Stack to store strings (plates in a stack)
Stack<string> plates = new Stack<string>();
// Adding items to the Stack (plates being stacked)
plates.Push("Plate 1");
plates.Push("Plate 2");
plates.Push("Plate 3");
// Displaying the contents of the Stack
Console.WriteLine("Plates in the stack:");
foreach (var plate in plates)
{
Console.WriteLine(plate); // Output: Plate 3, Plate 2, Plate 1 (Last added is at the top)
}
// Removing the top plate from the stack (the plate on top gets used)
string topPlate = plates.Pop();
Console.WriteLine($"{topPlate} has been used!"); // Output: Plate 3 has been used!
// Displaying the Stack after removing one plate
Console.WriteLine("Plates left in the stack:");
foreach (var plate in plates)
{
Console.WriteLine(plate); // Output: Plate 2, Plate 1
}
}
}
-
Push()
: Adds an item to the top of the Stack. -
Pop()
: Removes the item from the top of the Stack. -
Peek()
: Returns the item at the top without removing it. -
Count
: Returns the number of items in the Stack.
If you want to check whatβs on top of the Stack without removing it, you can use Peek()
.
Stack<string> plates = new Stack<string>();
plates.Push("Plate 1");
plates.Push("Plate 2");
plates.Push("Plate 3");
// Peek at the top plate without removing it
Console.WriteLine("Top plate: " + plates.Peek()); // Output: Plate 3
- Queue: Follows FIFO (First In, First Out), like a line at a bank or a movie theater. The first person to arrive is the first to be served.
- Stack: Follows LIFO (Last In, First Out), like a stack of books or plates. The last item you add is the first one you take out.
- Use a Queue when you need to process things in the order they arrive (e.g., print jobs, tasks, or messages).
- Use a Stack when you need to process things in the reverse order they were added (e.g., undo operations in software, navigating browser history).
- A Queue follows FIFO (First In, First Out) order, great for processing things in the order they arrive.
- A Stack follows LIFO (Last In, First Out) order, perfect for reverse processing or dealing with last-added items.
- Both Queue and Stack are useful collections for different types of problem-solving!