Implementato alcuni esercizi
This commit is contained in:
65
esercizi/QStack.java
Normal file
65
esercizi/QStack.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package esercizi;
|
||||
|
||||
import exceptions.EmptyStackException;
|
||||
import queue.NodeQueue;
|
||||
import stack.Stack;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 13/04/2014
|
||||
* Time: 23:48
|
||||
*/
|
||||
|
||||
// emulate a stack with a queue
|
||||
public class QStack<E> implements Stack<E> {
|
||||
|
||||
NodeQueue<E> a = new NodeQueue<E>();
|
||||
|
||||
@Override
|
||||
public void push(E element) {
|
||||
a.enqueue(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E top() throws EmptyStackException {
|
||||
if (a.isEmpty()){
|
||||
throw new EmptyStackException();
|
||||
}else {
|
||||
E t;
|
||||
for (int i = 0 ;i<a.size() - 1;i++){
|
||||
a.enqueue(a.dequeue());
|
||||
}
|
||||
t = a.front();
|
||||
a.enqueue(a.dequeue());
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() throws EmptyStackException {
|
||||
if (a.isEmpty()){
|
||||
throw new EmptyStackException();
|
||||
}else {
|
||||
for (int i = 0 ;i<a.size() - 1;i++){
|
||||
a.enqueue(a.dequeue());
|
||||
}
|
||||
return a.dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return a.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return a.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return a.toString();
|
||||
}
|
||||
}
|
||||
85
esercizi/SQueue.java
Normal file
85
esercizi/SQueue.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package esercizi;
|
||||
|
||||
import exceptions.EmptyQueueException;
|
||||
import queue.Queue;
|
||||
import sequence.ArraySequence;
|
||||
import stack.ArrayStack;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 13/04/2014
|
||||
* Time: 23:01
|
||||
*/
|
||||
|
||||
// queue emulated via 2 stacks
|
||||
|
||||
public class SQueue<E> implements Queue<E> {
|
||||
|
||||
|
||||
private ArrayStack<E> accoda = new ArrayStack<E>();
|
||||
private ArrayStack<E> decoda = new ArrayStack<E>();
|
||||
|
||||
@Override
|
||||
public void enqueue(E element) {
|
||||
|
||||
accoda.push(element);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public E dequeue() throws EmptyQueueException {
|
||||
|
||||
if (accoda.isEmpty()){
|
||||
throw new EmptyQueueException();
|
||||
} else {
|
||||
|
||||
E temp;
|
||||
int size = accoda.size();
|
||||
for (int i = 0; i <size; i++) {
|
||||
decoda.push(accoda.pop());
|
||||
}
|
||||
temp = decoda.pop();
|
||||
size = decoda.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
accoda.push(decoda.pop());
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E front() throws EmptyQueueException {
|
||||
if (accoda.isEmpty()){
|
||||
throw new EmptyQueueException();
|
||||
} else {
|
||||
|
||||
E temp;
|
||||
int size = accoda.size();
|
||||
for (int i = 0; i <size; i++) {
|
||||
decoda.push(accoda.pop());
|
||||
}
|
||||
temp = decoda.top();
|
||||
size = decoda.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
accoda.push(decoda.pop());
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return accoda.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return accoda.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return accoda.toString();
|
||||
}
|
||||
}
|
||||
69
esercizi/start.java
Normal file
69
esercizi/start.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package esercizi;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 13/04/2014
|
||||
* Time: 22:18
|
||||
*/
|
||||
public class start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println(static_methods.inverti("ehila"));
|
||||
|
||||
System.out.println(static_methods.checkParentheses("[()()()]([])"));
|
||||
|
||||
System.out.println (static_methods.isPalindrome("a"));
|
||||
|
||||
|
||||
///////////// test squeue
|
||||
SQueue<Integer> a = new SQueue<Integer>();
|
||||
a.enqueue(5);
|
||||
a.enqueue(10);
|
||||
a.enqueue(20);
|
||||
a.enqueue(30);
|
||||
a.enqueue(40);
|
||||
System.out.println (a);
|
||||
System.out.println (a.front());
|
||||
System.out.println (a.dequeue());
|
||||
System.out.println (a);
|
||||
a.enqueue(60);
|
||||
System.out.println (a);
|
||||
System.out.println (a.front());
|
||||
|
||||
System.out.println (static_methods.extract(a,0));
|
||||
///////////// end test squeue
|
||||
|
||||
|
||||
|
||||
QStack<Integer> b = new QStack<Integer>();
|
||||
b.push(1);
|
||||
b.push(2);
|
||||
b.push(3);
|
||||
b.push(4);
|
||||
|
||||
System.out.println (b);
|
||||
System.out.println (b.top());
|
||||
System.out.println (b.top());
|
||||
System.out.println (b.pop());
|
||||
System.out.println (b);
|
||||
System.out.println (b.top());
|
||||
|
||||
System.out.println (a);
|
||||
System.out.println (static_methods.isContained(a,115));
|
||||
System.out.println (a);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
193
esercizi/static_methods.java
Normal file
193
esercizi/static_methods.java
Normal file
@@ -0,0 +1,193 @@
|
||||
package esercizi;
|
||||
|
||||
import queue.NodeQueue;
|
||||
import queue.Queue;
|
||||
import stack.ArrayStack;
|
||||
import exceptions.*;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 13/04/2014
|
||||
* Time: 22:19
|
||||
*/
|
||||
public class static_methods {
|
||||
|
||||
|
||||
|
||||
// inver a string using a stack
|
||||
public static String inverti (String s){
|
||||
ArrayStack<Character> a = new ArrayStack<Character>();
|
||||
for (int i = 0 ;i< s.length();i++){
|
||||
a.push(s.charAt(i));
|
||||
}
|
||||
|
||||
String to_return = "";
|
||||
|
||||
for (int i = 0 ;i< s.length();i++){
|
||||
to_return += a.pop();
|
||||
}
|
||||
|
||||
return to_return;
|
||||
|
||||
}
|
||||
|
||||
// verify is a string of [ and ( is correct using a stack
|
||||
public static boolean checkParentheses (String s) {
|
||||
|
||||
ArrayStack<Character> a = new ArrayStack<Character>();
|
||||
|
||||
for (int i = 0 ; i< s.length();i++){
|
||||
|
||||
if (s.charAt(i) == '('){
|
||||
a.push(s.charAt(i));
|
||||
continue;
|
||||
}
|
||||
if (s.charAt(i) == ')'){
|
||||
if (a.isEmpty() || a.top() != '('){
|
||||
return false;
|
||||
} else {
|
||||
a.pop();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (s.charAt(i) == '['){
|
||||
if ( a.isEmpty() || a.top() != '(') {
|
||||
a.push(s.charAt(i));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
|
||||
}
|
||||
if (s.charAt(i) == ']'){
|
||||
if (a.isEmpty() || a.top() != '['){
|
||||
return false;
|
||||
} else {
|
||||
a.pop();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (a.isEmpty()){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// verify is a string is palindrome using a stack
|
||||
public static boolean isPalindrome (String s){
|
||||
|
||||
ArrayStack<Character> a = new ArrayStack<Character>();
|
||||
|
||||
if (s.length() < 2){
|
||||
return true;
|
||||
}else {
|
||||
|
||||
boolean pari;
|
||||
|
||||
if (s.length() % 2 == 0) {
|
||||
pari = true;
|
||||
} else {
|
||||
pari = false;
|
||||
}
|
||||
|
||||
|
||||
for (int i= 0 ;i < s.length();i++){
|
||||
if (!pari){
|
||||
if (i <= s.length()/2){
|
||||
a.push(s.charAt(i));
|
||||
if (i == s.length()/2 ){
|
||||
a.pop();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (i <= (s.length()/2) -1){
|
||||
a.push(s.charAt(i));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (s.charAt(i) != a.pop()){
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// extract the k element from a Queue Q. k should be >= 0
|
||||
public static Integer extract (Queue<Integer> Q, int k) throws NotEnoughElements {
|
||||
|
||||
if (Q.size() <= k) {
|
||||
throw new NotEnoughElements();
|
||||
} else {
|
||||
Integer t = null ;
|
||||
|
||||
for (int i = 0 ;i< Q.size();i++){
|
||||
if (i == k){
|
||||
t = Q.front();
|
||||
}
|
||||
Q.enqueue(Q.dequeue());
|
||||
}
|
||||
|
||||
return t;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// invert a Queue without recursive methods
|
||||
public static <E> void reverse (Queue<E> Q){
|
||||
|
||||
NodeQueue<E> newq = new NodeQueue<E>();
|
||||
|
||||
for (;Q.size()> 0;){
|
||||
|
||||
for (int i = 0;i< Q.size() - 1;i++){
|
||||
Q.enqueue(Q.dequeue());
|
||||
}
|
||||
newq.enqueue(Q.dequeue());
|
||||
}
|
||||
|
||||
int size = newq.size();
|
||||
for (int i = 0;i<size;i++){
|
||||
Q.enqueue(newq.dequeue());
|
||||
}
|
||||
}
|
||||
|
||||
// invert a Queue with recursion
|
||||
public static <E> void recReverse (Queue<E> Q){
|
||||
if (Q.size()>1){
|
||||
E t = Q.dequeue();
|
||||
recReverse(Q);
|
||||
Q.enqueue(t);
|
||||
}
|
||||
}
|
||||
|
||||
// search element x in Queue Q . WARNING THIS FUNCTION ALTER THE ORIGINAL QUEUE Q
|
||||
public static <E> boolean isContained (Queue<E>Q,E x){
|
||||
if (Q.size() > 0){
|
||||
E t = Q.dequeue();
|
||||
if (t==x){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return isContained(Q,x);
|
||||
}
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
21
exceptions/NotEnoughElements.java
Normal file
21
exceptions/NotEnoughElements.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package exceptions;
|
||||
/**
|
||||
* Thrown when a position is determined to be invalid.
|
||||
* @author Roberto Tamassia, Michael Goodrich
|
||||
*/
|
||||
//Copyright (c) 2003 Brown University, Providence, RI
|
||||
//Additional modifications and methods by xgiovio
|
||||
|
||||
|
||||
public class NotEnoughElements extends RuntimeException {
|
||||
public NotEnoughElements(String err) {
|
||||
super(err);
|
||||
}
|
||||
|
||||
public NotEnoughElements() {
|
||||
super("NotEnoughElements");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user