first commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package queue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayListQueue<T> implements MyQueue<T> {
|
||||
|
||||
private List<T> queue = new ArrayList<T>();
|
||||
|
||||
@Override
|
||||
public boolean offer(T item) {
|
||||
return queue.add(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T remove() {
|
||||
return queue.remove(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T peek() {
|
||||
return queue.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return queue.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return queue.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package queue;
|
||||
|
||||
public interface MyQueue<T> {
|
||||
|
||||
/**
|
||||
* Aggiungi l'elemento specificato in fondo alla coda
|
||||
* @param item
|
||||
*/
|
||||
boolean offer(T item);
|
||||
|
||||
/**
|
||||
* Rimuove l'elemento in testa alla coda e lo restituisce
|
||||
* @return
|
||||
*/
|
||||
T remove();
|
||||
|
||||
/**
|
||||
* Restituisce l'elemento in cima alla coda senza rimuoverlo.
|
||||
* @return
|
||||
*/
|
||||
T peek();
|
||||
|
||||
/**
|
||||
* Restituisce l'elemento il numero di elementi correnti nella coda.
|
||||
* @return
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* Restituisce se la coda è vuota o meno.
|
||||
* @return
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package queue;
|
||||
|
||||
public class QueueExample {
|
||||
|
||||
public static void main(String[] Args) {
|
||||
|
||||
MyQueue<Character> queue = new ArrayListQueue<Character>();
|
||||
|
||||
Character a = 'a';
|
||||
Character b = 'b';
|
||||
Character d = 'd';
|
||||
Character c = 'c';
|
||||
|
||||
queue.offer(a);
|
||||
queue.offer(b);
|
||||
queue.offer(c);
|
||||
queue.offer(d);
|
||||
|
||||
for (int i = queue.size(); i > 0; i--) {
|
||||
System.out.println(queue.remove());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user