Site icon T4Tutorials.com

Queue Insert Delete Implementation and Operations in Data Structures (C plus plus)

Queue Insert Delete Implementation and Operations in Data Structures (C plus plus)

In this tutorial, we will learn about Queue Insert Delete Implementation and Operations in Data Structures (C plus plus).

/*C++ Program to Implement  Queue using Linked List*/

#include<iostream>

using namespace std;

struct node
{
    int info;
    node *next;
}
*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
void push(int x)
{
    np = new node;
    np->info = x;
    np->next = NULL;
    if(front == NULL)
    {
        front = rear = np;
        rear->next = NULL;
    }
    else
    {
        rear->next = np;
        rear = np;
        rear->next = NULL;
    }
}
int del()
{
    int x;
    if(front == NULL)
    {
        cout<<"The queue is empty \n";
    }
    else
    {
        p = front;
        x = p->info;
        front = front->next;
        delete(p);
        return(x);
    }
}
int main()
{
    int num, z = 0 , val;
    cout<<"Please enter the number of values to be  entered into queue: \n";
    cin>>num;
    while (z < num)
    {
	cout<<"Please enter the values to be entered into queue: \n";
	cin>>val;
        push(val);
        z++;
     }
     cout<<"\n\nRemoved Values are following: \n\n";
     while(true)
     {
        if (front != NULL)
            cout<<del()<<endl;
        else
            break;
     }
     
     
}

Output:

Queue Insert Delete Implementation and Operations in Data Structures (C plus plus)
Exit mobile version