java ( 7978 ) - java ( 7979 ) stack: com.thealgorithms.datastructures.queues.Deque.main(Deque.java:176)
package com.thealgorithms.datastructures.queues;
import java.util.NoSuchElementException;
/**
* A [deque](https://en.wikipedia.org/wiki/Double-ended_queue) is short for a
* double ended queue pronounced "deck" and sometimes referred to as a head-tail
* linked list. A deque is a data structure based on a doubly linked list, but
* only supports adding and removal of nodes from the beginning and the end of
* the list.
*
* @author [Ian Cowan](https://github.com/iccowan)
*/
public class Deque {
/**
* Node for the deque
*/
private static class DequeNode {
S val;
DequeNode next = null;
DequeNode prev = null;
DequeNode(S val) {
this.val = val;
}
}
private DequeNode head = null;
private DequeNode tail = null;
private int size = 0;
/**
* Adds the specified value to the head of the deque
*
* @param val Value to add to the deque
*/
public void addFirst(T val) {
DequeNode newNode = new DequeNode<>(val);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
size++;
}
/**
* Adds the specified value to the tail of the deque
*
* @param val Value to add to the deque
*/
public void addLast(T val) {
DequeNode newNode = new DequeNode<>(val);
if (tail == null) {
head = newNode;
tail = newNode;
} else {
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
size++;
}
/**
* Removes and returns the first (head) value in the deque
*
* @return the value of the head of the deque
* @throws NoSuchElementException if the deque is empty
*/
public T pollFirst() {
if (head == null) {
throw new NoSuchElementException("Deque is empty");
}
T oldHeadVal = head.val;
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
head.prev = null;
}
size--;
return oldHeadVal;
}
/**
* Removes and returns the last (tail) value in the deque
*
* @return the value of the tail of the deque
* @throws NoSuchElementException if the deque is empty
*/
public T pollLast() {
if (tail == null) {
throw new NoSuchElementException("Deque is empty");
}
T oldTailVal = tail.val;
if (head == tail) {
head = null;
tail = null;
} else {
tail = tail.prev;
tail.next = null;
}
size--;
return oldTailVal;
}
/**
* Returns the first (head) value of the deque WITHOUT removing
*
* @return the value of the head of the deque, or null if empty
*/
public T peekFirst() {
return head != null ? head.val : null;
}
/**
* Returns the last (tail) value of the deque WITHOUT removing
*
* @return the value of the tail of the deque, or null if empty
*/
public T peekLast() {
return tail != null ? tail.val : null;
}
/**
* Returns the size of the deque
*
* @return the size of the deque
*/
public int size() {
return size;
}
/**
* Returns whether or not the deque is empty
*
* @return whether or not the deque is empty
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Returns a stringified deque in a pretty form:
*
*
* Head -> 1 <-> 2 <-> 3 <- Tail
*
* @return the stringified deque
*/
@Override
public String toString() {
StringBuilder dequeString = new StringBuilder("Head -> ");
DequeNode currNode = head;
while (currNode != null) {
dequeString.append(currNode.val);
if (currNode.next != null) {
dequeString.append(" <-> ");
}
currNode = currNode.next;
}
dequeString.append(" <- Tail");
return dequeString.toString();
}
public static void main(String[] args) {
Deque myDeque = new Deque<>();
for (int i = 0; i < 42; i++) {
if (i / 42.0 < 0.5) {
myDeque.addFirst(i);
} else {
myDeque.addLast(i);
}
}
System.out.println(myDeque);
System.out.println("Size: " + myDeque.size());
System.out.println();
myDeque.pollFirst();
myDeque.pollFirst();
myDeque.pollLast();
System.out.println(myDeque);
System.out.println("Size: " + myDeque.size());
System.out.println();
int dequeSize = myDeque.size();
for (int i = 0; i < dequeSize; i++) {
int removing = -1;
if (i / 39.0 < 0.5) {
removing = myDeque.pollFirst();
} else {
removing = myDeque.pollLast();
}
System.out.println("Removing: " + removing);
}
System.out.println(myDeque);
System.out.println(myDeque.size());
}
}
| No. | From | Name | Value |
|---|---|---|---|
| 1 | 176 | args | [Ljava.lang.String;@7852e922 |
| END | 0 | 0 | 0 |
| Process Filter | Thread Filter |
|---|---|
| 7978 java | 7979 java |
| No. | PN | PID | TID | TN | Message |
|---|---|---|---|---|---|
| 1 | java | 7978 | 7979 | java | Head -> 20 <-> 19 <-> 18 <-> 17 <-> 16 <-> 15 <-> 14 <-> 13 <-> 12 <-> 11 <-> 10 <-> 9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1 <-> 0 <-> 21 <-> 22 <-> 23 <-> 24 <-> 25 <-> 26 <-> 27 <-> 28 <-> 29 <-> 30 <-> 31 <-> 32 <-> 33 <-> 34 <-> 35 <-> 36 <-> 37 <-> 38 <-> 39 <-> 40 <-> 41 <- Tail |
| 2 | java | 7978 | 7979 | java | Size: 42 |
| 3 | java | 7978 | 7979 | java | Head -> 18 <-> 17 <-> 16 <-> 15 <-> 14 <-> 13 <-> 12 <-> 11 <-> 10 <-> 9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1 <-> 0 <-> 21 <-> 22 <-> 23 <-> 24 <-> 25 <-> 26 <-> 27 <-> 28 <-> 29 <-> 30 <-> 31 <-> 32 <-> 33 <-> 34 <-> 35 <-> 36 <-> 37 <-> 38 <-> 39 <-> 40 <- Tail |
| 4 | java | 7978 | 7979 | java | Size: 39 |
| 5 | java | 7978 | 7979 | java | Removing: 18 |
| 6 | java | 7978 | 7979 | java | Removing: 17 |
| 7 | java | 7978 | 7979 | java | Removing: 16 |
| 8 | java | 7978 | 7979 | java | Removing: 15 |
| 9 | java | 7978 | 7979 | java | Removing: 14 |
| 10 | java | 7978 | 7979 | java | Removing: 13 |
| 11 | java | 7978 | 7979 | java | Removing: 12 |
| 12 | java | 7978 | 7979 | java | Removing: 11 |
| 13 | java | 7978 | 7979 | java | Removing: 10 |
| 14 | java | 7978 | 7979 | java | Removing: 9 |
| 15 | java | 7978 | 7979 | java | Removing: 8 |
| 16 | java | 7978 | 7979 | java | Removing: 7 |
| 17 | java | 7978 | 7979 | java | Removing: 6 |
| 18 | java | 7978 | 7979 | java | Removing: 5 |
| 19 | java | 7978 | 7979 | java | Removing: 4 |
| 20 | java | 7978 | 7979 | java | Removing: 3 |
| 21 | java | 7978 | 7979 | java | Removing: 2 |
| 22 | java | 7978 | 7979 | java | Removing: 1 |
| 23 | java | 7978 | 7979 | java | Removing: 0 |
| 24 | java | 7978 | 7979 | java | Removing: 21 |
| 25 | java | 7978 | 7979 | java | Removing: 40 |
| 26 | java | 7978 | 7979 | java | Removing: 39 |
| 27 | java | 7978 | 7979 | java | Removing: 38 |
| 28 | java | 7978 | 7979 | java | Removing: 37 |
| 29 | java | 7978 | 7979 | java | Removing: 36 |
| 30 | java | 7978 | 7979 | java | Removing: 35 |
| 31 | java | 7978 | 7979 | java | Removing: 34 |
| 32 | java | 7978 | 7979 | java | Removing: 33 |
| 33 | java | 7978 | 7979 | java | Removing: 32 |
| 34 | java | 7978 | 7979 | java | Removing: 31 |
| 35 | java | 7978 | 7979 | java | Removing: 30 |
| 36 | java | 7978 | 7979 | java | Removing: 29 |
| 37 | java | 7978 | 7979 | java | Removing: 28 |
| 38 | java | 7978 | 7979 | java | Removing: 27 |
| 39 | java | 7978 | 7979 | java | Removing: 26 |
| 40 | java | 7978 | 7979 | java | Removing: 25 |
| 41 | java | 7978 | 7979 | java | Removing: 24 |
| 42 | java | 7978 | 7979 | java | Removing: 23 |
| 43 | java | 7978 | 7979 | java | Removing: 22 |
| 44 | java | 7978 | 7979 | java | Head -> <- Tail |
| END | 0 | 0 | 0 | 0 | 0 |
Functions and Shortcuts
| No. | Function | Shortcuts | Description |
|---|---|---|---|
| 1 | GB | Alt + LEFT, Alt + A | Go Backward |
| 2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
| 3 | PPE | Alt + UP, Alt + W | Previous Process End |
| 4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
| 5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
| 6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
| 7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
| 8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
| 9 | TB | LEFT, A | current Thread Backward |
| 10 | TF | RIGHT, D | current Thread Foreward |
| 11 | LU | UP, W | go Line Up of current code block in current thread |
| 12 | LD | DOWN, S | go Line Down of current code block in current thread |
| 13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
| 14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
| 15 | BS | Home | go to code Block Start |
| 16 | BE | End | go to code Block End |
| Project: | Alg-Java |
| Update: | 20240824 |
| Commit: | a7cd97d7 |
| Source Code: | datastructures.queues.Deque |
| BuildTool: | Java17 |
| Compiler: | Java17 |
| Runtime: | Openjdk17 |
| System: | MySystemD |
| Kernel: | Linux5.10.211 |
| Cpu: | Intel:Corei7-7700K |
| Machine: | AwesomeMachine |