eliminato classi superflue del progetto e risolto qualche bug. c'è ancora del lavoro da fare sulla queue e deque

This commit is contained in:
2014-03-23 22:00:57 +01:00
parent 6f3e597f65
commit e3059ed799
10 changed files with 94 additions and 322 deletions

View 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());
}
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -26,14 +26,14 @@ public class ArrayQueue<E> implements Queue<E> {
}
queue[rear]= element;
rear = (rear + 1) % queue.length;
if ( rear == front ){
if ( rear ==( front - 1) ){
//reallocate;
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];
front = (front + 1) % queue.length;
}
rear = queue.length;
rear = queue.length - 1;
front = 0;
queue = new_queue;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -1,7 +1,7 @@
package stack;
import exceptions.EmptyStackException;
import exceptions.FullStackException;
/**
* Created with MONSTER.
@@ -27,16 +27,18 @@ public class ArrayStack<E> implements Stack<E> {
@Override
public void push(E element) throws FullStackException {
if (isFull()){
public void push(E element) {
if (stack.length == (top + 1)){
E[] stack2 = (E[])new Object[(stack.length *2)];
for (int i = 0; i<= top; i++){
stack2[i] = stack[i];
}
stack = stack2;
stack[++top] = element;
top++;
stack[top] = element;
}else{
stack[++top] = element;
top++;
stack[top] = element;
}
}
@@ -51,7 +53,8 @@ public class ArrayStack<E> implements Stack<E> {
public E pop() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
return stack[top--];
top--;
return stack[top + 1];
}
@Override
@@ -69,9 +72,24 @@ public class ArrayStack<E> implements Stack<E> {
@Override
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 dcapacity = 100;

View File

@@ -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;
}

View File

@@ -1,7 +1,6 @@
package stack;
import exceptions.EmptyStackException;
import exceptions.FullStackException;
import stack.utility.Node;
/**
@@ -15,7 +14,7 @@ import stack.utility.Node;
public class NodeStack<E> implements Stack<E> {
@Override
public void push(E element) throws FullStackException {
public void push(E element) {
Node<E> temp = new Node<E>(element,stack);
stack = temp;
++number_of_elements;
@@ -60,6 +59,24 @@ public class NodeStack<E> implements Stack<E> {
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 int number_of_elements = 0;

View File

@@ -1,7 +1,7 @@
package stack;
import exceptions.EmptyStackException;
import exceptions.FullStackException;
/**
* Created with MONSTER.
@@ -11,7 +11,7 @@ import exceptions.FullStackException;
*/
public interface Stack<E> {
public void push (E element) throws FullStackException;
public void push (E element) ;
public E top () throws EmptyStackException;
public E pop () throws EmptyStackException;
public boolean isEmpty();