answersLogoWhite

0


Best Answer

A queue is a first-in-first-out (FIFO) list of elements. In C++, you could implement a queue by designing a queue class that provided a constructor, destructor, add, and remove methods. The private implementation could use an array, with size specified in the constructor. It would have pointers or indicies that "follow each other" around the array. At boundary state, the queue::add() method would either throw a queue full exception, or it could dynamically adjust the array size, and the queue:remove() method would return null or throw a queue empty exception. Other implementations could be used, such as a singly or doubly linked list, but the "cost" of constantly allocating and deallocating elements would seem to overwhelm the simplicity of the array approach, even considering the possibility of (intermittantly) increasing the allocated size. No matter what approach is used, the public interface should be stable and well defined, so that various internal approachs could be tried and evaluated.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago
#include
#include


template
class queue
{
private:
std::list data;
public:
void push (const T& val) { data.push_back (val); }
void pop () { data.pop_front (); }
const T& front () { return data.front(); }
const size_t size () const { return data.size(); }
};


int main()
{
queue q;


size_t loop = 0;
while (loop != 10)
q.push (++loop);


while (loop--)
{
std::cout << q.front() << std::endl;
q.pop();
}
}
This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<queue>

#include<stack>

int main()

{

std::queue<int> q;

std::stack<int> s;

for (int i=0; i!=10; ++i)

{

q.push (i);

s.push (i);

}

std::cout << "Queue\tStack\n";

for (int i=0; i!=10; ++i)

{

std::cout << q.front() << '\t' << s.top() << '\n';

q.pop();

s.pop();

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program using queue operation?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Can you give the explanation for various operations in a queue implementation using arrays?

The following are operations performed by queue in data structuresEnqueue (Add operation)Dequeue (Remove operation)Initialize


Queue ADT Using Array?

implement the queue ADT using an array


How do you write Square program using vb?

write a vb program to find the magic square


Explai the nature of the various types queues in data structures?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer. A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve). Mainly there are two kinds of priority queue: 1) Static priority queue 2) Dynamic priority queue A double ended queue (or deque ) is a queue where insertion and deletion can be performed at both end that is front pointer can be used for insertion (apart from its usual operation i.e. deletion) and rear pointer can be used for deletion (apart from its usual operation i.e. insertion)

Related questions

Write a program to convert stack into queue using c language?

In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Can you give the explanation for various operations in a queue implementation using arrays?

The following are operations performed by queue in data structuresEnqueue (Add operation)Dequeue (Remove operation)Initialize


Difference between circular queue and De queue?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer. A double ended queue (or deque ) is a queue where insertion and deletion can be performed at both end that is front pointer can be used for insertion (apart from its usual operation i.e. deletion) and rear pointer can be used for deletion (apart from its usual operation i.e. insertion)


What is the need of circular queue in data structures?

Circular queue is a linear data structure that follows the First In First Out principle. A re-buffering problem often occurs for each dequeue operation in a standard queue data structure. This is solved by using a circular queue which joins the front and rear ends of a queue.


Which queue most efficient queue using array?

circular queue


Queue ADT Using Array?

implement the queue ADT using an array


Write a sample program using ASPNET explaining all the syntax and semantics of the program?

write a sample program using asp.net explaining all the syntax and semantics of the program


How do you write Square program using vb?

write a vb program to find the magic square


What is circular queue operations circular queue?

A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer. There are 2 conditions for queue full if queue is implemented using arrays. First condition is Front = 1 and Rear = N Second condition is Front = Rear + 1


Would rearranging the paragraphs in a document using a word processing program be an editing operation or a forming operation?

would rearranging the paragraphs in a document using a word processing pragram be an editing operation or a formatting operation


Explai the nature of the various types queues in data structures?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer. A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve). Mainly there are two kinds of priority queue: 1) Static priority queue 2) Dynamic priority queue A double ended queue (or deque ) is a queue where insertion and deletion can be performed at both end that is front pointer can be used for insertion (apart from its usual operation i.e. deletion) and rear pointer can be used for deletion (apart from its usual operation i.e. insertion)