If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In a double-ended queue (deque), the dequeue operation allows you to remove elements from both ends of the queue. In C++, you can use the
std::dequecontainer from the Standard Template Library (STL) to implement a double-ended queue.Here's an example of how to use the
std::dequeto perform dequeue operations:cppCopy code
#include <iostream> #include <deque> int main() { std::deque<int> deque; deque.push_back(10); // Enqueue at the back (rear) of the deque deque.push_back(20); deque.push_back(30); deque.push_front(5); // Enqueue at the front (front) of the deque deque.push_front(2); std::cout << "Deque elements: "; for (const auto& element : deque) { std::cout << element << " "; } std::cout << std::endl; deque.pop_back(); // Dequeue from the back (rear) of the deque deque.pop_front(); // Dequeue from the front (front) of the deque std::cout << "Deque elements after dequeue: "; for (const auto& element : deque) { std::cout << element << " "; } std::cout << std::endl; return 0; }In this example, we use the
std::dequeto create a double-ended queue nameddeque. We can usepush_back()to enqueue elements at the back (rear) of the deque andpush_front()to enqueue elements at the front (front) of the deque. Thepop_back()operation dequeues an element from the back (rear) of the deque, andpop_front()dequeues an element from the front (front) of the deque.The output of the program will be:
yamlCopy code
Deque elements: 2 5 10 20 30 Deque elements after dequeue: 5 10 20As you can see, elements are dequeued from both ends of the deque using the
pop_back()andpop_front()operations. Thestd::dequeis a versatile data structure that allows efficient insertion and deletion from both ends, making it suitable for implementing a double-ended queue.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform
Didn't find what you're looking for?
Contact Support