Sitemap

Stack Using Array in Java

2 min readAug 12, 2024

--

import java.util.Scanner;

class Stack {
private int maxSize;
private int[] stackArray;
private int top;

// Constructor to initialize the stack
public Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

// Push operation
public void push(int value) {
if (top >= maxSize - 1) {
System.out.println("Stack Overflow! Unable to push " + value);
} else {
stackArray[++top] = value;
System.out.println("Pushed " + value + " onto the stack.");
}
}

// Pop operation
public int pop() {
if (top < 0) {
System.out.println("Stack Underflow! No element to pop.");
return -1;
} else {
int value = stackArray[top--];
System.out.println("Popped " + value + " from the stack.");
return value;
}
}

// Peek (Peep) operation
public int peek() {
if (top < 0) {
System.out.println("Stack is empty! No element to peek.");
return -1;
} else {
return stackArray[top];
}
}

// Change operation (replaces the value at a given position)
public void change(int position, int newValue) {
if (position < 1 || position > top + 1) {
System.out.println("Invalid position! No change performed.");
} else {
stackArray[position - 1] = newValue;
System.out.println("Changed element at position " + position + " to " + newValue);
}
}

// Display stack elements
public void display() {
if (top < 0) {
System.out.println("Stack is empty!");
} else {
System.out.println("Stack elements are:");
for (int i = top; i >= 0; i--) {
System.out.println(stackArray[i]);
}
}
}
}

// Main class to run the Stack operations
public class Stack_using_array {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the stack: ");
int size = scanner.nextInt();

Stack stack = new Stack(size);
boolean quit = false;

while (!quit) {
System.out.println("\nStack Operations Menu:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Change");
System.out.println("5. Display");
System.out.println("6. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter value to push: ");
int value = scanner.nextInt();
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
int peekValue = stack.peek();
if (peekValue != -1) {
System.out.println("Top element is: " + peekValue);
}
break;
case 4:
System.out.print("Enter position to change: ");
int position = scanner.nextInt();
System.out.print("Enter new value: ");
int newValue = scanner.nextInt();
stack.change(position, newValue);
break;
case 5:
stack.display();
break;
case 6:
quit = true;
break;
default:
System.out.println("Invalid choice! Please try again.");
}
}

scanner.close();
}
}

Get Gohil P’s stories in your inbox

Join Medium for free to get updates from this writer.

Overview

The code defines a simple stack data structure using an array in Java, encapsulated within a Stack class. It also provides a command-line interface to interact with this stack, allowing users to perform various stack operations.

--

--