eliminato classi superflue del progetto e risolto qualche bug. c'è ancora del lavoro da fare sulla queue e deque
This commit is contained in:
45
com/xgiovio/ArrayTest.java
Normal file
45
com/xgiovio/ArrayTest.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.xgiovio;
|
||||||
|
|
||||||
|
import general_utility.test_object;
|
||||||
|
import stack.ArrayStack;
|
||||||
|
import stack.FixedArrayStack;
|
||||||
|
import stack.NodeStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with xgiovio.macbookair.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 23/03/14
|
||||||
|
* Time: 20:37
|
||||||
|
*/
|
||||||
|
public class ArrayTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NodeStack<test_object> a = new NodeStack<test_object>();
|
||||||
|
a.push(new test_object(10));
|
||||||
|
a.push(new test_object(20));
|
||||||
|
a.push(new test_object(30));
|
||||||
|
a.push(new test_object(40));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
a.pop();
|
||||||
|
a.pop();
|
||||||
|
|
||||||
|
|
||||||
|
System.out.print(a.toString());
|
||||||
|
|
||||||
|
a.push(new test_object(50));
|
||||||
|
a.push(new test_object(60));
|
||||||
|
|
||||||
|
System.out.print(a.toString());
|
||||||
|
a.pop();
|
||||||
|
System.out.print(a.toString());
|
||||||
|
System.out.print(a.top().toString());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package exceptions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created with MONSTER.
|
|
||||||
* User: xgiovio
|
|
||||||
* Date: 05/03/14
|
|
||||||
* Time: 0.09
|
|
||||||
*/
|
|
||||||
public class FullQueueException extends RuntimeException {
|
|
||||||
|
|
||||||
public FullQueueException(){
|
|
||||||
super("Queue Full");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package exceptions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created with MONSTER.
|
|
||||||
* User: xgiovio
|
|
||||||
* Date: 05/03/14
|
|
||||||
* Time: 0.09
|
|
||||||
*/
|
|
||||||
public class FullStackException extends RuntimeException {
|
|
||||||
|
|
||||||
public FullStackException(){
|
|
||||||
super("Stack Full");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -26,14 +26,14 @@ public class ArrayQueue<E> implements Queue<E> {
|
|||||||
}
|
}
|
||||||
queue[rear]= element;
|
queue[rear]= element;
|
||||||
rear = (rear + 1) % queue.length;
|
rear = (rear + 1) % queue.length;
|
||||||
if ( rear == front ){
|
if ( rear ==( front - 1) ){
|
||||||
//reallocate;
|
//reallocate;
|
||||||
E[] new_queue = (E[]) new Object[queue.length * 2];
|
E[] new_queue = (E[]) new Object[queue.length * 2];
|
||||||
for (int i= 0 ;i<queue.length; i++){
|
for (int i= 0 ;i<(queue.length -1); i++){
|
||||||
new_queue[i] = queue[front];
|
new_queue[i] = queue[front];
|
||||||
front = (front + 1) % queue.length;
|
front = (front + 1) % queue.length;
|
||||||
}
|
}
|
||||||
rear = queue.length;
|
rear = queue.length - 1;
|
||||||
front = 0;
|
front = 0;
|
||||||
queue = new_queue;
|
queue = new_queue;
|
||||||
|
|
||||||
|
|||||||
@@ -1,103 +0,0 @@
|
|||||||
package queue;
|
|
||||||
|
|
||||||
import exceptions.EmptyQueueException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created with xgiovio.macbookair.
|
|
||||||
* User: xgiovio
|
|
||||||
* Date: 10/03/14
|
|
||||||
* Time: 15:49
|
|
||||||
*/
|
|
||||||
public class ArrayQueueNoBlank<E> implements Queue<E> {
|
|
||||||
|
|
||||||
public ArrayQueueNoBlank(){
|
|
||||||
queue = (E[])new Object[def_size];
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayQueueNoBlank(int in_size){
|
|
||||||
queue = (E[])new Object[in_size];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void enqueue(E element) {
|
|
||||||
if (front == -1){
|
|
||||||
front++;
|
|
||||||
}
|
|
||||||
queue[rear]= element;
|
|
||||||
rear = (rear + 1) % queue.length;
|
|
||||||
if ( rear == front ){
|
|
||||||
//reallocate;
|
|
||||||
E[] new_queue = (E[]) new Object[queue.length * 2];
|
|
||||||
for (int i= 0 ;i<queue.length; i++){
|
|
||||||
new_queue[i] = queue[front];
|
|
||||||
front = (front + 1) % queue.length;
|
|
||||||
}
|
|
||||||
rear = queue.length;
|
|
||||||
front = 0;
|
|
||||||
queue = new_queue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E dequeue() throws EmptyQueueException {
|
|
||||||
|
|
||||||
if (front == -1){
|
|
||||||
throw new EmptyQueueException();
|
|
||||||
}else{
|
|
||||||
E temp = queue[front];
|
|
||||||
front++;
|
|
||||||
if (front == rear){
|
|
||||||
front = -1;
|
|
||||||
rear = 0;
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E front() throws EmptyQueueException {
|
|
||||||
if (front == -1){
|
|
||||||
throw new EmptyQueueException();
|
|
||||||
}else{
|
|
||||||
return queue[front];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
if (size() == 0){
|
|
||||||
return true;
|
|
||||||
}else{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isFull() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
if (front == -1){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (front < rear){
|
|
||||||
return (rear - front);
|
|
||||||
}else{
|
|
||||||
return (queue.length - front + rear);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int def_size = 100;
|
|
||||||
E[] queue ;
|
|
||||||
int front = -1;
|
|
||||||
int rear = 0;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
package queue;
|
|
||||||
|
|
||||||
import exceptions.EmptyQueueException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created with xgiovio.macbookair.
|
|
||||||
* User: xgiovio
|
|
||||||
* Date: 10/03/14
|
|
||||||
* Time: 15:49
|
|
||||||
*/
|
|
||||||
public class FixedArrayQueue<E> implements Queue<E> {
|
|
||||||
|
|
||||||
public FixedArrayQueue(){
|
|
||||||
queue = (E[])new Object[def_size];
|
|
||||||
}
|
|
||||||
|
|
||||||
public FixedArrayQueue(int in_size){
|
|
||||||
queue = (E[])new Object[in_size];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void enqueue(E element) {
|
|
||||||
if (front == -1){
|
|
||||||
front++;
|
|
||||||
}
|
|
||||||
queue[rear]= element;
|
|
||||||
rear = (rear + 1) % queue.length;
|
|
||||||
if ( rear == front ){
|
|
||||||
//reallocate;
|
|
||||||
E[] new_queue = (E[]) new Object[queue.length * 2];
|
|
||||||
for (int i= 0 ;i<queue.length; i++){
|
|
||||||
new_queue[i] = queue[front];
|
|
||||||
front = (front + 1) % queue.length;
|
|
||||||
}
|
|
||||||
rear = queue.length;
|
|
||||||
front = 0;
|
|
||||||
queue = new_queue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E dequeue() throws EmptyQueueException {
|
|
||||||
|
|
||||||
if (front == -1){
|
|
||||||
throw new EmptyQueueException();
|
|
||||||
}else{
|
|
||||||
E temp = queue[front];
|
|
||||||
front++;
|
|
||||||
if (front == rear){
|
|
||||||
front = -1;
|
|
||||||
rear = 0;
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E front() throws EmptyQueueException {
|
|
||||||
if (front == -1){
|
|
||||||
throw new EmptyQueueException();
|
|
||||||
}else{
|
|
||||||
return queue[front];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
if (size() == 0){
|
|
||||||
return true;
|
|
||||||
}else{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isFull() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
if (front == -1){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (front < rear){
|
|
||||||
return (rear - front);
|
|
||||||
}else{
|
|
||||||
return (queue.length - front + rear);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int def_size = 100;
|
|
||||||
E[] queue ;
|
|
||||||
int front = -1;
|
|
||||||
int rear = 0;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package stack;
|
package stack;
|
||||||
|
|
||||||
import exceptions.EmptyStackException;
|
import exceptions.EmptyStackException;
|
||||||
import exceptions.FullStackException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created with MONSTER.
|
* Created with MONSTER.
|
||||||
@@ -27,16 +27,18 @@ public class ArrayStack<E> implements Stack<E> {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void push(E element) throws FullStackException {
|
public void push(E element) {
|
||||||
if (isFull()){
|
if (stack.length == (top + 1)){
|
||||||
E[] stack2 = (E[])new Object[(stack.length *2)];
|
E[] stack2 = (E[])new Object[(stack.length *2)];
|
||||||
for (int i = 0; i<= top; i++){
|
for (int i = 0; i<= top; i++){
|
||||||
stack2[i] = stack[i];
|
stack2[i] = stack[i];
|
||||||
}
|
}
|
||||||
stack = stack2;
|
stack = stack2;
|
||||||
stack[++top] = element;
|
top++;
|
||||||
|
stack[top] = element;
|
||||||
}else{
|
}else{
|
||||||
stack[++top] = element;
|
top++;
|
||||||
|
stack[top] = element;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +53,8 @@ public class ArrayStack<E> implements Stack<E> {
|
|||||||
public E pop() throws EmptyStackException {
|
public E pop() throws EmptyStackException {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new EmptyStackException();
|
||||||
return stack[top--];
|
top--;
|
||||||
|
return stack[top + 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -69,9 +72,24 @@ public class ArrayStack<E> implements Stack<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return (++top);
|
return (top + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[";
|
||||||
|
for (int i = 0;i<=top;i++){
|
||||||
|
if ( i == top){
|
||||||
|
to_return+=(stack[i].toString());
|
||||||
|
}else{
|
||||||
|
to_return+=(stack[i].toString() + ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
to_return = to_return + "]";
|
||||||
|
|
||||||
|
return to_return;
|
||||||
|
}
|
||||||
|
|
||||||
private int top = -1;
|
private int top = -1;
|
||||||
private int dcapacity = 100;
|
private int dcapacity = 100;
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
package stack;
|
|
||||||
|
|
||||||
import exceptions.EmptyStackException;
|
|
||||||
import exceptions.FullStackException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created with MONSTER.
|
|
||||||
* User: xgiovio
|
|
||||||
* Date: 05/03/14
|
|
||||||
* Time: 0.12
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
public class FixedArrayStack<E> implements Stack<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 EmptyStackException {
|
|
||||||
if (isEmpty())
|
|
||||||
throw new EmptyStackException();
|
|
||||||
return stack[top];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E pop() throws EmptyStackException {
|
|
||||||
if (isEmpty())
|
|
||||||
throw new EmptyStackException();
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package stack;
|
package stack;
|
||||||
|
|
||||||
import exceptions.EmptyStackException;
|
import exceptions.EmptyStackException;
|
||||||
import exceptions.FullStackException;
|
|
||||||
import stack.utility.Node;
|
import stack.utility.Node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,7 +14,7 @@ import stack.utility.Node;
|
|||||||
public class NodeStack<E> implements Stack<E> {
|
public class NodeStack<E> implements Stack<E> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void push(E element) throws FullStackException {
|
public void push(E element) {
|
||||||
Node<E> temp = new Node<E>(element,stack);
|
Node<E> temp = new Node<E>(element,stack);
|
||||||
stack = temp;
|
stack = temp;
|
||||||
++number_of_elements;
|
++number_of_elements;
|
||||||
@@ -60,6 +59,24 @@ public class NodeStack<E> implements Stack<E> {
|
|||||||
return number_of_elements;
|
return number_of_elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String to_return = "";
|
||||||
|
to_return = to_return + "[";
|
||||||
|
|
||||||
|
for ( Node<E> temp = stack; temp != null ; temp = temp.getNext()){
|
||||||
|
|
||||||
|
if ( temp.getNext() == null){
|
||||||
|
to_return+=(temp.getElement().toString());
|
||||||
|
}else{
|
||||||
|
to_return+=( temp.getElement().toString() + ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
to_return = to_return + "]";
|
||||||
|
|
||||||
|
return to_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private Node<E> stack = null;
|
private Node<E> stack = null;
|
||||||
private int number_of_elements = 0;
|
private int number_of_elements = 0;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package stack;
|
package stack;
|
||||||
|
|
||||||
import exceptions.EmptyStackException;
|
import exceptions.EmptyStackException;
|
||||||
import exceptions.FullStackException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created with MONSTER.
|
* Created with MONSTER.
|
||||||
@@ -11,7 +11,7 @@ import exceptions.FullStackException;
|
|||||||
*/
|
*/
|
||||||
public interface Stack<E> {
|
public interface Stack<E> {
|
||||||
|
|
||||||
public void push (E element) throws FullStackException;
|
public void push (E element) ;
|
||||||
public E top () throws EmptyStackException;
|
public E top () throws EmptyStackException;
|
||||||
public E pop () throws EmptyStackException;
|
public E pop () throws EmptyStackException;
|
||||||
public boolean isEmpty();
|
public boolean isEmpty();
|
||||||
|
|||||||
Reference in New Issue
Block a user