Q:

C# program to implement Linear Queue using structure

0

C# program to implement Linear Queue using structure

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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, frontrear, 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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Data Structure Solved Programs/Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to implement circular queue using array... >>
<< C# program to implement linear queue using array...