commit 84aa1374c779349db36c3788660633043b7acce5 Author: Giovanni Di Grezia Date: Wed Mar 5 01:13:42 2014 +0100 Crazione Stack diff --git a/com/xgiovio/Main.java b/com/xgiovio/Main.java new file mode 100644 index 0000000..24454ef --- /dev/null +++ b/com/xgiovio/Main.java @@ -0,0 +1,8 @@ +package com.xgiovio; + +public class Main { + + public static void main(String[] args) { + // write your code here + } +} diff --git a/exceptions/EmpyStackException.java b/exceptions/EmpyStackException.java new file mode 100644 index 0000000..847d3ae --- /dev/null +++ b/exceptions/EmpyStackException.java @@ -0,0 +1,14 @@ +package exceptions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.09 + */ +public class EmpyStackException extends RuntimeException { + + public EmpyStackException (){ + super("Stack Empty"); + } +} diff --git a/exceptions/FullStackException.java b/exceptions/FullStackException.java new file mode 100644 index 0000000..2df4f7a --- /dev/null +++ b/exceptions/FullStackException.java @@ -0,0 +1,14 @@ +package exceptions; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.09 + */ +public class FullStackException extends RuntimeException { + + public FullStackException(){ + super("Stack Full"); + } +} diff --git a/stack/DynamicArrayStack.java b/stack/DynamicArrayStack.java new file mode 100644 index 0000000..fbfe7b0 --- /dev/null +++ b/stack/DynamicArrayStack.java @@ -0,0 +1,80 @@ +package stack; + +import exceptions.EmpyStackException; +import exceptions.FullStackException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.12 + */ + + +public class DynamicArrayStack extends StackRules { + + + public DynamicArrayStack(){ + + stack = (E[])new Object[dcapacity]; + + } + public DynamicArrayStack(int size){ + + stack = (E[])new Object[size]; + + } + + + @Override + public void push(E element) throws FullStackException { + if (isFull()){ + E[] stack2 = (E[])new Object[(stack.length *2)]; + for (int i = 0; i<= top; i++){ + stack2[i] = stack[i]; + } + stack = stack2; + stack[++top] = element; + }else{ + stack[++top] = element; + } + } + + @Override + public E top() throws EmpyStackException { + if (isEmpty()) + throw new EmpyStackException(); + return stack[top]; + } + + @Override + public E pop() throws EmpyStackException { + if (isEmpty()) + throw new EmpyStackException(); + return stack[top--]; + } + + @Override + public boolean isEmpty() { + if (size() < 1) + return true; + return false; + } + + @Override + // not used + public boolean isFull() { + return false; + } + + @Override + public int size() { + return (++top); + } + + + private int top = -1; + private int dcapacity = 100; + private E[] stack; + +} diff --git a/stack/FixedArrayStack.java b/stack/FixedArrayStack.java new file mode 100644 index 0000000..7f1c364 --- /dev/null +++ b/stack/FixedArrayStack.java @@ -0,0 +1,74 @@ +package stack; + +import exceptions.EmpyStackException; +import exceptions.FullStackException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.12 + */ + + +public class FixedArrayStack extends StackRules { + + + public FixedArrayStack(){ + + stack = (E[])new Object[dcapacity]; + + } + public FixedArrayStack( int size){ + + stack = (E[])new Object[size]; + + } + + + @Override + public void push(E element) throws FullStackException { + if (isFull()) + throw new FullStackException(); + stack[++top] = element; + } + + @Override + public E top() throws EmpyStackException { + if (isEmpty()) + throw new EmpyStackException(); + return stack[top]; + } + + @Override + public E pop() throws EmpyStackException { + if (isEmpty()) + throw new EmpyStackException(); + return stack[top--]; + } + + @Override + public boolean isEmpty() { + if (size() < 1) + return true; + return false; + } + + @Override + public boolean isFull() { + if (stack.length == (top + 1)) + return true; + return false; + } + + @Override + public int size() { + return (++top); + } + + + private int top = -1; + private int dcapacity = 100; + private E[] stack; + +} diff --git a/stack/SinglePointerStack.java b/stack/SinglePointerStack.java new file mode 100644 index 0000000..fcb75ff --- /dev/null +++ b/stack/SinglePointerStack.java @@ -0,0 +1,66 @@ +package stack; + +import exceptions.EmpyStackException; +import exceptions.FullStackException; +import stack.utility.SinglePointerNode; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.12 + */ + + +public class SinglePointerStack extends StackRules { + + @Override + public void push(E element) throws FullStackException { + SinglePointerNode temp = new SinglePointerNode(element,stack); + stack = temp; + ++number_of_elements; + } + + + @Override + public E top() throws EmpyStackException { + if (isEmpty()) + throw new EmpyStackException(); + return stack.getElement(); + } + + @Override + public E pop() throws EmpyStackException { + SinglePointerNode to_return; + if (isEmpty()) { + throw new EmpyStackException(); + }else{ + to_return = stack; + stack = stack.getNext(); + return to_return.getElement(); + } + } + + @Override + public boolean isEmpty() { + if (size() < 1) + return true; + return false; + } + + @Override + // not used + public boolean isFull() { + return false; + } + + @Override + public int size() { + return number_of_elements; + } + + + private SinglePointerNode stack = null; + private int number_of_elements = 0; + +} diff --git a/stack/StackRules.java b/stack/StackRules.java new file mode 100644 index 0000000..63987db --- /dev/null +++ b/stack/StackRules.java @@ -0,0 +1,21 @@ +package stack; + +import exceptions.EmpyStackException; +import exceptions.FullStackException; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.06 + */ +public abstract class StackRules { + + public abstract void push (E element) throws FullStackException; + public abstract E top () throws EmpyStackException; + public abstract E pop () throws EmpyStackException; + public abstract boolean isEmpty(); + public abstract boolean isFull(); + public abstract int size(); + +} diff --git a/stack/utility/SinglePointerNode.java b/stack/utility/SinglePointerNode.java new file mode 100644 index 0000000..ebcd9ff --- /dev/null +++ b/stack/utility/SinglePointerNode.java @@ -0,0 +1,41 @@ +package stack.utility; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 05/03/14 + * Time: 0.49 + */ +public class SinglePointerNode { + + public SinglePointerNode (){ + element = null; + next = null; + + } + + public SinglePointerNode (E in_element, SinglePointerNode in_next){ + element = in_element; + next = in_next; + } + + public E getElement() { + return element; + } + + public void setElement(E element) { + this.element = element; + } + + public SinglePointerNode getNext() { + return next; + } + + public void setNext(SinglePointerNode next) { + this.next = next; + } + + private E element; + private SinglePointerNode next; + +}