C#: Swap the top two elements of a stack
C# Sharp Stack: Exercise-15 with Solution
Write a C# program to swap the top two elements of a given stack.
Sample Solution:
C# Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Stack
{
private int[] items; // Array to hold stack elements
private int top; // Index indicating the top of the stack
// Constructor to initialize the stack with a specified size
public Stack(int size)
{
items = new int[size]; // Creating an array of integers with the given size
top = -1; // Initializing top index to -1 indicating an empty stack
}
// Check if the stack is empty
public bool IsEmpty()
{
return top == -1; // Stack is empty if top is -1
}
// Check if the stack is full
public bool IsFull()
{
return top == items.Length - 1; // Stack is full if top equals the last index of the array
}
// Push an element onto the stack
public void Push(int item)
{
if (IsFull()) // Check if the stack is full
{
Console.WriteLine("Stack Full!"); // Display a message indicating stack overflow
return;
}
items[++top] = item; // Increment top and add the item to the stack
}
// Pop an element from the stack
public int Pop()
{
if (IsEmpty()) // Check if the stack is empty
{
Console.WriteLine("Stack underflow"); // Display a message indicating stack underflow
return -1;
}
return items[top--]; // Return and decrement top to remove the element from the stack
}
// Peek at the top element of the stack without removing it
public int Peek()
{
if (IsEmpty()) // Check if the stack is empty
{
Console.WriteLine("Stack is empty"); // Display a message indicating the stack is empty
return -1;
}
return items[top]; // Return the top element of the stack
}
// Static method to get the size of a stack
public static int Size(Stack stack)
{
return stack.top + 1; // Return the size of the stack based on the current top index
}
// Static method to count all the elements in a stack
public static int Count(Stack stack)
{
int count = 0; // Initialize count variable to track the number of elements
Stack temp = new Stack(Size(stack)); // Create a temporary stack to preserve the original stack order
// Move elements from the original stack to the temporary stack and count them
while (!stack.IsEmpty())
{
temp.Push(stack.Pop()); // Move elements from original stack to temporary stack
count++; // Increment count for each element moved
}
// Restore the original stack by moving elements back from the temporary stack
while (!temp.IsEmpty())
{
stack.Push(temp.Pop()); // Move elements back to the original stack
}
return count; // Return the total count of elements in the stack
}
// Static method to clear the stack by popping all elements
public static void Clear(Stack stack)
{
while (!stack.IsEmpty())
{
stack.Pop(); // Remove elements from the stack until it's empty
}
}
// Method to swap the top two elements of a stack
public static void SwapTopTwo(Stack stack)
{
if (Count(stack) < 2) // Check if there are at least two elements in the stack
{
return; // If there are not enough elements, exit without performing the swap
}
int top = stack.Pop(); // Pop the top element of the stack
int secondTop = stack.Pop(); // Pop the second top element of the stack
stack.Push(top); // Push the previously top element to the stack in the second top position
stack.Push(secondTop); // Push the previously second top element to the stack in the top position
}
// Method to display the elements of the stack
public static void Display(Stack stack)
{
if (stack.IsEmpty()) // Check if the stack is empty
{
Console.WriteLine("Stack is empty"); // Display a message indicating the stack is empty
return;
}
Console.WriteLine("Stack elements:"); // Display a message indicating stack elements will be shown
for (int i = stack.top; i >= 0; i--) // Loop through the stack elements
{
Console.Write(stack.items[i] + " "); // Print each element of the stack
}
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Initialize a stack:");
Stack stack = new Stack(10); // Create a stack with a capacity of 10 elements
Console.WriteLine("Input some elements onto the stack:");
stack.Push(1); // Add elements to the stack
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
stack.Push(6);
Stack.Display(stack); // Display the initial stack elements
Console.WriteLine("\n\nSwap the top two elements of a stack:");
Stack.SwapTopTwo(stack); // Swap the top two elements of the stack
Stack.Display(stack); // Display the stack after the swap
}
}
Sample Output:
Initialize a stack: Input some elements onto the stack: Stack elements: 6 5 4 3 2 1 Swap the top two elements of a stack: Stack elements: 5 6 4 3 2 1
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Rotate the stack elements to the left.
Next: Nth element from the top of the stack.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/stack/csharp-stack-exercise-15.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics