Implement Queue using Linked List || Linear Queue || C++
In this we are going to see a program to implement linear queue using linked list  in C++ Programming Language.

 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *front;
struct node *rear;
void insert_element()
{
    struct node *ptr;
    int item;
    ptr = (struct node *)malloc(sizeof(struct node *));
    if (ptr == NULL)
        cout << "Overflow" << endl;
    else
    {
        cout << "Enter a value: ";
        cin >> item;
        ptr->data = item;
        ptr->next = NULL;
        if (front == NULL)
        {
            front = ptr;
            rear = ptr;
        }
        else
        {
            rear->next = ptr;
            rear = ptr;
        }
        cout << "Value inserted" << endl;
    }
}
void remove_element()
{
    struct node *ptr;
    int item;
    if (front == NULL)
        cout << "Underflow" << endl;
    else
    {
        item = front->data;
        ptr = front;
        cout << item << endl;
        front = front->next;
        free(ptr);
        cout << "Value Deleted" << endl;
    }
}
void display()
{
    struct node *ptr;
    ptr = front;
    cout << "Values are: ";
    while (ptr != NULL)
    {
        cout << ptr->data << " ";
        ptr = ptr->next;
    }
}
void main()
{
    clrscr();
    int choice;
    cout << "MENU\n1. Insert Element\n2. Remove Element\n3. Display\n4. Exit\n";
    do
    {
        cout << "\nEnter your choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            insert_element();
            break;
        case 2:
            remove_element();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(1);
            break;
        default:
            cout << "Invalid choice" << endl;
        }
    } while (choice != 4);
    getch();
}



The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{
    int data;
    struct node *next;
};
struct node *front;
struct node *rear;
void insert_element()
{
    struct node *ptr;
    int item;
    ptr = (struct node *)malloc(sizeof(struct node *));
    if (ptr == NULL)
    {
        cout << "Overflow" << endl;
    }
    else
    {
        cout << "Enter a value: ";
        cin >> item;
        ptr->data = item;
        ptr->next = NULL;
        if (front == NULL)
        {
            front = ptr;
            rear = ptr;
        }
        else
        {
            rear->next = ptr;
            rear = ptr;
        }
        cout << "Value inserted" << endl;
    }
}
void remove_element()
{
    struct node *ptr;
    int item;
    if (front == NULL)
    {
        cout << "Underflow" << endl;
    }
    else
    {
        item = front->data;
        ptr = front;
        cout << item << endl;
        front = front->next;
        free(ptr);
        cout << "Value Deleted" << endl;
    }
}
void display()
{
    struct node *ptr;
    ptr = front;
    cout << "Values are: ";
    while (ptr != NULL)
    {
        cout << ptr->data << " ";
        ptr = ptr->next;
    }
}
int main()
{
    int choice;
    cout << "MENU\n1. Insert Element\n2. Remove Element\n3. Display\n4. Exit\n";
    do
    {
        cout << "\nEnter your choice: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
        {
            insert_element();
            break;
        }
        case 2:
        {
            remove_element();
            break;
        }
        case 3:
        {
            display();
            break;
        }
        case 4:
        {
            exit(0);
            break;
        }
        default:
        {
            cout << "Invalid choice" << endl;
        }
        }
    } while (choice != 4);
    return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post