C# program to implement Linear Queue using structure

Here, we are going to learn how to implement Linear Queue using structure in C#?
Submitted by Nidhi, on November 05, 2020 [Last updated : March 27, 2023]

Linear Queue Using Structure

Here, we will implement a Linear Queue using structure. The linear queue follows FIFO (First In First Out) property. In the linear queue, we used FRONT and REAR pointer. We can insert the item at the REAR end and delete the item from the REAR end.

Linear Queue Implementation Using Structure in C#

The source code to implement Linear Queue using structure is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to implement Linear Queue using structure.

using System;

struct LinearQueue
{
    private int[] ele;
    private int front;
    private int rear;
    private int max;

    public LinearQueue(int size)
    {
        ele = new int[size];
        front = 0;
        rear = -1;
        max = size;
    }

    public void InsertItem(int item)
    {
        if (rear == max - 1)
        {
            Console.WriteLine("Queue Overflow");
            return;
        }
        else
        {
            ele[++rear] = item;
        }
    }

    public int DeleteItem()
    {
        if (front == rear + 1)
        {
            Console.WriteLine("Queue is Empty");
            return -1;
        }
        else
        {
            Console.WriteLine("Deleted element is: " + ele[front]);
            return ele[front++];
        }
    }

    public void PrintQueue()
    {
        if (front == rear + 1)
        {
            Console.WriteLine("Queue is Empty");
            return;
        }
        else
        {
            for (int i = front; i <= rear; i++)
            {
                Console.WriteLine("\tItem[" + (i + 1) + "]: " + ele[i]);
            }
        }
    }
}

class Demo
{
    static void Main()
    {
        LinearQueue Q = new LinearQueue(5);

        Q.InsertItem(101);
        Q.InsertItem(202);
        Q.InsertItem(303);
        Q.InsertItem(404);
        Q.InsertItem(505);

        Console.WriteLine("Queue Items are : ");
        Q.PrintQueue();

        Q.DeleteItem();
        Q.DeleteItem();

        Console.WriteLine("Queue Items are : ");
        Q.PrintQueue();
    }
}

Output

Queue Items are :
        Item[1]: 101
        Item[2]: 202
        Item[3]: 303
        Item[4]: 404
        Item[5]: 505
Deleted element is: 101
Deleted element is: 202
Queue Items are :
        Item[3]: 303
        Item[4]: 404
        Item[5]: 505
Press any key to continue . . .

Explanation

In the above program, we created a structure LinearQueue that contains an array of elements, front, rear, and max data members.

The LinearQueue structure contains a constructor to initialize data members and InsertItem(), DeleteItem(), and PrintQueue() method.

The InsertItem() method is used to insert an item from the rear end. The DeleteItem() method is used to delete the item from the front end, and the PrintQueue() method is used to print the Queue items on the console screen.

Now look the Demo class that contains the Main() method, The Main() method is the entry point for the program. Here we created the reference of LinearQueue structure and perform insert and delete operations.

C# Data Structure Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.