Stack Implementation example in Java

For beginners, using ArrayList

Image for post
Image for post

The concept

Image for post
Image for post
Empty stack
Image for post
Image for post
Stack with one element
Image for post
Image for post
Stack with three elements
Image for post
Image for post
Stack with two elements

What is LIFO?

Image for post
Image for post
Last In First Out (LIFO)

The Code

class StackImpl<T> {
}
// Creating a private array list of the generic T class.
private List<T> list = new ArrayList<T>();
void push(T value) {
list.add(value);
}
T pop() {
return this.list.get(this.list.size() — 1);
}
T pop() {    // Getting the last value from the list.
T value = this.list.get(this.list.size() - 1);
// Removing the last value from the list,
// thereby popping the last value.
this.list.remove(this.list.size() - 1);
return value;
}
public class StackEmptyException extends Exception {    public StackEmptyException(String message) {
super(message);
}
}
T pop() throws StackEmptyException {

// Throwing an exception if the list is empty.
if (this.list.isEmpty()) {
throw new StackEmptyException("The stack is empty. Push a value before popping it.");
}

// Getting the last value from the list.
T value = this.list.get(this.list.size() - 1);

// Removing the last value from the list, thereby popping the last value.
this.list.remove(this.list.size() - 1);

return value;
}
public long size() {
return this.list.size();
}
List<T> getStackAndEmpty() {
List<T> stack = new ArrayList<T>(this.list);

this.list.removeAll(stack);

return stack;
}
void flush() {
this.list = new ArrayList<>();
}
class StackImpl<T> {

// Creating a private array list of the generic T class.
private List<T> list = new ArrayList<T>();

public long size() {
return this.list.size();
}

void push(T value) {
list.add(value);
}

T pop() throws StackEmptyException {

// Throwing an exception if the list is empty.
if (this.list.isEmpty()) {
throw new StackEmptyException("The stack is empty. Push a value before popping it.");
}

// Getting the last value from the list.
T value = this.list.get(this.list.size() - 1);

// Removing the last value from the list, thereby popping the last value.
this.list.remove(this.list.size() - 1);

return value;
}

List<T> getStackAndEmpty() {
List<T> stack = new ArrayList<T>(this.list);

this.list.removeAll(stack);

return stack;
}

void flush() {
this.list = new ArrayList<>();
}
}
StackImpl<String> stack = new StackImpl<String>();
stack.push("First");
stack.push("Second");
String value = stack.pop();
System.out.println("Popped value: " + value);
Popped value: Second
StackImpl<String> stack = new StackImpl<String>();

stack.push("First");
stack.push("Second");

String value = stack.pop();
System.out.println("Popped value: " + value);

stack.push("Third");

List<String> completeStack = stack.getStackAndEmpty();

System.out.println("Complete stack: " + completeStack);

stack.push("First");
stack.push("Second");

long stackSize = stack.size();
System.out.println("Stack size before flush: " + stackSize);

stack.flush();

stackSize = stack.size();
System.out.println("Stack size after flush: " + stackSize);

String lastValue = stack.pop();
StackImpl<Integer> stack = new StackImpl<Integer>();

stack.push(100);
stack.push(200);

Integer value = stack.pop();
System.out.println("Popped value: " + value);

stack.push(300);

List<Integer> completeStack = stack.getStackAndEmpty();

System.out.println("Complete stack: " + completeStack);

stack.push(100);
stack.push(200);

long stackSize = stack.size();
System.out.println("Stack size before flush: " + stackSize);

stack.flush();

stackSize = stack.size();
System.out.println("Stack size after flush: " + stackSize);

Integer lastValue = stack.pop();
public class App {

public static void main(String[] args) {

stringStackExample();

integerStackExample();

}

private static void integerStackExample() {

try {
StackImpl<Integer> stack = new StackImpl<Integer>();

stack.push(100);
stack.push(200);

Integer value = stack.pop();
System.out.println("Popped value: " + value);

stack.push(300);

List<Integer> completeStack = stack.getStackAndEmpty();

System.out.println("Complete stack: " + completeStack);

stack.push(100);
stack.push(200);

long stackSize = stack.size();
System.out.println("Stack size before flush: " + stackSize);

stack.flush();

stackSize = stack.size();
System.out.println("Stack size after flush: " + stackSize);

Integer lastValue = stack.pop();

} catch (StackEmptyException e) {
System.out.println("Error: " + e.getMessage());
}
}

private static void stringStackExample() {

try {
StackImpl<String> stack = new StackImpl<String>();

stack.push("First");
stack.push("Second");

String value = stack.pop();
System.out.println("Popped value: " + value);

stack.push("Third");

List<String> completeStack = stack.getStackAndEmpty();

System.out.println("Complete stack: " + completeStack);

stack.push("First");
stack.push("Second");

long stackSize = stack.size();
System.out.println("Stack size before flush: " + stackSize);

stack.flush();

stackSize = stack.size();
System.out.println("Stack size after flush: " + stackSize);

String lastValue = stack.pop();

} catch (StackEmptyException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Coding, machine learning, reading, sleeping, listening, potato. https://blog.contactsunny.com and https://www.linkedin.com/in/sunnysrinidhi/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store