From b22cd894c1aac32fad2f6d3a4644e94029d43c88 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Mon, 14 Apr 2014 01:11:38 +0200 Subject: [PATCH] Implementato alcuni esercizi --- esercizi/QStack.java | 65 ++++++++++ esercizi/SQueue.java | 85 +++++++++++++ esercizi/start.java | 69 +++++++++++ esercizi/static_methods.java | 193 ++++++++++++++++++++++++++++++ exceptions/NotEnoughElements.java | 21 ++++ 5 files changed, 433 insertions(+) create mode 100644 esercizi/QStack.java create mode 100644 esercizi/SQueue.java create mode 100644 esercizi/start.java create mode 100644 esercizi/static_methods.java create mode 100644 exceptions/NotEnoughElements.java diff --git a/esercizi/QStack.java b/esercizi/QStack.java new file mode 100644 index 0000000..fb82e3a --- /dev/null +++ b/esercizi/QStack.java @@ -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 implements Stack { + + NodeQueue a = new NodeQueue(); + + @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 implements Queue { + + + private ArrayStack accoda = new ArrayStack(); + private ArrayStack decoda = new ArrayStack(); + + @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 a = new SQueue(); + 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 b = new QStack(); + 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); + + + + + + + + + + + + } + +} diff --git a/esercizi/static_methods.java b/esercizi/static_methods.java new file mode 100644 index 0000000..6f442f5 --- /dev/null +++ b/esercizi/static_methods.java @@ -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 a = new ArrayStack(); + 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 a = new ArrayStack(); + + 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 a = new ArrayStack(); + + 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 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 void reverse (Queue Q){ + + NodeQueue newq = new NodeQueue(); + + 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 void recReverse (Queue 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 boolean isContained (QueueQ,E x){ + if (Q.size() > 0){ + E t = Q.dequeue(); + if (t==x){ + return true; + } + else{ + return isContained(Q,x); + } + }else { + return false; + } + } + + +} diff --git a/exceptions/NotEnoughElements.java b/exceptions/NotEnoughElements.java new file mode 100644 index 0000000..e028944 --- /dev/null +++ b/exceptions/NotEnoughElements.java @@ -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"); + + } + +} +