Appearance
Linked List based Queue
Queues are a fundamental data structure used for managing ordered data with FIFO (First-In-First-Out) semantics. They're especially useful in systems where order of arrival matters and throughput needs to be predictable.
Real world use cases for queues
- Passing messages between different components in your program.
- Producer-Consumer pipelines use queues to pass messages safely between threads.
- Market data ingestion pipelines buffer millions of messages per second before they can be consumed and processed.
- Network layer uses queues to buffer packets internally before the application picks them off the NIC.
Your task
Implement a templated linked list--based queue in C++. Your queue should support:
- push: Add an element to the back of the queue
- pop: Remove an element from the front of the queue
- front: Return the element at the front of the queue
- back: Return the element at the back of the queue
- emplace (bonus): Construct an element in place at the back of the queue
Use smart pointers where appropriate and ensure no memory leaks. Ensure O(1) time complexity for all operations.
cpp
struct Msg
{
uint8_t id;
uint64_t timestamp;
};
queue<Msg> myQueue;
Msg msg{1,1};
myQueue.push(msg); // enqueue
myQueue.emplace(2, 2); // construct message in place
int front = myQueue.front(); // peek at front
myQueue.pop(); // dequeueImplement a basic queue using a linked list in C++.
cpp
// TODO: Implement push operation
// Add element to the back of the queue
}
void pop() {
// TODO: Implement pop operation
// Remove from the front of the queue
return T{};
}
T& front() const {
// TODO: Implement front operation
// Return the front element without removing it
return T{};
}
T& back() const {
// TODO: Implement back operation
// Return the back element without removing it
return T{};
}
bool empty() const {
// TODO: Implement empty check
// Return true if queue is empty, false otherwise
return true;
}
};
#include <memory>
template<typename T>
class Queue {
private:
// TODO: Add your data members here
// Hint: You'll need front and rear pointers for a linked list implementation
public:
Queue() {
// TODO: Initialize your queue
}
void push(const T& value) {