C++ Queues
Descriptions
A Queue stores multiple data’s in a specific order, called FIFO.
FIFO stands for First in, First Out. To visualize FIFO, think of a standing in a line to get a ticket for circus show, The first person to stand in line is also the first who can pay and get ticket and leave the line.
To use a Queue, you have to include the <queue> header file
Unlike vectors, data’s in the queue are not accessed by index numbers. Since queue data’s are added at the end and removed from the front, you can only access an element at the front or the back.
விரிவுரை
ஒரு வரிசை என்பது FIFO எனப்படும், ஒரு குறிப்பிட்ட வரிசையில் பல தரவுகளைச் சேமிக்கிறது.
FIFO என்பது முதலில் உள்ளே, முதலில் வெளியே என்பதைக் குறிக்கிறது. FIFO ஐ காட்சிப்படுத்த, சர்க்கஸ் நிகழ்ச்சிக்கான டிக்கெட்டைப் பெற வரிசையில் நிற்பதை நினைத்துப் பாருங்கள். வரிசையில் நிற்கும் முதல் நபர் முதலில் பணம் செலுத்தி டிக்கெட்டைப் பெற்று வரியை விட்டு வெளியேறக்கூடியவராகவும் இருப்பார்.
வரிசையைப் பயன்படுத்த, நீங்கள் <queue> தலைப்புக் கோப்பைச் சேர்க்க வேண்டும்.
வெக்டர்களைப் போலன்றி, வரிசையில் உள்ள தரவு குறியீட்டு எண்களால் அணுகப்படுவதில்லை. வரிசைத் தரவு இறுதியில் சேர்க்கப்பட்டு முன்பக்கத்திலிருந்து அகற்றப்படுவதால், முன்பக்கத்தில் அல்லது பின்பக்கத்தில் உள்ள ஒரு உறுப்பை மட்டுமே அணுக முடியும்.
Create / Add/ Remove / Access Data in Queue
To create a queue, use the queue keyword, and specify the type of values it should store within angle brackets <> and then the name of the queue, like: queue<type> queueName.
ஒரு வரிசையை உருவாக்க, வரிசை முக்கிய சொல்லைப் பயன்படுத்தி, கோண அடைப்புக்குறிக்குள் சேமிக்க வேண்டிய மதிப்புகளின் வகையைக் குறிப்பிடவும், பின்னர் வரிசையின் பெயரைக் குறிப்பிடவும், queue<type>queueName.
Syntax
// Include the queue library
#include <queue>
queue <string> foods;
type queueName
To add data to the queue, you can use the .push() function after declaring the queue.
The .push() function adds a data at the end of the queue
வரிசையில் தரவைச் சேர்க்க, வரிசையை அறிவித்த பிறகு .push() செயல்பாட்டைப் பயன்படுத்தலாம்.
.push() செயல்பாடு வரிசையின் முடிவில் ஒரு உறுப்பைச் சேர்க்கிறது.
Data can removed from the queue by using .pop() function.
This will remove the front element (the first and oldest data that was added to the queue)
.pop() செயல்பாட்டைப் பயன்படுத்தி வரிசையில் இருந்து தரவை நீக்கலாம்.
இது முன் உறுப்பை (வரிசையில் சேர்க்கப்பட்ட முதல் மற்றும் பழைய தரவு) நீக்கும்.
.front() and .back() function are used to access or to change the data of the front and back in queue, as index not supported.
.front() மற்றும் .back() செயல்பாடுகள், குறியீட்டு ஆதரிக்கப்படாததால், வரிசையில் முன் மற்றும் பின் தரவை அணுக அல்லது மாற்றப் பயன்படுகின்றன.
Example / உதாரணம்
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> foods;
// Add a data to queue
foods.push(“RavaDosa”);
foods.push(“Kesari”);
foods.push(“Idly”);
foods.push(“Dosa”);
cout<<foods.front(); // RavaDosa
cout<<“\n”;
// Remove the FIFO data
foods.pop(); // RavaDosa oldest data got removed first
cout<<foods.front(); // Outputs Kesari
cout<<“\n”;
// Change the value of the first element
foods.front() = “MasalDosa”;
// show the last item on the stack
cout<<foods.back(); // Dosa
foods.back() = “Pongal”;
cout<<“\n”;
cout<<foods.front(); // MasalDosa
// Remove the FIFO data
foods.pop(); // MasalDosa
cout<<“\n”;
cout<<foods.front(); //Idly
return 0;
}
Output
RavaDosa
Kesari
Dosa
MasalDosa
Idly
Queue size and empty
To find size of queue, use the .size() function and to check queue is empty or not use .empty() function returns 1 (true) when queue is empty. 0 (false) when data is in queue.
வரிசையின் அளவைக் கண்டறிய, .size() செயல்பாட்டைப் பயன்படுத்தவும், வரிசை காலியாக உள்ளதா இல்லையா என்பதைச் சரிபார்க்க, .empty() செயல்பாட்டைப் பயன்படுத்தவும், வரிசை காலியாக இருக்கும்போது 1 (true) ஐயும், தரவு வரிசையில் இருக்கும்போது 0 (false) ஐயும் வழங்கும்.
Example / உதாரணம்
queue<string> foods;
cout << foods.empty(); // Outputs 1 (The queue is empty)
queue<string> cars;
cars.push(“Idly”);
cars.push(“Vada”);
cars.push(“Pongal”);
cars.push(“Dosa”);
cout << foods.empty(); // Outputs 0 (not empty)
cout<< “\n”;
cout << foods.size(); // Outputs 4