Site icon T4Tutorials.com

Which of the following principle does queue use?

Which of the following principle does queue use?

(A). LIFO

(B). FIFO

(C). Both

(D). None of these

Question’s Answer: FIFO

FIFO means first element added to the queue is the first one to be removed from queue.

Data Structure Principle
Queue FIFO
Stack LIFO
Array linear data structure without an inherent ordering principle
Tree hierarchical data structure without an inherent ordering principle
Linked List linear data structure without an inherent ordering principle

Example

#include <iostream>
#include <queue>

using namespace std;

int main()
{
    // Create an empty queue
    queue<int> Queue;

    // Push elements onto the queue
    Queue.push(5);
    Queue.push(8);
       cout << "Queue size: " << Queue.size() << endl<< endl;

    // Access the front and back elements of the queue
    cout << "Front element: " << Queue.front() << endl<< endl;
    cout << "Back element: " << Queue.back() << endl<< endl;

    // Pop elements from the queue
    Queue.pop();
    Queue.pop();

    // Get the new size of the queue
    cout << "Queue size after two pops: " << Queue.size() << endl<< endl;

    // Check if the queue is empty
    if (Queue.empty()) {
        cout << "Queue is empty" << endl<< endl;
    } else {
        cout << "Queue is not empty" << endl<< endl;
    }

    return 0;
}

Output

Queue size: 2
Front element: 5
Back element: 8
Queue size after two pops: 0
Queue is empty

 

Exit mobile version