A collection that allows insertions at one end and removals from the other.
Pros | Cons |
---|---|
Provides a way to keep a buffer of things in the received order | Limited access to elements |
q = deque()
q.append(1);
q.append(2); // [1, 2]
q.append(3); // [1, 2, 3]q.popleft(); // [2, 3]
q.popleft(); // [3]
std::queue q;
q.push(1);
q.push(2); // [1, 2]
q.push(3); // [1, 2, 3]q.pop(); // [2, 3]
q.pop(); // [3]
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2); // [1, 2]
q.add(3); // [1, 2, 3]q.poll(); // [2, 3]
q.poll(); // [3]
Operations | Worst | Average |
---|---|---|
Delete | O(1) | |
Insert | O(1) | |
Search | O(n) |