Skip to content

Instantly share code, notes, and snippets.

@ntub46010
Last active June 29, 2021 02:36
Show Gist options
  • Save ntub46010/f6d8f0c215028182c0daa2cb8da842c6 to your computer and use it in GitHub Desktop.
Save ntub46010/f6d8f0c215028182c0daa2cb8da842c6 to your computer and use it in GitHub Desktop.
public class ArrayListStack<E> implements IStack<E> {
private Object[] elements;
private int size;
public ArrayListStack() {
this.elements = new Object[0];
}
public ArrayListStack(int capacity) {
this.elements = new Object[capacity];
}
public boolean push(E data) {
if (isFull()) {
grow();
}
this.elements[this.size] = data;
this.size++;
return true;
}
public E pop() {
if (this.size == 0) {
throw new EmptyStackException();
}
int targetIndex = this.size - 1;
E data = (E) this.elements[targetIndex];
this.elements[targetIndex] = null;
this.size--;
return data;
}
public E peek() {
if (this.size == 0) {
throw new EmptyStackException();
}
return (E) this.elements[this.size - 1];
}
public int size() {
return this.size;
}
public boolean isFull() {
return this.size >= this.elements.length;
}
private void grow() {
int newCapacity = this.elements.length == 0
? 10
: (int) (this.elements.length * 1.5);
Object[] newElements = new Object[newCapacity];
for (int i = 0; i < this.elements.length; i++) {
newElements[i] = this.elements[i];
}
this.elements = newElements;
}
}
public class ArrayStack<E> implements IStack<E> {
private Object[] elements;
private int size;
public ArrayStack(int capacity) {
this.elements = new Object[capacity];
}
public boolean push(E data) {
if (isFull()) {
return false;
}
this.elements[this.size] = data;
this.size++;
return true;
}
public E pop() {
if (this.size == 0) {
throw new EmptyStackException();
}
int targetIndex = this.size - 1;
E data = (E) this.elements[targetIndex];
this.elements[targetIndex] = null;
this.size--;
return data;
}
public E peek() {
if (this.size == 0) {
throw new EmptyStackException();
}
return (E) this.elements[this.size - 1];
}
public int size() {
return this.size;
}
public boolean isFull() {
return this.size >= this.elements.length;
}
}
public interface IStack<E> {
boolean push(E data);
E pop();
E peek();
int size();
boolean isFull();
}
public class LinkedListStack<E> implements IStack<E>{
private Node<E> topNode;
private int size;
public boolean push(E data) {
Node<E> node = new Node<>(data);
node.setNext(this.topNode);
this.topNode = node;
this.size++;
return true;
}
public E pop() {
if (this.size == 0) {
throw new EmptyStackException();
}
Node<E> node = this.topNode;
this.topNode = this.topNode.getNext();
this.size--;
return node.getData();
}
public E peek() {
if (this.size == 0) {
throw new EmptyStackException();
}
return this.topNode.getData();
}
public int size() {
return this.size;
}
public boolean isFull() {
return false;
}
}
public class Node<E> {
private E data;
private Node<E> next;
public Node(E data) {
this.data = data;
}
public E getData() {
return data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment