Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
using System; public class StackArray { private int[] array; private int top; // Index of the top element private int capacity; // Public property to expose the current number of elements public int CurrentStackSize => top + 1; public StackArray(int size) { capacity = size; array = new int[capacity]; top = -1; } public void Push(int element) { if (IsFull()) { Console.WriteLine("Stack Overflow! Cannot push element. Stack is full."); return; } array[++top] = element; Console.WriteLine($"Pushed element: {element}"); } public int Pop() { if (IsEmpty()) { Console.WriteLine("Stack Underflow! Cannot pop element. Stack is empty."); return -1; } int element = array[top--]; Console.WriteLine($"Popped element: {element}"); return element; } public int Peek() { if (IsEmpty()) { Console.WriteLine("Stack is empty. Cannot peek."); return -1; } Console.WriteLine($"Top element is: {array[top]}"); return array[top]; } public bool IsEmpty() { return top == -1; } public bool IsFull() { return top == capacity - 1; } public void Display() { if (IsEmpty()) { Console.WriteLine("Stack is empty. Nothing to display."); return; } Console.Write("Stack elements (Top -> Bottom): "); for (int i = top; i >= 0; i--) { Console.Write(array[i] + (i == 0 ? "" : ", ")); } Console.WriteLine(); } } public class QueueArray { private int[] array; private int front; private int rear; private int size; private int capacity; // Public method to expose the current number of elements public int GetCurrentQueueSize() => size; public QueueArray(int capacity) { this.capacity = capacity; array = new int[this.capacity]; front = 0; rear = -1; size = 0; } public void Enqueue(int element) { if (IsFull()) { Console.WriteLine("Queue Overflow! Cannot enqueue element. Queue is full."); return; } rear = (rear + 1) % capacity; array[rear] = element; size++; Console.WriteLine($"Enqueued element: {element}"); } public int Dequeue() { if (IsEmpty()) { Console.WriteLine("Queue Underflow! Cannot dequeue element. Queue is empty."); return -1; } int element = array[front]; front = (front + 1) % capacity; size--; Console.WriteLine($"Dequeued element: {element}"); return element; } public int Peek() { if (IsEmpty()) { Console.WriteLine("Queue is empty. Cannot peek."); return -1; } Console.WriteLine($"Front element is: {array[front]}"); return array[front]; } public bool IsEmpty() { return size == 0; } public bool IsFull() { return size == capacity; } public void Display() { if (IsEmpty()) { Console.WriteLine("Queue is empty. Nothing to display."); return; } Console.Write("Queue elements (Front -> Rear): "); for (int i = 0; i < size; i++) { int index = (front + i) % capacity; Console.Write(array[index] + (i == size - 1 ? "" : ", ")); } Console.WriteLine(); } } public class Program { private static int arraySize = 0; private static StackArray stack = null; private static QueueArray queue = null; public static void Main(string[] args) { GetArraySize(); stack = new StackArray(arraySize); queue = new QueueArray(arraySize); MainMenu(); } private static void GetArraySize() { Console.Title = "Data Structures: Stack & Queue Implementation"; while (arraySize <= 0) { Console.Write("Enter the size of the array for Stack and Queue: "); if (int.TryParse(Console.ReadLine(), out int size) && size > 0) { arraySize = size; } else { Console.WriteLine("Invalid input. Please enter a positive integer for the size."); } } } private static void MainMenu() { bool running = true; while (running) { Console.WriteLine("\n=================================="); Console.WriteLine("Main Menu 🏠"); Console.WriteLine("=================================="); Console.WriteLine("1. STACK Operations (LIFO)"); Console.WriteLine("2. QUEUE Operations (FIFO)"); Console.WriteLine("3. EXIT"); Console.Write("Enter your choice (1-3): "); string choice = Console.ReadLine(); Console.WriteLine(); switch (choice) { case "1": StackSubMenu(); break; case "2": QueueSubMenu(); break; case "3": running = false; Console.WriteLine("Exiting program. Goodbye! 👋"); break; default: Console.WriteLine("Invalid choice. Please enter 1, 2, or 3."); break; } } } private static void StackSubMenu() { bool back = false; while (!back) { // FIX: Use the public property CurrentStackSize instead of the private field 'top' Console.WriteLine("\n----------------------------------"); Console.WriteLine($"STACK Menu (Size: {arraySize}, Elements: {stack.CurrentStackSize}) 📚"); Console.WriteLine("----------------------------------"); Console.WriteLine("1. Push (Add element)"); Console.WriteLine("2. Pop (Remove element)"); Console.WriteLine("3. Peek (View top element)"); Console.WriteLine("4. IsEmpty (Check if empty)"); Console.WriteLine("5. IsFull (Check if full)"); Console.WriteLine("6. View Elements"); Console.WriteLine("7. Back to Main Menu"); Console.Write("Enter your choice (1-7): "); string choice = Console.ReadLine(); Console.WriteLine(); switch (choice) { case "1": Console.Write("Enter integer to Push: "); if (int.TryParse(Console.ReadLine(), out int element)) { stack.Push(element); } else { Console.WriteLine("Invalid input. Please enter an integer."); } break; case "2": stack.Pop(); break; case "3": stack.Peek(); break; case "4": Console.WriteLine($"Stack is Empty: {stack.IsEmpty()}"); break; case "5": Console.WriteLine($"Stack is Full: {stack.IsFull()}"); break; case "6": stack.Display(); break; case "7": back = true; break; default: Console.WriteLine("Invalid choice. Please enter a number from 1 to 7."); break; } } } private static void QueueSubMenu() { bool back = false; while (!back) { // FIX: Use the public method GetCurrentQueueSize() instead of the private field 'size' Console.WriteLine("\n----------------------------------"); Console.WriteLine($"QUEUE Menu (Size: {arraySize}, Elements: {queue.GetCurrentQueueSize()}) 🚌"); Console.WriteLine("----------------------------------"); Console.WriteLine("1. Enqueue (Add element)"); Console.WriteLine("2. Dequeue (Remove element)"); Console.WriteLine("3. Peek (View front element)"); Console.WriteLine("4. IsEmpty (Check if empty)"); Console.WriteLine("5. IsFull (Check if full)"); Console.WriteLine("6. View Elements"); Console.WriteLine("7. Back to Main Menu"); Console.Write("Enter your choice (1-7): "); string choice = Console.ReadLine(); Console.WriteLine(); switch (choice) { case "1": Console.Write("Enter integer to Enqueue: "); if (int.TryParse(Console.ReadLine(), out int element)) { queue.Enqueue(element); } else { Console.WriteLine("Invalid input. Please enter an integer."); } break; case "2": queue.Dequeue(); break; case "3": queue.Peek(); break; case "4": Console.WriteLine($"Queue is Empty: {queue.IsEmpty()}"); break; case "5": Console.WriteLine($"Queue is Full: {queue.IsFull()}"); break; case "6": queue.Display(); break; case "7": back = true; break; default: Console.WriteLine("Invalid choice. Please enter a number from 1 to 7."); break; } } } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)