File tree 1 file changed +13
-5
lines changed
src/main/java/com/dataStructures
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change 4
4
5
5
import java .util .Iterator ;
6
6
import java .util .LinkedList ;
7
+ import java .util .NoSuchElementException ;
7
8
8
9
/**
9
10
* linkedList based implementation of queue.
@@ -24,18 +25,23 @@ public GeneralQueue() {
24
25
public boolean add (T t ) {
25
26
26
27
if (queue == null ) {
27
- throw new NullPointerException ();
28
+ throw new IllegalStateException ();
29
+ }
30
+ if (t == null ){
31
+ throw new NullPointerException ();
28
32
}
29
-
30
33
queue .add (t );
31
34
return true ;
32
35
}
33
36
34
37
@ Override
35
38
public boolean remove (T t ) {
36
- if (null == queue || queue . size () == 0 ){
39
+ if (null == queue ){
37
40
throw new NullPointerException ();
38
41
}
42
+ if (queue .isEmpty ()) {
43
+ throw new NoSuchElementException ();
44
+ }
39
45
queue .remove (t );
40
46
return true ;
41
47
}
@@ -65,7 +71,9 @@ public boolean offer(T t) {
65
71
if (null == queue ) {
66
72
return false ;
67
73
}
68
-
74
+ if (t == null ){
75
+ throw new NullPointerException ();
76
+ }
69
77
queue .add (t );
70
78
return true ;
71
79
}
@@ -84,7 +92,7 @@ public T poll() {
84
92
public T element () {
85
93
86
94
if (queue == null || queue .isEmpty ()) {
87
- throw new NullPointerException ();
95
+ throw new NoSuchElementException ();
88
96
}
89
97
90
98
return queue .peekFirst ();
You can’t perform that action at this time.
0 commit comments