0% found this document useful (0 votes)
6 views

Stack and Queue

The document provides a C# program demonstrating the usage of a Stack and a Queue data structure. It includes steps for pushing and popping elements in the Stack, as well as enqueuing and dequeuing elements in the Queue, along with methods to check the top/front elements and the count of elements. Additionally, it shows how to clear both data structures.

Uploaded by

awasharingang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Stack and Queue

The document provides a C# program demonstrating the usage of a Stack and a Queue data structure. It includes steps for pushing and popping elements in the Stack, as well as enqueuing and dequeuing elements in the Queue, along with methods to check the top/front elements and the count of elements. Additionally, it shows how to clear both data structures.

Uploaded by

awasharingang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;

class Program

static void Main()

// Step 1: Creating a Stack of integers

Stack<int> stack = new Stack<int>();

// Step 2: Pushing elements onto the stack

stack.Push(10); // Stack: 10

stack.Push(20); // Stack: 20, 10

stack.Push(30); // Stack: 30, 20, 10

// Step 3: Peek to see the top element (without removing it)

Console.WriteLine("Top element (using Peek): " + stack.Peek()); // Output: 30

// Step 4: Popping elements off the stack (removes top element)

Console.WriteLine("Popped element: " + stack.Pop()); // Output: 30 (Stack: 20, 10)

Console.WriteLine("Popped element: " + stack.Pop()); // Output: 20 (Stack: 10)

// Step 5: Checking the top element after pops

Console.WriteLine("Top element after pops (using Peek): " + stack.Peek()); // Output: 10

// Step 6: Checking the number of elements in the stack

Console.WriteLine("Number of elements in stack: " + stack.Count); // Output: 1

// Step 7: Checking if a specific element is in the stack


Console.WriteLine("Contains 10: " + stack.Contains(10)); // Output: True

Console.WriteLine("Contains 30: " + stack.Contains(30)); // Output: False

// Step 8: Clearing the stack

stack.Clear();

Console.WriteLine("Number of elements in stack after clearing: " + stack.Count); // Output: 0

using System;

using System.Collections.Generic;

class Program

static void Main()

// Step 1: Create a queue of integers

Queue<int> queue = new Queue<int>();

// Step 2: Enqueue elements into the queue

queue.Enqueue(10); // Queue: 10

queue.Enqueue(20); // Queue: 10, 20

queue.Enqueue(30); // Queue: 10, 20, 30

// Step 3: Peek at the front element (without removing)

Console.WriteLine("Front element (Peek): " + queue.Peek()); // Output: 10


// Step 4: Dequeue elements (removes front element)

Console.WriteLine("Dequeued: " + queue.Dequeue()); // Output: 10 (Queue: 20, 30)

Console.WriteLine("Dequeued: " + queue.Dequeue()); // Output: 20 (Queue: 30)

// Step 5: Check the front element after dequeues

Console.WriteLine("Front element after dequeues (Peek): " + queue.Peek()); // Output: 30

// Step 6: Check number of elements in the queue

Console.WriteLine("Number of elements in queue: " + queue.Count); // Output: 1

// Step 7: Clear the queue

queue.Clear();

Console.WriteLine("Number of elements after clearing: " + queue.Count); // Output: 0

You might also like