0% found this document useful (0 votes)
17 views2 pages

Stack

This document defines a Stack class in Java that implements a stack data structure using an array. The class includes methods to check if the stack is empty or full, peek at the top element, and push or pop elements onto and off of the stack. It also includes a main method that demonstrates using the stack by prompting a user for operations like push, pop, peek, and display until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Stack

This document defines a Stack class in Java that implements a stack data structure using an array. The class includes methods to check if the stack is empty or full, peek at the top element, and push or pop elements onto and off of the stack. It also includes a main method that demonstrates using the stack by prompting a user for operations like push, pop, peek, and display until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;

class Stack {
public int capacity;
public int top = -1;
public int[] a;
Scanner sc = new Scanner(System.in);

public Stack(int capacity) {


this.capacity = capacity;
a = new int[capacity];
}

public int size() {


return (top + 1);
}

public boolean isEmpty() {


return (top == -1);
}

public boolean isFull() {


return (size() == capacity);
}

public int peek() {


if (isEmpty()) {
System.out.println("Stack is empty!");
return -1; // Return a default value or signal an error condition.
}
return a[top];
}

public void push(int data) {


if (isFull()) {
System.out.println("Stack Overflow!");
} else {
top++;
a[top] = data;
}
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack Underflow!");
return -1; // Return a default value or signal an error condition.
} else {
int temp = top;
top--;
return a[temp];
}
}

public void display() {


int i = top;
while (i >= 0) {
System.out.print(a[i] + " ");
i--;
}
System.out.println();
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter size of stack");
int capacity = sc.nextInt();
Stack stack = new Stack(capacity);

while (true) {
System.out.println("1. Push 2. Pop 3. Peek 4. Display 5. Exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter element to push:");
int data = sc.nextInt();
stack.push(data);
break;
case 2:
int popped = stack.pop();
if (popped != -1) {
System.out.println("Popped element: " + popped);
}
break;
case 3:
int peeked = stack.peek();
if (peeked != -1) {
System.out.println("Peeked element: " + peeked);
}
break;
case 4:
stack.display();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy