Crazione Stack

This commit is contained in:
2014-03-05 01:13:42 +01:00
commit 84aa1374c7
8 changed files with 318 additions and 0 deletions

View File

@@ -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<E> extends StackRules<E> {
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;
}

View File

@@ -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<E> extends StackRules<E> {
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;
}

View File

@@ -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<E> extends StackRules<E> {
@Override
public void push(E element) throws FullStackException {
SinglePointerNode<E> temp = new SinglePointerNode<E>(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<E> 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<E> stack = null;
private int number_of_elements = 0;
}

21
stack/StackRules.java Normal file
View File

@@ -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 <E> {
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();
}

View File

@@ -0,0 +1,41 @@
package stack.utility;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 05/03/14
* Time: 0.49
*/
public class SinglePointerNode<E> {
public SinglePointerNode (){
element = null;
next = null;
}
public SinglePointerNode (E in_element, SinglePointerNode<E> in_next){
element = in_element;
next = in_next;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public SinglePointerNode<E> getNext() {
return next;
}
public void setNext(SinglePointerNode<E> next) {
this.next = next;
}
private E element;
private SinglePointerNode<E> next;
}