Queue implementation in c# through array in DataStructures

No.of Views1622
Bookmarked0 times
Downloads 
Votes0
By  poly666   On  16 Feb 2010 00:02:46
Tag : CSharp , Miscellaneous
Queue implementation in c# through array in DataStructures
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

C# Code

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication46

{
class Program

{
/*first creating an array of size 5*/

int[] arr = new int[5];

/*declaring rear for insertion n front for deletion in a queue

for example-array is 5 in size

* here 0 memory location is rear end 0 memory location  is front

so when we start inserting we will increment rear by 1

* everytime we enter a value in array till array becomes full

and similary for deletion we will increment front by 1 everytime we delete an

* element from the array*/

int rear = -1;

int front = -1;

/*creating insertion method that will insert value in array*/

public void insert(int value)

{
/*we r checking if array is empty or not*/

if (rear == -1)
{
/*insertion starts so will increment both rear n front by 1*/

front=0;

rear++;

/*now we will store the int variable in array at rear position */

arr[rear] = value;

}

else

{

/*here the array is not empty so

* we are entering value in arrays next memory space

by incrementing the rear*/

rear++;

arr[rear] = value;             

}

Console.WriteLine("Data has been entered");

Console.ReadLine();

}

/*this method will start deleting array elements from 0 to 4*/

public void delete()

{

/*again checking if array is empty */

if (front == -1)

{

Console.WriteLine("Queue is already empty");

}

else

{

Console.WriteLine("the element deleted is ={0}",arr[front]);

Console.ReadLine();

front++;

}

}

public void show_elements_entered()

{

/*Checking if queue is empty*/

if (rear == -1)

{

Console.WriteLine("Queue is empty");

Console.ReadLine();

}

else

{

Console.WriteLine("Elements entered by u are as follows");

for (int i = 0; i<5; i++)

{

Console.Write("<" + (i + 1) + ">");

Console.Write(arr[i]);

Console.ReadLine();

}



}

}

/*now this method will show remaning array elements after deletion*/

public void display_after_deletion()

{

Console.WriteLine("Elements after deletion");

/*in this i = front caze front is used for deletion*/

for (int i = front; i < 5; i++)

{

Console.Write(arr[i]);

Console.ReadLine();

}

}



static void Main(string[] args)

{

Program obj = new Program();

/*here i hv created a menu*/

do

{

Console.Clear();

Console.WriteLine("************************");

Console.WriteLine("******* Main Menu*******");

Console.WriteLine("1.insert                ");

Console.WriteLine("2.Delete                ");

Console.WriteLine("3.Display               ");

Console.WriteLine("4.array after deletion  ");

Console.WriteLine("************************");

Console.WriteLine(" Enter ur choice");

int choice = int.Parse(Console.ReadLine());

/*applying switch case*/

switch (choice)

{

case 1:

{

/*taking value from user*/

Console.WriteLine("Enter a number");

int y = int.Parse(Console.ReadLine());

obj.insert(y);

break;

}

case 2: { obj.delete(); break; }

case 3: { obj.show_elements_entered(); break; }

case 4: { obj.display_after_deletion(); break; }

default: { Console.WriteLine("Invalid"); break; }

}

} while (true);

/*and this is how its done*/



}

}

}
{/codecitation}

Thank you

Bhanfari


 
Sign Up to vote for this article
 
About Author
 
poly666
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-15 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top