import java.util.Arrays;
import java.util.Stack;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank">Primitive Stack<E></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
/**
* @see Stack#peek()
*
*/
public static void applayMethodPeek(Stack<Integer> buffer) {
applayLoop(buffer);
System.out.println("TOP: " + buffer.peek());
applayLoop(buffer);
}
/**
* @see Stack#pop()
*
*/
public static void applayMethodPop(Stack<Integer> buffer) {
applayLoop(buffer);
System.out.println("TOP: " + buffer.pop());
applayLoop(buffer);
}
/**
* @see Stack#search(Object)
* @see Vector#size()
* @see Vector#get(int)
*
*/
public static void applayMethodSearch(Stack<Integer> buffer, int search) {
applayLoop(buffer);
Integer pos = buffer.search(3);
if (pos == -1)
System.out.println("not found");
else {
System.out.println("position: " + pos);
System.out.println("random access: " + buffer.get(position(buffer.size(), pos)));
}
}
/**
* @see Arrays#sort(Object[])
* @see Stack#add(Object)
*
*/
public static void applayOrderedCrossing(Stack<Integer> buffer) {
applayLoop(buffer);
Object[] source = buffer.toArray();
Arrays.sort(source);
buffer = new Stack<Integer>();
int size = source.length - 1;
for (int i = size; i >= 0; i--)
buffer.add((Integer) source[i]);
applayLoop(buffer);
}
/**
* @see Vector#size()
* @see Vector#get(int)
*
*/
private static void applayLoop(Stack<Integer> buffer) {
int size = buffer.size();
if (buffer.isEmpty())
System.out.print("[]");
else
for (int i = 0; i < size; i++)
System.out.print("[i=" + position(size, i) + ", e=" + buffer.get(i) + "] ");
println();
}
private static void println() {
System.out.println();
}
private static int position(int size, int i) {
return size - i;
}
/**
* @see Stack#push(Object)
*
*/
private static Stack<Integer> createBufferTest() {
return new Stack<Integer>() {
private static final long serialVersionUID = -69L;
{
push(1);
push(2);
push(3); // cima
}
};
}
public static void main(String args[]) throws Exception {
// Unit test - estrazione dell'elemento in cima alla struttura ( SENZA RIMOZIONE
// )
applayMethodPeek(createBufferTest());
// Unit test - estrazione dell'elemento in cima alla struttura ( CON RIMOZIONE )
applayMethodPop(createBufferTest());
// Unit test - ricerca di un elemento nella struttura
applayMethodSearch(createBufferTest(), 3);
// Unit test - attraversamento ordinato della struttura
applayOrderedCrossing(createBufferTest());
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: