Circular queue implementation. Here, we are implementing circular queue using array. Submitted by IncludeHelp



Download 21,54 Kb.
Sana13.07.2022
Hajmi21,54 Kb.
#789616
Bog'liq
C


C# program to implement circular queue using array
In this C# program, we will learn about circular queue implementation. Here, we are implementing circular queue using array.
Submitted by IncludeHelp, on November 21, 2017
Circular Queue follows FIFO (First In First Out) property, it means first inserted elements, deleted first. In Circular queue there are two pointers are used:

  1. FRONT : It points the location from where we can delete an item from circular queue.

  2. REAR : It points the location from where we can insert element into circular queue.

In Circular queue, we remove problem of linear queue, In Linear queue we cannot insert element at deleted location because front is moved ahead after deletion.
Here (by using circular queue), we move rear and front pointers circularly, if front or rear reached at end of array, than it moves to 0th location of array.
Circular queue implementation using array in C#
using System;


namespace ConsoleApplication1
{
class CircularQueue
{
private int []ele;
private int front;
private int rear;
private int max;
private int count;


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


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

count++;
}


}


public void delete()
{
if(count == 0)
{
Console.WriteLine("Queue is Empty");
}
else
{
Console.WriteLine("deleted element is: " + ele[front]);

front = (front + 1) % max;


count--;
}


}


public void printQueue()
{
int i = 0;
int j = 0;


if (count==0)
{
Console.WriteLine("Queue is Empty");
return;
}
else
{
for (i = front;j{
Console.WriteLine("Item[" + (i + 1) + "]: " + ele[i]);

i = (i+1)%max;


j++;
}
}
}
}


class Program
{
static void Main()
{
CircularQueue Q = new CircularQueue(5);

Q.insert(10);


Q.insert(20);
Q.insert(30);
Q.insert(40);
Q.insert(50);

Console.WriteLine("Items are : ");


Q.printQueue();
Q.delete();
Q.delete();

Q.insert(60);


Q.insert(70);

Console.WriteLine("Items are : ");


Q.printQueue();

}
}
}


Output
Items are :
Item[1]: 10
Item[2]: 20
Item[3]: 30
Item[4]: 40
Item[5]: 50
deleted element is: 10
deleted element is: 20
Items are :
Item[3]: 30
Item[4]: 40
Item[5]: 50
Item[1]: 60
Item[2]: 70
C# Data Structure Programs »

Items are :


Items[1] : 10
Items[2] : 20
Items[3] : 30
Items[4] :40
Items[5]: 50
Deleted element is : 10
Deleted element is : 20
Items[3]: 30
Items[4]: 40
Items[5]: 50
Items[1]: 60
Items[2]: 70
Download 21,54 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish