Queue Implementation using Linked List in DSA C++

Program 1

// Queue Linked List
#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
    int data;
    Node *add;
    public:
    void create();
    void insert();
    void delete1();
    void display();
};
Node *start=NULL,*new1,*temp,*prev1;
void Node::create()
{
      int n;
      char choice;
      cout<<"\n Enter an element: ";
      cin>>n;
      start=new Node();
      start->data=n;
      start->add=NULL;
      temp=start;
      cout<<"\n Want to continue(Y/y): ";
      cin>>choice;
      while(choice=='y' || choice=='Y')
      {
          cout<<"\n Enter next element: ";
         cin>>n; 
         new1=new Node();
         new1->data=n;
         new1->add=NULL;
         temp->add=new1;
         temp=temp->add;
         cout<<"\n Want to continue(Y/y): ";
         cin>>choice;
      }
}
void Node::display()
{
       if(start==NULL)
       cout<<"\n List is empty";
       else
       {
            temp=start;
            while(temp!=NULL)
            {
                cout<<temp->data<<"    ";
                temp=temp->add;
            }
       }
}
void Node::insert()
{
    int n;
    if(start==NULL)
        cout<<"\n Queue not found";
    else
    {
        cout<<"Enter an element: ";
        cin>>n;
        new1=new Node();
        new1->data=n;
        new1->add=NULL;
        temp=start;
        while(temp->add!=NULL)
           temp=temp->add;
        temp->add=new1;
    }    
    
}
void Node::delete1()
{
    int n;
    if(start==NULL)
        cout<<"\n Queue not found";
    else
    {
          temp=start;
          start=start->add;
          cout<<"\n Deleted element  from queue is: "<<temp->data;
          delete temp;
    }
}

int main()
{
    Node obj;
    system("cls");
    int ch;
  do
  {
     cout<<"\n ----------------Queue Linked List-------------------";
    cout<<"\n 1. Create Queue";
    cout<<"\n 2. Insert";
    cout<<"\n 3. Delete";
    cout<<"\n 4. Display";
    cout<<"\n 5. Exit";
    cout<<"\n--------------------------------------------------------";
    cout<<"\n Enter your choice: ";
     cin>>ch;
     switch(ch)
     {
        case 1: obj.create();break;
        case 2: obj.insert();break;
        case 3:obj.delete1();break;
        case 4:obj.display();break;
        case 5:break;
     }
    }while(ch!=5);

    return 0;
}

 

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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