answersLogoWhite

0


Best Answer

A queue usually infers first in first out (FIFO), therefore new values will need to be inserted after the last node (the tail node) and the queue must maintain pointers to both the head node for extraction and the tail node for insertion, and each node must maintain a pointer to the next node in the queue (singly-linked list).

The following code provides a bare-bones implementation of a singly-linked FIFO list, where each value is an integer. In real-life you'd use one of the built-in class templates, otherwise you'd have to re-write the same code to cater for all the different value types you might want to place in a queue (or create your own class template). However, the example serves to show the algorithm that is used to insert new values to the end of a queue. It does not show how to extract the first node from the queue.

#include <iostream>

using namespace std;

class Node

{

public:

Node( int iValue ):m_iValue( iValue ),m_pNext( NULL ){}

~Node(){ if( m_pNext ) delete m_pNext; }

Node * GetNext()const{ return m_pNext; }

void SetNext(Node * pNext ){ m_pNext = pNext; }

int GetValue()const{ return m_iValue; }

private:

Node * m_pNext;

int m_iValue;

};

class Queue

{

public:

Queue():m_pFirst(NULL),m_pLast(NULL){}

~Queue(){ if( m_pFirst ) delete m_pFirst; }

Node * GetFirst()const{ return m_pFirst; }

Node * GetLast()const{ return m_pLast; }

void Insert( int iValue );

void ShowAll()const;

private:

Node * m_pFirst;

Node * m_pLast;

};

void Queue::Insert( int iValue )

{

Node * pNew = new Node( iValue );

if( m_pLast )

m_pLast->SetNext( pNew );

else

m_pFirst = pNew;

m_pLast = pNew;

}

void Queue::ShowAll() const

{

Node * pNode = m_pFirst;

while( pNode )

{

cout << pNode->GetValue();

if( pNode != m_pLast )

cout << ", ";

pNode = pNode->GetNext();

}

cout << endl << endl;

}

int main()

{

Queue queue;

queue.Insert( 2 );

queue.Insert( 1 );

queue.Insert( 3 );

queue.ShowAll();

return( 0 );

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the algorithm for inserting values into a queue in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What difference between two nodes in c plus plus of single link list?

Their address. They may also have different values, and their sequence may matter, depending on the design of the algorithm.


Array implementation of priority queue example program in c plus plus?

yes


Algorithm of push and pop in c plus plus?

pop push c++ programming


A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.


What is c plus plus program use to convert algorithm in to c plus plus program?

You can't convert an algorithm into code. That is the job of the programmer, not the language. Algorithm's are expressed in plain-English and typically use pseudocode to broadly demonstrate the implementation of the algorithm. However, it is the programmer's job to convert these algorithms into working code. Pseudocode isn't a programming language as such, but it uses structures and statements that are familiar to any programmer and can be easily translated into any language. However, pseudocode is not a standard so there are many different ways to present pseudocode to the programmer. Moreover, pseudocode is generalised and is far too generic to be converted directly into any one language, never mind C++, which can take advantage of the underlying hardware to produce more efficient algorithms than would otherwise be implied by the pseudocode alone. Hence the need for plain-English algorithms in conjunction with the pseudocode. Programmer's can process all this information far more easily than any computer can. Even if you could program a converter for one algorithm, there's no guarantee it would work for any other algorithm. The time spent programming an algorithm converter would be far better spent simply translating the algorithm yourself.

Related questions

What is the program of breadth first search in c plus plus?

The algorithm for breadth first search is to start at the root node or at an arbitrary node within the tree. First, push this node onto a queue. Then proceed as follows 1. If the queue is empty, quit the search and return a "not found" result. 2. Pop the first node from the queue. 3. If this node contains the value you seek, quit the search and return the node. 4. Enumerate the child nodes (if any), and push them onto the queue. 5. Go to step 1.


What difference between two nodes in c plus plus of single link list?

Their address. They may also have different values, and their sequence may matter, depending on the design of the algorithm.


Array implementation of priority queue example program in c plus plus?

yes


How does algorithm and c plus plus program get along?

They are bosom-friends.


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Algorithm of push and pop in c plus plus?

pop push c++ programming


A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.


What is dry run in c plus plus terminology?

A manual check of the algorithm to ensure its correctness.


How do you check a user's text input in C plus plus?

Use an SLR parser algorithm.


How do you find a largest algorithm in c plus plus?

#define max (a, b) ((a) &gt;= (b)) ? (a) : (b)


What is GCD in C Plus Plus?

A C++ implementation of the Binary GCD (Stern's) algorithm is shown in the Related Link below.


How you write C plus plus programming code for Lempel-Zip and Walch algorithm to compress files?

Lempel-Ziv-Welch (LZW) encoding is patented, thus we cannot show you a working example. However, the basic algorithm is well-documented on various websites including Wikipedia. If you can follow the algorithm, you can write the code. But you cannot distribute the code without the requisite licence.