﻿ C# - Rotate the stack elements to the left

# C#: Rotate the stack elements to the left

## C# Sharp Stack: Exercise-14 with Solution

Write a C# program to rotate the stack elements to the left direction.

Sample Solution:

C# Code:

``````using System;
using System.Collections;
using System.Collections.Generic;

public class Stack
{
private int[] items; // Array to store stack elements
private int top; // Index representing 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

// Create a temporary stack to preserve the original stack order
Stack temp = new Stack(Size(stack));

// 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
}
}

// Static method to rotate the stack elements to the left direction
public static void RotateLeft(Stack stack, int rotations)
{
int count = Count(stack); // Get the count of elements in the stack
rotations %= count; // Calculate effective rotations considering the stack size

var tempArray = new int[count]; // Create a temporary array to hold stack elements

// Transfer stack elements to the temporary array
for (int i = 0; i < count; i++)
{
tempArray[i] = (int)stack.Pop(); // Pop elements from the stack and store in the array
}

// Reconstruct the stack by rotating elements to the left based on the specified rotations
for (int i = count - 1; i >= 0; i--)
{
int newIndex = (i + rotations) % count; // Calculate the new index after rotation
stack.Push(tempArray[newIndex]); // Push elements back to the stack with new positions
}
}

// Static 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
int rotations = 1; // Define the number of rotations
Console.WriteLine("\n\nRotate the stack elements to the left, Rotations = {0}", rotations);
Stack.RotateLeft(stack, rotations); // Rotate the stack elements to the left
Stack.Display(stack); // Display the stack after rotation
rotations = 2; // Change the number of rotations
Console.WriteLine("\n\nRotate the stack elements to the left, Rotations = {0}", rotations);
Stack.RotateLeft(stack, rotations); // Rotate the stack elements to the left again
Stack.Display(stack); // Display the stack after rotation
rotations = 3; // Change the number of rotations
Console.WriteLine("\n\nRotate the stack elements to the left, Rotations = {0}", rotations);
Stack.RotateLeft(stack, rotations); // Rotate the stack elements to the left again
Stack.Display(stack); // Display the stack after rotation
}
}
``````

Sample Output:

```Initialize a stack:
Input some elements onto the stack:
Stack elements:
6 5 4 3 2 1

Rotate the stack elements to the left, Rotations = 1
Stack elements:
5 4 3 2 1 6

Rotate the stack elements to the left, Rotations = 2
Stack elements:
3 2 1 6 5 4

Rotate the stack elements to the left, Rotations = 3
Stack elements:
6 5 4 3 2 1
```

Flowchart:

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿