forked from kionkina/JDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DequeTester.java
76 lines (66 loc) · 2.43 KB
/
DequeTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Tiffany Moi
//APCS2 pd 5
//HW29 -- Stress is the Best
//2017-04-05
import java.util.NoSuchElementException;
import java.util.ArrayList;
public class DequeTester {
public static void main( String[] args ) {
// Test using deque of Strings
System.out.println( "\nTesting deque of Strings" );
DLLDeque<String> stringDeque = new DLLDeque<String>(); //error if generic typing is not allowed
System.out.println( stringDeque );
System.out.println( "\nAdding elements..." );
// a, b, c should be added to the deque
stringDeque.addLast( "b" );
stringDeque.addFirst( "a" );
stringDeque.addLast( "c" );
System.out.println( stringDeque ); // a b c
System.out.println( "\nRemoving elements..." );
// a and c should be removed from the deque
stringDeque.removeFirst();
stringDeque.removeLast();
System.out.println( stringDeque ); // b
System.out.println( "\nRemoving remaining element..." );
// b should be removed from the deque
stringDeque.removeFirst();
System.out.println( stringDeque ); // empty deque
System.out.println( "\nRemvoing element..." );
// should print exception if exceptions are handled
try {
stringDeque.removeFirst(); // error
} catch (NoSuchElementException e) {
e.printStackTrace(); //prints error message
}
System.out.println( "\nGetting first element..." );
// should print exception if exceptions are handled
try {
stringDeque.getFirst(); //error
} catch (NoSuchElementException e) {
e.printStackTrace(); //prints error message
}
System.out.println( "\nAdding element..." );
// a should be added to the deque
stringDeque.addFirst( "a" );
System.out.println( "\nGetting first element..." );
// a should be printed
System.out.println( stringDeque.getFirst() + "\n" ); // a
// Test using deque of Objects
System.out.println( "\nTesting deque of Objects" );
DLLDeque mixDeque = new DLLDeque();
System.out.println( "\nAdding elements..." );
// elements should be added to the deque
mixDeque.addFirst( 1 );
mixDeque.addLast( "a" );
mixDeque.addFirst( new ArrayList<Integer>() );
// elements should be properly printed in order
System.out.println( mixDeque );
System.out.println( "\nAdding 64 to the ArrayList at front" );
// first element should be removed
ArrayList a = (ArrayList)mixDeque.removeFirst(); //pop the ArrayList
a.add( 64 ); //add element 64
// ArrayList containing 64 should be added to the deque
mixDeque.addFirst( a );
System.out.println( mixDeque ); // [64] 1 a
}
}