Skip to content

Commit 68d3b17

Browse files
committed
feat: added deque implementation
1 parent ac99759 commit 68d3b17

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package src.dataStructures.queue;
2+
3+
public class Deque<T> {
4+
5+
private Node<T> first;
6+
private Node<T> last;
7+
private int size;
8+
9+
/**
10+
* Constructor for empty Deque.
11+
*/
12+
public Deque() {
13+
this.size = 0;
14+
this.first = this.last = null;
15+
}
16+
17+
/**
18+
* Check if deque is empty.
19+
*
20+
* @return True if empty.
21+
*/
22+
public boolean isEmpty() {
23+
return this.size == 0;
24+
}
25+
26+
/**
27+
* Get size of deque.
28+
*
29+
* @return Size of the deque.
30+
*/
31+
public int getSize() {
32+
return this.size;
33+
}
34+
35+
/**
36+
* Add element into the back of the deque.
37+
*
38+
* @param val Element to be added.
39+
*/
40+
public void addElement(T val) {
41+
Node<T> newNode = new Node<>(val);
42+
if (this.isEmpty()) {
43+
this.last = this.first = newNode;
44+
size++;
45+
return;
46+
}
47+
Node<T> temp = this.last;
48+
temp.next = newNode;
49+
newNode.prev = temp;
50+
this.last = newNode;
51+
this.size++;
52+
}
53+
54+
/**
55+
* Add a element into the front of the deque.
56+
* @param val Element to be added.
57+
*/
58+
public void addFirst(T val) {
59+
Node<T> newNode = new Node<>(val);
60+
if (this.isEmpty()) {
61+
this.last = this.first = newNode;
62+
size++;
63+
return;
64+
}
65+
Node<T> temp = this.first;
66+
temp.prev = newNode;
67+
newNode.next = temp;
68+
this.first = newNode;
69+
size++;
70+
}
71+
72+
/**
73+
* Peek the first element of the deque.
74+
*
75+
* @return The value in the first node of the deque.
76+
*/
77+
public T peekFirst() {
78+
if (this.isEmpty()) {
79+
return null;
80+
}
81+
return first.val;
82+
}
83+
84+
/**
85+
* Peek the last element of the deque.
86+
*
87+
* @return The value in the last node of the deque.
88+
*/
89+
public T peekLast() {
90+
if (this.isEmpty()) {
91+
return null;
92+
}
93+
return last.val;
94+
}
95+
96+
/**
97+
* Removes and retrieves the first element of the deque.
98+
*
99+
* @return The value in the first node of the deque.
100+
*/
101+
public T pollFirst() {
102+
if (this.isEmpty()) {
103+
return null;
104+
}
105+
Node<T> firstNode = this.first;
106+
Node<T> newFirstNode = this.first.next;
107+
if (newFirstNode != null) {
108+
newFirstNode.prev = null;
109+
}
110+
this.first = newFirstNode;
111+
firstNode.next = null;
112+
this.size--;
113+
return firstNode.val;
114+
}
115+
116+
/**
117+
* Removes and retrieves the last element of the deque.
118+
*
119+
* @return The value in the last node of the deque.
120+
*/
121+
public T pollLast() {
122+
if (this.isEmpty()) {
123+
return null;
124+
}
125+
Node<T> lastNode = this.last;
126+
Node<T> newLastNode = lastNode.prev;
127+
if (newLastNode != null) {
128+
newLastNode.next = null;
129+
}
130+
lastNode.prev = null;
131+
this.last = newLastNode;
132+
this.size--;
133+
return lastNode.val;
134+
}
135+
136+
/**
137+
* Converts Deque into String iteratively.
138+
*
139+
* @return String representation of elements in the deque.
140+
*/
141+
@Override
142+
public String toString() {
143+
if (this.isEmpty()) {
144+
return "[]";
145+
}
146+
String ret = "[";
147+
Node<T> temp = this.first;
148+
while (temp != null) {
149+
ret += " " + temp.toString() + " ";
150+
temp = temp.next;
151+
}
152+
return ret + "]";
153+
}
154+
155+
private static class Node<T> {
156+
private T val;
157+
Node<T> next;
158+
Node<T> prev;
159+
160+
public Node(T val) {
161+
this.val = val;
162+
this.next = this.prev = null;
163+
}
164+
165+
@Override
166+
public String toString() {
167+
return this.val.toString();
168+
}
169+
}
170+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package test.dataStructures.queue;public class dequeTest {
2+
}

0 commit comments

Comments
 (0)