Logo Questions Linux Laravel Mysql Ubuntu Git Menu

What is the efficient way of implementing a queue?




What is the efficient way of implementing a queue, inorder to learn how it is implemented?

EDIT: I looked into stl::queue code inorder to learn abt how it is implemented, but the template code making it difficult to understand. After all there is better efficient way is used than having a linked list.

like image 667
Passionate programmer Avatar asked Jul 23 '10 19:07

Passionate programmer

People also ask

What is the most efficient way to implement a queue?

In the most generic sense, a linked-list would be your best bet if you maintain a front and rear pointer. In this case, queue insertion and deletion is an O(1) operation. You can also implement one using an array and maintaining indices for the front and rear.

Which of the following typically uses a queue for an efficient implementation?

Among these data structures, heap data structure provides an efficient implementation of priority queues. In a normal queue, queue is implemented based on FIFO but in priority queue nodes are removed based on the priority.

What are the different ways to implement queue?

Queue can be implemented using an Array, Stack or Linked List. The easiest way of implementing a queue is by using an Array. Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of the array (starting the index of array from 0 ).

How do you effectively implement K queues in an array?

A simple way to implement k queues is to divide the array in k slots of size n/k each, and fix the slots for different queues, i.e., use arr[0] to arr[n/k-1] for the first queue, and arr[n/k] to arr[2n/k-1] for queue2 where arr[] is the array to be used to implement two queues and size of array be n.

1 Answers

The most efficent way is to have someone else do it.

Both C++ and C# (and .NET et al) have one in their native libraries.

like image 133
James Curran Avatar answered Oct 13 '22 20:10

James Curran