• June 12, 2021

PHSARIT

Bring Technology Ideas with you

 Queue STD Data Structure in C++ programming

Queue is one of the Standard Template Library to use for data containers or collections in Data Structure. Its work as designed to operate in a FIFO context.

First In First Out Context

The context is allows the program to insert data from one an other and remove it accordingly from the first data. By definition from http://www.cplusplus.com/reference/queue/queue/ said that queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the “back” of the specific container and popped from its “front”.

Function or Operations to work with Queue

There are few operations which will used to access or work with data in the collections of queue such as:

  • empty() : To working on checking whether there are any data in the collection
  • size () : To know the size of data in the data collection and return the counting number.
  • front (): To access the first value/data from the collection (this value must be adding first into collection)
  • back(): To access the last value/data from the collection (this value must be the last inserting into collection)
  • push(): To insert elements/values in to queue data collection
  • pop(): To remove the elements/values from queue data collection.

Try out example as below:

  1. The first example is our coding in C++ by implementing the code in the main program of C++. However, you need to include the #include<queue> in the head of the code in Visual Studio. See the example below:
#include<iostream>
#include<queue>

#include<string>
using namespace std;

The above code is the head of the code for you to try with queue. We use #include<string> due to we want to insert string values into Queue collection. As the main code will be below:

int main()
{
	queue <string> myStudent; // declear queue , queue <data type> Obje.Name
	myStudent.push("Sela"); // using push function to insert data
	myStudent.push("Niroth");
	myStudent.push("Seyha");
	myStudent.push("Pheaktra");
	myStudent.push("Raksmey");
	/-----remove----using pop() to remove
	myStudent.pop();

	//using size() function to count the number of students
	cout << "The amount of students in the class: " << myStudent.size() << endl; 

	//access to the front/first value/data
	//cout << "The first element is :" << myStudent.front() << endl;

	//access to the last data 
	cout << "The last element is :" << myStudent.back() << endl;

	// show the student list using empty fuction
	cout << "List of students: " << endl;
	*while (!myStudent.empty())
	{
	cout << "-" << myStudent.front() << endl;
	myStudent.pop();
	}

The full code in the first example

#include<iostream>
#include<queue>

#include<string>
using namespace std;
int main()
{
	queue <string> myStudent; // declear queue , queue <data type> Obje.Name
	myStudent.push("Sela"); // using push function to insert data
	myStudent.push("Niroth");
	myStudent.push("Seyha");
	myStudent.push("Pheaktra");
	myStudent.push("Raksmey");
	/-----remove----using pop() to remove
	myStudent.pop();

	//using size() function to count the number of students
	cout << "The amount of students in the class: " << myStudent.size() << endl; 

	//access to the front/first value/data
	//cout << "The first element is :" << myStudent.front() << endl;

	//access to the last data 
	cout << "The last element is :" << myStudent.back() << endl;

	// show the student list using empty fuction
	cout << "List of students: " << endl;
	*while (!myStudent.empty())
	{
	cout << "-" << myStudent.front() << endl;
	myStudent.pop();
	}
system("pause==0");
}

Now, we are giving you an other example by using function to display the values from the collection as below:

void DiplayStudents(queue <string> MyQue)
	{
		while (!MyQue.empty())
		{
			cout << "-" << MyQue.front() << endl;
			MyQue.pop();
		}

	}

The full code can be show as below:

#include<iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
//objective to create queue for students row
	void DiplayStudents(queue <string> MyQue)
	{
		while (!MyQue.empty())
		{
			cout << "-" << MyQue.front() << endl;
			MyQue.pop();
		}

	}
int main()
{
cout << "List of students in Class A:" << endl;
	queue <string> ClassA;
	ClassA.push("Tina");
	ClassA.push("Vicheka");
	ClassA.push("Niza");
	ClassA.push("Kanha");
	ClassA.push("Songha");
	DiplayStudents(ClassA);
system("pause==0");
}

Both the example the above will show the same result as below:

    Compiling result

    Related post