Que Implementation with LinkedList

/*
The structure of the node of the queue is
struct QueueNode
{
    int data;
    QueueNode *next;
};


and the structure of the class is
class Queue {
private:
    QueueNode *front;
    QueueNode *rear;
public :
    void push(int);
    int pop();
};

 */

/* The method push to push element into the queue*/
void Queue:: push(int x)
{
        // Your Code
        QueueNode *tmp = new QueueNode;
        tmp->data=x;

        if(front==NULL)
        {
            front=tmp;
            rear=tmp;
        }
        else
        {
            rear->next=tmp;
            rear=rear->next;
        }
}

/*The method pop which return the element poped out of the queue*/
int Queue :: pop()
{
        // Your Code
        if(front==NULL)
        return -1;
        int v=front->data;
        front=front->next;
        return v;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *