Skip to content

Instantly share code, notes, and snippets.

@TheGloriousGenesis
Created May 10, 2020 22:32
Show Gist options
  • Save TheGloriousGenesis/75bbcdb0e24a43f1481a19d14b50546a to your computer and use it in GitHub Desktop.
Save TheGloriousGenesis/75bbcdb0e24a43f1481a19d14b50546a to your computer and use it in GitHub Desktop.
Stack data type with max
public class StackWithMax {
private Node head;
private int maximum = 0;
private int pointer = 0;
public StackWithMax() {
}
public void push(final Integer value) {
if (value >= maximum) {
maximum = value;
}
if (head == null) {
head = new Node(value);
} else {
Node newHead = new Node(value);
newHead.next = head;
head = newHead;
}
pointer++;
}
public int pop() {
int removedData;
if (pointer == 0) {
removedData = Integer.MIN_VALUE;
} else if (pointer == 1) {
removedData = head.data;
head = null;
} else {
removedData = head.data;
head = head.next;
}
pointer--;
if (maximum == removedData) {
maximum = Integer.MIN_VALUE;
}
findMax();
return removedData;
}
private void findMax() {
Node current = head;
while (current != null) {
if (current.data > maximum) {
maximum = current.data;
}
current = current.next;
}
}
private class Node {
private int data;
private Node next;
public Node(final int data) {
this.data = data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment