Skip to content

Commit c8ee96d

Browse files
author
asri71
committed
Adding Linked List based General queue implementation
1 parent ed53bd0 commit c8ee96d

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package src.main.java.com.dataStructures;
2+
3+
import src.main.java.com.types.Queue;
4+
5+
import java.util.Iterator;
6+
import java.util.LinkedList;
7+
8+
/**
9+
* linkedList based implementation of queue.
10+
* This implementation is not thread safe and need exclusive thread safety measures from the client.
11+
* @param <T>
12+
*/
13+
public class GeneralQueue<T> implements Queue<T> {
14+
15+
private LinkedList<T> queue;
16+
private Iterator<T> itr;
17+
18+
//Overloaded constructor to create queue of specific size
19+
public GeneralQueue() {
20+
queue = new LinkedList<>();
21+
}
22+
23+
@Override
24+
public boolean add(T t) {
25+
26+
if(queue == null) {
27+
throw new NullPointerException();
28+
}
29+
30+
queue.add(t);
31+
return true;
32+
}
33+
34+
@Override
35+
public boolean remove(T t) {
36+
if(null == queue || queue.size() == 0){
37+
throw new NullPointerException();
38+
}
39+
queue.remove(t);
40+
return true;
41+
}
42+
43+
@Override
44+
public boolean isEmpty() {
45+
46+
if(null == queue || queue.size() == 0){
47+
return true;
48+
}
49+
50+
return false;
51+
}
52+
53+
@Override
54+
public Iterator<T> iterator() {
55+
56+
if(queue == null) {
57+
return null;
58+
}
59+
itr = queue.iterator();
60+
return itr;
61+
}
62+
63+
@Override
64+
public boolean offer(T t) {
65+
if(null == queue) {
66+
return false;
67+
}
68+
69+
queue.add(t);
70+
return true;
71+
}
72+
73+
@Override
74+
public T poll() {
75+
76+
if(queue == null || queue.isEmpty()){
77+
return null;
78+
}
79+
80+
return queue.pollFirst();
81+
}
82+
83+
@Override
84+
public T element() {
85+
86+
if(queue == null || queue.isEmpty()) {
87+
throw new NullPointerException();
88+
}
89+
90+
return queue.peekFirst();
91+
}
92+
93+
@Override
94+
public T peek() {
95+
if(null == queue || queue.size() == 0){
96+
return null;
97+
}
98+
99+
return queue.peekFirst();
100+
}
101+
102+
@Override
103+
public boolean hasNext() {
104+
105+
if(itr.hasNext()){
106+
return true;
107+
}
108+
return false;
109+
}
110+
111+
@Override
112+
public T next() {
113+
114+
return itr.next();
115+
}
116+
117+
@Override
118+
public Object[] toArray() {
119+
120+
Object[] elements = {};
121+
if(null == queue || queue.isEmpty()){
122+
return elements;
123+
}
124+
elements = new Object[queue.size()];
125+
for(int i=0;i<queue.size();i++){
126+
elements[i] = queue.get(i);
127+
}
128+
129+
return elements;
130+
}
131+
132+
@Override
133+
public int size() {
134+
if(null == queue || queue.isEmpty()) {
135+
return 0;
136+
}
137+
138+
return queue.size();
139+
}
140+
141+
142+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package src.main.java.com.types;
2+
3+
import java.util.Iterator;
4+
5+
/**
6+
* This interface is to define bacis functionality expected out of any implementation class
7+
* Since this is a data structure it should have the flexibility to contain any kind of object hence it has been made generic
8+
* Any implementation class need not to be thread safe or it could be depending on the implementation class how does it want to behave.
9+
* @param <T>
10+
*/
11+
public interface DataStructure<T> extends Iterator<T> {
12+
13+
//Method to add element in the structure
14+
public boolean add(T t);
15+
16+
//Method to remove the given object from structure
17+
public boolean remove(T o);
18+
19+
//Method to get Iterator to parse on the given structure
20+
public Iterator<T> iterator();
21+
22+
//Method to check if structure is empty
23+
public boolean isEmpty();
24+
25+
//Method to get all the elements of data structure in array
26+
public Object[] toArray();
27+
28+
//Method to get the size or number of elements in structure
29+
public int size();
30+
31+
}

src/main/java/com/types/Queue.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package src.main.java.com.types;
2+
3+
4+
/**
5+
* Interface to provide queue specific functionality to the implementing class
6+
* This interface only defines the functionality which the queue implementing classes require.
7+
* Any class having queue behaviour should implement this interface and override all of its methods
8+
* @param <T>
9+
*/
10+
public interface Queue<T> extends DataStructure<T> {
11+
12+
//Method to add element
13+
public boolean offer(T t);
14+
15+
//Method to remove element
16+
public T poll();
17+
18+
//Method to check element on head
19+
public T peek();
20+
21+
//Method to check element on head. This throws exception on runtime if the queue is empty
22+
public T element();
23+
24+
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package src.test.java.com.dataStructures;
2+
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import src.main.java.com.dataStructures.GeneralQueue;
7+
import src.main.java.com.types.Queue;
8+
9+
public class GeneralQueueTest {
10+
11+
@Test
12+
public void testGeneralQueue() {
13+
14+
Queue<Integer> myQueue = new GeneralQueue<>();
15+
myQueue.add(10);
16+
myQueue.add(20);
17+
myQueue.add(30);
18+
myQueue.add(40);
19+
myQueue.add(50);
20+
21+
22+
Object[] myArray = myQueue.toArray();
23+
Assert.assertEquals(myArray.length, myQueue.size());
24+
25+
myQueue.remove(20);
26+
Assert.assertEquals(myQueue.size(), 4);
27+
28+
Boolean isEmpty = myQueue.isEmpty();
29+
Assert.assertEquals(Boolean.valueOf("false"), Boolean.valueOf(isEmpty));
30+
31+
myQueue.offer(60);
32+
Assert.assertEquals(5, myQueue.size());
33+
34+
int polledElement = myQueue.poll();
35+
Assert.assertEquals(10, polledElement);
36+
37+
int element = myQueue.element();
38+
Assert.assertEquals(30, element);
39+
40+
myQueue.poll();
41+
int peekedElement = myQueue.peek();
42+
Assert.assertEquals(40, peekedElement);
43+
44+
}
45+
}

0 commit comments

Comments
 (0)