Questions tagged [deque]

A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.

993 questions
5
votes
4 answers

C++ large deque - program takes very long time to exit?

Consider the following C++ program: #include #include using namespace std; int main() { deque d(30000000); cout << "Done\n"; } The memory allocation in the first line only takes a second, but after it prints…
user14145203
5
votes
4 answers

Why does java linkedlist implementation use the interface deque?

I was looking at the java implementation of LinkedList, and found this: public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, java.io.Serializable Why should a LinkedList…
Abhijeet Kashnia
  • 12,290
  • 8
  • 38
  • 50
5
votes
2 answers

How to use collections.deque most effectively (popleft vs. appendleft)

I have been learning queues as I study data structures in Python and wanted to ask a question regarding its use. I suppose there are two ways append/pop from a queue. First way would be to use deque.append() and deque.popleft(). Another way would…
mysl
  • 1,003
  • 2
  • 9
  • 19
5
votes
3 answers

How to keep ordered unique items in a size limited structure?

I need to save stream of elements in a size limited list. There may be duplicate elements in the stream but I need to just keep the unique ones. Also when the size of my list exceeds a specified limit, I need to remove the oldest element and add the…
Farzin
  • 359
  • 1
  • 4
  • 21
5
votes
1 answer

How to maintain deque with quick lookup?

I need to build a circular buffer as a deque in python with efficient search (not O(n) el in deque, but O(1) like in set()) from collections import deque deque = deque(maxlen=10) # in my case maxlen=1000 for i in range(20): …
dandan
  • 163
  • 7
5
votes
3 answers

Queue performance wise which is better implementation - Array or Linked list

Which way gives the faster enqueueing and dequeuing when I have to insert very few elements, Is array better than a linked list? I need to insert few element and I have to remove and read that removed element from queue. If it is array I may have to…
NoAIUser
  • 3,966
  • 6
  • 34
  • 52
5
votes
2 answers

clearing the `deque`s: is it thread-safe?

I've been reading about the thread-safety of CPython's collections.deque here and here. I understand that .append() and .popleft() operations play nice with each other across threads, as do .appendleft() and .pop(). My question is: does the same…
jez
  • 14,867
  • 5
  • 37
  • 64
5
votes
2 answers

Python: Is there a thread-safe version of a deque?

I have a threaded program consisting of a Consumer class and a Producer class. Currently, I use a Fifo queue.Queue in the implementation, where the producer puts the data at the end of the queue and the consumer gets it. However, I would like to add…
Victor Odouard
  • 1,345
  • 3
  • 15
  • 28
5
votes
2 answers

Assigning a size to a Deque in Java

I'm having trouble assigning a limit to the size of my double ended queue (Deque). It seems that my queue never gets full and just resizes whenever I add or offer a value unto it. My simple code just accepts a string value, splits it by space " ",…
Juni
  • 79
  • 2
  • 7
5
votes
4 answers

Problem with invalidation of STL iterators when calling erase

The STL standard defines that when an erase occurs on containers such as std::deque, std::list etc iterators are invalidated. My question is as follows, assuming the list of integers contained in a std::deque, and a pair of indicies indicating a…
Matthieu N.
5
votes
3 answers

collections.deque get index of element by value

For list we can get index of element list_name.index(3) How to get index of item in deque. ex: d_list = deque([1, 2, 3, 4]) what is the best way to get the index of element 3. Edit: I am using Python 2.7.6
Ravi Kumar
  • 1,769
  • 2
  • 21
  • 31
5
votes
2 answers

How do I find out if a value is in a deque?

say i have a deque with values [0,3,5,1,5,8]. I want to save all information about the deque including order, but I have to find if the value 5 is in the deque. What is some pseudo-code that could determine this?
compStudent
  • 71
  • 2
  • 7
5
votes
3 answers

Sorting a deque using limited operations?

Hi I came across a question in the Algorithms 4th Edition by Robert Sedgewick. Dequeue sort. Explain how you would sort a deck of cards, with the restriction that the only allowed operations are to look at the values of the top two cards, to…
Navleen Singh
  • 155
  • 2
  • 11
5
votes
2 answers

When does std::deque need reallocation?

As far as I know std::deque stores its elements in pieces of chunks (although its implementation-dependent, but this is what I read in most of the sources) as opposed to std::vector which in most of the cases uses a single block of memory. So, it's…
ravi
  • 10,994
  • 1
  • 18
  • 36
5
votes
1 answer

Will copy constructor of an object be called upon resize of a vector

Suppose I have a std::vector now I understand that insertion at the end of a vector is amortized constant. Which means it could either be O(1) or O(n) (since it acquire a new memory block , copies the old content into the new memory block). My…
MistyD
  • 16,373
  • 40
  • 138
  • 240