Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc8ee96d

Browse files
author
asri71
committed
Adding Linked List based General queue implementation
1 parented53bd0 commitc8ee96d

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+
packagesrc.main.java.com.dataStructures;
2+
3+
importsrc.main.java.com.types.Queue;
4+
5+
importjava.util.Iterator;
6+
importjava.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+
publicclassGeneralQueue<T>implementsQueue<T> {
14+
15+
privateLinkedList<T>queue;
16+
privateIterator<T>itr;
17+
18+
//Overloaded constructor to create queue of specific size
19+
publicGeneralQueue() {
20+
queue =newLinkedList<>();
21+
}
22+
23+
@Override
24+
publicbooleanadd(Tt) {
25+
26+
if(queue ==null) {
27+
thrownewNullPointerException();
28+
}
29+
30+
queue.add(t);
31+
returntrue;
32+
}
33+
34+
@Override
35+
publicbooleanremove(Tt) {
36+
if(null ==queue ||queue.size() ==0){
37+
thrownewNullPointerException();
38+
}
39+
queue.remove(t);
40+
returntrue;
41+
}
42+
43+
@Override
44+
publicbooleanisEmpty() {
45+
46+
if(null ==queue ||queue.size() ==0){
47+
returntrue;
48+
}
49+
50+
returnfalse;
51+
}
52+
53+
@Override
54+
publicIterator<T>iterator() {
55+
56+
if(queue ==null) {
57+
returnnull;
58+
}
59+
itr =queue.iterator();
60+
returnitr;
61+
}
62+
63+
@Override
64+
publicbooleanoffer(Tt) {
65+
if(null ==queue) {
66+
returnfalse;
67+
}
68+
69+
queue.add(t);
70+
returntrue;
71+
}
72+
73+
@Override
74+
publicTpoll() {
75+
76+
if(queue ==null ||queue.isEmpty()){
77+
returnnull;
78+
}
79+
80+
returnqueue.pollFirst();
81+
}
82+
83+
@Override
84+
publicTelement() {
85+
86+
if(queue ==null ||queue.isEmpty()) {
87+
thrownewNullPointerException();
88+
}
89+
90+
returnqueue.peekFirst();
91+
}
92+
93+
@Override
94+
publicTpeek() {
95+
if(null ==queue ||queue.size() ==0){
96+
returnnull;
97+
}
98+
99+
returnqueue.peekFirst();
100+
}
101+
102+
@Override
103+
publicbooleanhasNext() {
104+
105+
if(itr.hasNext()){
106+
returntrue;
107+
}
108+
returnfalse;
109+
}
110+
111+
@Override
112+
publicTnext() {
113+
114+
returnitr.next();
115+
}
116+
117+
@Override
118+
publicObject[]toArray() {
119+
120+
Object[]elements = {};
121+
if(null ==queue ||queue.isEmpty()){
122+
returnelements;
123+
}
124+
elements =newObject[queue.size()];
125+
for(inti=0;i<queue.size();i++){
126+
elements[i] =queue.get(i);
127+
}
128+
129+
returnelements;
130+
}
131+
132+
@Override
133+
publicintsize() {
134+
if(null ==queue ||queue.isEmpty()) {
135+
return0;
136+
}
137+
138+
returnqueue.size();
139+
}
140+
141+
142+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagesrc.main.java.com.types;
2+
3+
importjava.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+
publicinterfaceDataStructure<T>extendsIterator<T> {
12+
13+
//Method to add element in the structure
14+
publicbooleanadd(Tt);
15+
16+
//Method to remove the given object from structure
17+
publicbooleanremove(To);
18+
19+
//Method to get Iterator to parse on the given structure
20+
publicIterator<T>iterator();
21+
22+
//Method to check if structure is empty
23+
publicbooleanisEmpty();
24+
25+
//Method to get all the elements of data structure in array
26+
publicObject[]toArray();
27+
28+
//Method to get the size or number of elements in structure
29+
publicintsize();
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+
packagesrc.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+
publicinterfaceQueue<T>extendsDataStructure<T> {
11+
12+
//Method to add element
13+
publicbooleanoffer(Tt);
14+
15+
//Method to remove element
16+
publicTpoll();
17+
18+
//Method to check element on head
19+
publicTpeek();
20+
21+
//Method to check element on head. This throws exception on runtime if the queue is empty
22+
publicTelement();
23+
24+
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packagesrc.test.java.com.dataStructures;
2+
3+
4+
importorg.junit.Assert;
5+
importorg.junit.Test;
6+
importsrc.main.java.com.dataStructures.GeneralQueue;
7+
importsrc.main.java.com.types.Queue;
8+
9+
publicclassGeneralQueueTest {
10+
11+
@Test
12+
publicvoidtestGeneralQueue() {
13+
14+
Queue<Integer>myQueue =newGeneralQueue<>();
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+
BooleanisEmpty =myQueue.isEmpty();
29+
Assert.assertEquals(Boolean.valueOf("false"),Boolean.valueOf(isEmpty));
30+
31+
myQueue.offer(60);
32+
Assert.assertEquals(5,myQueue.size());
33+
34+
intpolledElement =myQueue.poll();
35+
Assert.assertEquals(10,polledElement);
36+
37+
intelement =myQueue.element();
38+
Assert.assertEquals(30,element);
39+
40+
myQueue.poll();
41+
intpeekedElement =myQueue.peek();
42+
Assert.assertEquals(40,peekedElement);
43+
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp