deques in c++


// A CPP program to demonstrate linked list  
// based implementation of queue  
#include <bits/stdc++.h> 
using namespace std; 
  
// A linked list (LL) node to store a queue entry  
class QNode  
{  
    public: 
    int key;  
    QNode *next;  
};  
  
// The queue, front stores the front node 
// of LL and rear stores ths last node of LL  
class Queue  
{  
    public: 
    QNode *front, *rear;  
};  
  
// A utility function to create  
// a new linked list node.  
QNode* newNode(int k)  
{  
    QNode *temp = new QNode(); 
    temp->key = k;  
    temp->next = NULL;  
    return temp;  
}  
  
// A utility function to create an empty queue  
Queue *createQueue()  
{  
    Queue *q = new Queue(); 
    q->front = q->rear = NULL;  
    return q;  
}  
  
// The function to add a key k to q  
void enQueue(Queue *q, int k)  
{  
    // Create a new LL node  
    QNode *temp = newNode(k);  
  
    // If queue is empty, then  
    // new node is front and rear both  
    if (q->rear == NULL)  
    {  
    q->front = q->rear = temp;  
    return;  
    }  
  
    // Add the new node at  
    // the end of queue and change rear  
    q->rear->next = temp;  
    q->rear = temp;  
}  
  
// Function to remove 
// a key from given queue q  
QNode *deQueue(Queue *q)  
{  
    // If queue is empty, return NULL.  
    if (q->front == NULL)  
    return NULL;  
  
    // Store previous front and  
    // move front one node ahead  
    QNode *temp = q->front;  
    q->front = q->front->next;  
  
    // If front becomes NULL, then  
    // change rear also as NULL  
    if (q->front == NULL)  
    q->rear = NULL;  
    return temp;  
}  
  
// Driver code  
int main()  
{  
    Queue *q = createQueue();  
    enQueue(q, 10);  
    enQueue(q, 20);  
    deQueue(q);  
    deQueue(q);  
    enQueue(q, 30);  
    enQueue(q, 40);  
    enQueue(q, 50);  
    QNode *n = deQueue(q);  
    if (n != NULL)  
    cout << "Dequeued item is " << n->key;  
    return 0;  
}  
  

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source