File tree Expand file tree Collapse file tree 5 files changed +151
-0
lines changed
Expand file tree Collapse file tree 5 files changed +151
-0
lines changed Original file line number Diff line number Diff line change 11* .o
2+ * .class
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+
3+ class EmptyQueueException extends IndexOutOfBoundsException {
4+ public EmptyQueueException () {
5+ super ("Queue is empty." );
6+ }
7+ }
8+
9+ class Queue <T > {
10+ private ArrayList <T > list ;
11+
12+ public Queue () {
13+ this .list = new ArrayList <T >();
14+ }
15+
16+ public void enqueue (T item ) {
17+ this .list .add (item );
18+ }
19+
20+ public T dequeue () {
21+ if (this .size () == 0 ) {
22+ throw new EmptyQueueException ();
23+ }
24+
25+ return this .list .remove (0 );
26+ }
27+
28+ public T peek () {
29+ if (this .size () == 0 ) {
30+ throw new EmptyQueueException ();
31+ }
32+
33+ return this .list .get (0 );
34+ }
35+
36+ public boolean isEmpty () {
37+ return this .size () == 0 ;
38+ }
39+
40+ public int size () {
41+ return this .list .size ();
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ class QueueTester {
2+ public static void main (String [] args ) {
3+ Queue <Integer > queue = new Queue <Integer >();
4+
5+ for (int i = 5 ; i >= 0 ; i --) {
6+ queue .enqueue (i );
7+ System .out .println ("Enqueued " + i );
8+ }
9+
10+ System .out .println ("Size: " + queue .size ());
11+ System .out .println ("Top: " + queue .peek ());
12+
13+ while (queue .size () > 0 ) {
14+ System .out .println (queue .dequeue ());
15+ }
16+
17+ System .out .println ("Size: " + queue .size ());
18+ System .out .print ("Top: " );
19+
20+ try {
21+ System .out .println (queue .peek ());
22+ } catch (EmptyQueueException e ) {
23+ System .out .println (e .toString ());
24+ }
25+
26+ try {
27+ System .out .println (queue .dequeue ());
28+ } catch (EmptyQueueException e ) {
29+ System .out .println (e .toString ());
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+
3+ class EmptyStackException extends IndexOutOfBoundsException {
4+ public EmptyStackException () {
5+ super ("Stack is empty." );
6+ }
7+ }
8+
9+ class Stack <T > {
10+ private ArrayList <T > list ;
11+
12+ public Stack () {
13+ this .list = new ArrayList <T >();
14+ }
15+
16+ public void push (T item ) {
17+ this .list .add (item );
18+ }
19+
20+ public T pop () {
21+ if (this .size () == 0 ) {
22+ throw new EmptyStackException ();
23+ }
24+
25+ return this .list .remove (this .size () - 1 );
26+ }
27+
28+ public T peek () {
29+ if (this .size () == 0 ) {
30+ throw new EmptyStackException ();
31+ }
32+
33+ return this .list .get (0 );
34+ }
35+
36+ public boolean isEmpty () {
37+ return this .size () == 0 ;
38+ }
39+
40+ public int size () {
41+ return this .list .size ();
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ class StackTester {
2+ public static void main (String [] args ) {
3+ Stack <Integer > stack = new Stack <Integer >();
4+
5+ for (int i = 5 ; i >= 0 ; i --) {
6+ stack .push (i );
7+ System .out .println ("Pushed " + i );
8+ }
9+
10+ System .out .println ("Size: " + stack .size ());
11+ System .out .println ("Top: " + stack .peek ());
12+
13+ while (stack .size () > 0 ) {
14+ System .out .println (stack .pop ());
15+ }
16+
17+ System .out .println ("Size: " + stack .size ());
18+ System .out .print ("Top: " );
19+
20+ try {
21+ System .out .println (stack .peek ());
22+ } catch (EmptyStackException e ) {
23+ System .out .println (e .toString ());
24+ }
25+
26+ try {
27+ System .out .println (stack .pop ());
28+ } catch (EmptyStackException e ) {
29+ System .out .println (e .toString ());
30+ }
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments