Crazione Stack
This commit is contained in:
8
com/xgiovio/Main.java
Normal file
8
com/xgiovio/Main.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.xgiovio;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// write your code here
|
||||||
|
}
|
||||||
|
}
|
||||||
14
exceptions/EmpyStackException.java
Normal file
14
exceptions/EmpyStackException.java
Normal file
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
14
exceptions/FullStackException.java
Normal file
14
exceptions/FullStackException.java
Normal file
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
80
stack/DynamicArrayStack.java
Normal file
80
stack/DynamicArrayStack.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
74
stack/FixedArrayStack.java
Normal file
74
stack/FixedArrayStack.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
66
stack/SinglePointerStack.java
Normal file
66
stack/SinglePointerStack.java
Normal 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
21
stack/StackRules.java
Normal 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();
|
||||||
|
|
||||||
|
}
|
||||||
41
stack/utility/SinglePointerNode.java
Normal file
41
stack/utility/SinglePointerNode.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user