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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #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