Reputation: 667
in C++ all I had to do was
#include <queue> -> including
queue<int> a; -> defining
a.push(1); ->using
but in java I found very difficult to use simple deque what should I do...? more specifically, How should I code to simply do the same steps as I did in C++; including, defining, using.
even more specifically, I want to make a deque so that I can add any integer in the deque at front or back. and print whole numbers in that deque by the size of the deque
Upvotes: 5
Views: 2824
Reputation: 128849
Java has both Queue and Deque types, and a LinkedList, among others, can act as either one:
import java.util.*;
Deque<Integer> q = new LinkedList<Integer>();
q.push(1);
Upvotes: 9
Reputation: 304
The current answers suggest that Java's java.util.LinkedList is the Java translation of C++'s std::deque. While LinkedList
does have an interface that is roughly equivalent to that of std::deque
, it does not provide the complexity guarantees that std::deque
does. In particular, std::deque
guarantees O(1)
lookup by index (random access), while LinkedList
has O(n)
lookup. In this sense (the sense in which an experienced C++ user views std::deque
), Java's LinkedList
is nothing at all like std::deque
(though it is very much like std::list
). This thread provides a better answer to the question "What is is the Java equivalent of C++ deque". To sum up, there is no equivalent in the standard Java library.
Upvotes: 10
Reputation: 20061
Look at java.util.LinkedList.
LinkedList<Integer> linkedList = new LinkedList<Integer>();
linkedList.add(5);
linkedList.addFirst(2); // add to front, equivalent to push()
linkedList.addLast(3); // add to end, equivalent to add()
Upvotes: -1