5

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

Jeff
  • 12,555
  • 5
  • 33
  • 60
hongtaesuk
  • 557
  • 5
  • 10
  • 19
  • 2
    A more suitable title would have been "What is the Java equivalent of C++ deque" – Murali VP Jul 26 '11 at 06:29
  • What do you mean by "print whole numbers in that deque by the size of the deque"? – Paul Jul 26 '11 at 06:32
  • If you use an IDE, it will help you find classes to import and how to construct the code. Free IDEs include; IntelliJ CE, Netbeans or Eclipse – Peter Lawrey Jul 26 '11 at 06:34

3 Answers3

10

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.

Community
  • 1
  • 1
dsmith
  • 304
  • 4
  • 12
9

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);
jmnwong
  • 1,577
  • 6
  • 22
  • 33
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • how can i print the whole numbers in queue like if I add 1,2,3,4 in the queue it will print like 1 2 3 4 – hongtaesuk Jul 26 '11 at 06:39
  • 1
    This sounds like homework. Have you even tried to do it yet? Show some code and point out where you're having trouble. – Ryan Stewart Jul 26 '11 at 06:54
-1

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()
Paul
  • 19,704
  • 14
  • 78
  • 96