Queue Implementation Using Linked List














































Queue Implementation Using Linked List



DESCRIPTION: To implement data structure Queue using linked list, we maintain two pointers, fptr and rptr. The fptr  points the first element of queue while rptr points to the last element.

There are some useful operations of queue.

push(): This operation adds a new node in the last of queue and moves rptr to the next node.

pop(): This operation removes the front node of queue and moves fptr to the next node.

front(): Return the first element of queue.

size(): Print the size of queue


/* Implementation of Queue using linked list*/

#include<iostream>
#include<bits/stdc++.h>

using namespace std;

inline void error(){
    throw runtime_error("Queue is empty");
}

template<typename T>
struct node{
    T data;
    node *next;
};
template<typename T>
class Queue{
private:
    node<T> *fptr,*rptr;
    int sz;
public:
    Queue():fptr(NULL),rptr(NULL),sz(0){}  //constructor
   
    //method to create new node
    node<T> *create_node(T data){
         node<T> *temp = new node<T>;
        temp->data = data;
        temp->next = NULL;
    }
    //push method to push the element in the queue
    void push(T data){
         node<T> *new_node = create_node(data);
        if(fptr==NULL){
        //if the list is empty
            fptr = new_node;
            rptr = new_node;
        }
        else{
            rptr->next = new_node;
            rptr = rptr->next;
        }
        sz++;
    }
    //pop method to pop the elememt from the queue
   void pop(){
        //try will throw error if queue will be empty, in that case
        //catch will execute

        try{
           if(fptr==NULL)
                    error();
            node<T> *temp = fptr;
            cout<<"Popped element is: "<<temp->data<<"\n\n";
            fptr = fptr->next;
            free(temp);sz--;
            }
         catch(runtime_error e){
                    cout<<"Runtime-error: "<<e.what()<<"\n\n";   //to display the error message
                }

    }
   
    //to check the front elememt of queue
   T front(){
       
        return fptr->data;

    }
    //to find the size of queue
    int size(){
         return sz;
    }
};

int main(){
    cout<<"Press 1 to push the elememt\n";
    cout<<"Press 2 to pop the element\n";
    cout<<"Press 3 for front element\n";
    cout<<"Press 4 to find the size of queue\n";
    cout<<"Press 5 to exit from the program\n";

     Queue<int> que;  //declares the object

    while(true){
       cout<<"Select  the choice: ";
        int ch;cin>>ch;
        switch(ch){
            case 1:
                cout<<"Enter the element: ";
                int ele;cin>>ele;
                que.push(ele);   //element is pushed
                break;

            case 2:
                que.pop();    //element is popped
                break;

            case 3:
                try{
                    if(que.size()<=0)
                    error();
                    cout<<"Front element is: "<<que.front()<<"\n\n";
                }
                catch(runtime_error e){
                    cout<<"Runtime-error: "<<e.what()<<"\n\n";     //to display the error message
                }
                break;

            case 4:
                cout<<"Size of the queue is: "<<que.size()<<"\n\n";
                break;

            case 5:
                exit(0);

            default:
                 cout<<"Incorrect option, Select the option from (1,2,3,4,5): Thanks\n\n";

        }
    }
     return 0;
}





More Articles of Ashish Kumar:

Name Views Likes
Queue Implementation Using Linked List 343 25

Comments

  • Rahul
    12-May-2019 02:45:44 AM
    nice writeup!
  • Raj
    12-May-2019 12:20:19 AM
    nice