If you want to create a custom class to implement stack in java, you can do something like this:-
import java.util.ArrayList;
import java.util.List;
interface StackList<T> {
public boolean push(T data);
public T pop();
public int size();
}
public class Stack<T> implements StackList<T> {
private final int max;
private int top = -1;
private final List<T> stack;
public Stack(int size) {
max = size - 1;
stack = new ArrayList<>(size);
}
public boolean push(T data) {
if (top <= max) {
top += 1;
stack.add(top, data);
return true;
}
throw new IndexOutOfBoundsException("Stack is already full");
}
public T pop() {
T data;
if (top >= 0) {
data = stack.get(top);
top -= 1;
return data;
}
throw new IndexOutOfBoundsException("Stack is already empty");
}
public int size() {
return stack.size();
}
}
You can use the above class as follows:-
import java.util.Scanner;
public class StackTest {
Stack<Integer> stack = new Stack<>(5);
int data;
Scanner scan = new Scanner(System.in);
void input() {
try {
do {
System.out.printf("Enter data to enter or -11 to exit: ");
data = scan.nextInt();
if (data == -11) {
break;
}
stack.push(data);
} while (true);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error Occurred!\n" + e.getMessage());
System.exit(0);
}
}
void size() {
System.out.println("\nStack size: " + stack.size());
}
void remove() {
try {
int data;
do {
data = stack.pop();
System.out.printf("Removed data: " + data + "\nEnter -11 to exit: ");
data = scan.nextInt();
} while (data != -11);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error Occurred!\n" + e.getMessage());
System.exit(0);
}
}
public static void main(String[] args) {
StackTest st = new StackTest();
st.input();
st.size();
st.remove();
}
}
import java.util.ArrayList;
import java.util.List;
interface StackList<T> {
public boolean push(T data);
public T pop();
public int size();
}
public class Stack<T> implements StackList<T> {
private final int max;
private int top = -1;
private final List<T> stack;
public Stack(int size) {
max = size - 1;
stack = new ArrayList<>(size);
}
public boolean push(T data) {
if (top <= max) {
top += 1;
stack.add(top, data);
return true;
}
throw new IndexOutOfBoundsException("Stack is already full");
}
public T pop() {
T data;
if (top >= 0) {
data = stack.get(top);
top -= 1;
return data;
}
throw new IndexOutOfBoundsException("Stack is already empty");
}
public int size() {
return stack.size();
}
}
You can use the above class as follows:-
import java.util.Scanner;
public class StackTest {
Stack<Integer> stack = new Stack<>(5);
int data;
Scanner scan = new Scanner(System.in);
void input() {
try {
do {
System.out.printf("Enter data to enter or -11 to exit: ");
data = scan.nextInt();
if (data == -11) {
break;
}
stack.push(data);
} while (true);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error Occurred!\n" + e.getMessage());
System.exit(0);
}
}
void size() {
System.out.println("\nStack size: " + stack.size());
}
void remove() {
try {
int data;
do {
data = stack.pop();
System.out.printf("Removed data: " + data + "\nEnter -11 to exit: ");
data = scan.nextInt();
} while (data != -11);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error Occurred!\n" + e.getMessage());
System.exit(0);
}
}
public static void main(String[] args) {
StackTest st = new StackTest();
st.input();
st.size();
st.remove();
}
}
Comments