Skip to content

Commit 37b99ff

Browse files
authored
Update GeneralQueue.java
1 parent 5ce337f commit 37b99ff

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

src/main/java/com/dataStructures/GeneralQueue.java

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,40 @@
99
/**
1010
* linkedList based implementation of queue.
1111
* This implementation is not thread safe and need exclusive thread safety measures from the client.
12+
*
1213
* @param <T>
1314
*/
1415
public class GeneralQueue<T> implements Queue<T> {
1516

1617
private LinkedList<T> queue;
1718
private Iterator<T> itr;
1819

19-
//Overloaded constructor to create queue of specific size
20+
/**
21+
* Overloaded constructor to create queue of specific size
22+
*/
2023
public GeneralQueue() {
2124
queue = new LinkedList<>();
2225
}
2326

2427
@Override
2528
public boolean add(T t) {
2629

27-
if(queue == null) {
30+
if (queue == null) {
2831
throw new IllegalStateException();
2932
}
30-
if(t == null){
31-
throw new NullPointerException();
33+
if (t == null) {
34+
throw new NullPointerException();
3235
}
3336
queue.add(t);
3437
return true;
3538
}
3639

3740
@Override
3841
public boolean remove(T t) {
39-
if(null == queue){
42+
if (null == queue) {
4043
throw new NullPointerException();
4144
}
42-
if(queue.isEmpty()) {
45+
if (queue.isEmpty()) {
4346
throw new NoSuchElementException();
4447
}
4548
queue.remove(t);
@@ -48,18 +51,12 @@ public boolean remove(T t) {
4851

4952
@Override
5053
public boolean isEmpty() {
51-
52-
if(null == queue || queue.size() == 0){
53-
return true;
54-
}
55-
56-
return false;
54+
return null == queue || queue.size() == 0;
5755
}
5856

5957
@Override
6058
public Iterator<T> iterator() {
61-
62-
if(queue == null) {
59+
if (queue == null) {
6360
return null;
6461
}
6562
itr = queue.iterator();
@@ -68,10 +65,10 @@ public Iterator<T> iterator() {
6865

6966
@Override
7067
public boolean offer(T t) {
71-
if(null == queue) {
68+
if (null == queue) {
7269
return false;
7370
}
74-
if(t == null){
71+
if (t == null) {
7572
throw new NullPointerException();
7673
}
7774
queue.add(t);
@@ -80,18 +77,16 @@ public boolean offer(T t) {
8077

8178
@Override
8279
public T poll() {
83-
84-
if(queue == null || queue.isEmpty()){
85-
return null;
80+
if (queue == null || queue.isEmpty()) {
81+
return null;
8682
}
8783

8884
return queue.pollFirst();
8985
}
9086

9187
@Override
9288
public T element() {
93-
94-
if(queue == null || queue.isEmpty()) {
89+
if (queue == null || queue.isEmpty()) {
9590
throw new NoSuchElementException();
9691
}
9792

@@ -100,37 +95,31 @@ public T element() {
10095

10196
@Override
10297
public T peek() {
103-
if(null == queue || queue.size() == 0){
104-
return null;
98+
if (null == queue || queue.size() == 0) {
99+
return null;
105100
}
106101

107102
return queue.peekFirst();
108103
}
109104

110105
@Override
111106
public boolean hasNext() {
112-
113-
if(itr.hasNext()){
114-
return true;
115-
}
116-
return false;
107+
return itr.hasNext();
117108
}
118109

119110
@Override
120111
public T next() {
121-
122112
return itr.next();
123113
}
124114

125115
@Override
126116
public Object[] toArray() {
127-
128117
Object[] elements = {};
129-
if(null == queue || queue.isEmpty()){
118+
if (null == queue || queue.isEmpty()) {
130119
return elements;
131120
}
132121
elements = new Object[queue.size()];
133-
for(int i=0;i<queue.size();i++){
122+
for (int i = 0; i < queue.size(); i++) {
134123
elements[i] = queue.get(i);
135124
}
136125

@@ -139,12 +128,9 @@ public Object[] toArray() {
139128

140129
@Override
141130
public int size() {
142-
if(null == queue || queue.isEmpty()) {
131+
if (null == queue || queue.isEmpty()) {
143132
return 0;
144133
}
145-
146134
return queue.size();
147135
}
148-
149-
150136
}

0 commit comments

Comments
 (0)