Program in C# on Queue with fixed array, Essays (high school) of Computer science

Program in C# on Queue with fixed array Linked List

Typology: Essays (high school)

2018/2019

Uploaded on 03/04/2022

joshua13
joshua13 🇬🇧

11 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Program in C# to demonstrate the different operations on Queue using
fixed array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Queue_with_fixed_array
{
public struct Queue // data structure for Queue
{ public int Front;
public int Rear;
public int[] items;
}
public class QueueStructure
{ Queue queue = new Queue();
public QueueStructure(int maxsize) // use of constructor
{
queue.items = new int[maxsize]; // creating an array of given size.
queue.Front = 0;
queue.Rear = -1;
}
public void Size()
{
int counter= (queue.Rear - queue.Front)+1;
Console.WriteLine("The number of jobs in the queue is {0}", counter);
}
public void Display()
{
Console.WriteLine("The items now are");
if (queue.Rear < queue.Front)
{
Console.WriteLine("Queue is Empty");
}
else
{
for (int i = queue.Front; i <= queue.Rear; i++)
{
Console.WriteLine(queue.items[i]);
}
}
}
public void Clear()
{
Console.WriteLine("The whole jobs is getting cleared in the Queue");
if (queue.Rear < queue.Front)
{
Console.WriteLine("Queue isalready Empty");
}
pf3
pf4
pf5

Partial preview of the text

Download Program in C# on Queue with fixed array and more Essays (high school) Computer science in PDF only on Docsity!

Program in C# to demonstrate the different operations on Queue using

fixed array.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace Queue_with_fixed_array { public struct Queue // data structure for Queue

{ public int Front; public int Rear; public int[] items; }

public class QueueStructure

{ Queue queue = new Queue();

public QueueStructure(int maxsize) // use of constructor { queue.items = new int[maxsize]; // creating an array of given size. queue.Front = 0; queue.Rear = -1; } public void Size() { int counter= (queue.Rear - queue.Front)+1; Console.WriteLine("The number of jobs in the queue is {0}", counter);

public void Display() { Console.WriteLine("The items now are"); if (queue.Rear < queue.Front) { Console.WriteLine("Queue is Empty"); } else { for (int i = queue.Front; i <= queue.Rear; i++) { Console.WriteLine(queue.items[i]); } } }

public void Clear() { Console.WriteLine("The whole jobs is getting cleared in the Queue"); if (queue.Rear < queue.Front) { Console.WriteLine("Queue isalready Empty"); }

else { queue.Front = 0; queue.Rear = -1; } Display(); } public int Dequeue()

{ if (queue.Front > queue.Rear) Console.WriteLine("Queue is empty"); else Console.WriteLine("The first job in the queue taken out : {0}", queue.items[queue.Front]); queue.Front++; Display(); return -1; }

public void Enqueue() {

if (queue.Rear == queue.items.Length-1) { Console.WriteLine("Queue is full and items cannot be added.."); } else { Console.Write("Enter a number to be added in Queue : "); queue.Rear++; queue.items[queue.Rear] = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enqueue done/ Job added to the queue.."); }

Display(); } }

class Program { static void Main() { char choice = 'Y'; QueueStructure p = new QueueStructure(5); // Initilising the size of Queue

p.Display(); // to initilise the Queue

int opt; // to input the choice do { Console.Write("\nHere are the options :\n"); Console.Write("1-Enqueue (Adding a data from last point).\n2-Dequeue (Taking out a data from first point).\n3-Size of the jobs in the Queue (Number of elements present).\n4-Cleary (Clear all the element of the Queue).\n5-Exit.\n"); Console.Write("\nInput your choice :"); opt = Convert.ToInt32(Console.ReadLine());

switch (opt) {

// Constructor to create a new node.Next is by default initialized as null

public Node(int d)

{

data = d;

next = null;

}

} internal class LinkListQueue

{

Node front;

Node rear;

public LinkListQueue()

{ this.front = this.rear = null; }

internal void Enqueue(int item)

{

Node newNode = new Node(item);

// If queue is empty, then new node is front and rear both

if (this.rear == null)

{

this.front = this.rear = newNode;

}

else

{

// Add the new node at the end of queue and change rear

this.rear.next = newNode;

this.rear = newNode;

}

Console.WriteLine("{0} inserted into Queue", item);

internal void Dequeue()

{

// If queue is empty, return NULL.

if (this.front == null)

{

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

return;

// Store previous front and move front one node ahead

Node temp = this.front;

this.front = this.front.next;

// If front becomes NULL, then change rear also as NULL

if (this.front == null)

{

this.rear = null;

}

Console.WriteLine("Item deleted is {0}", temp.data);

} }

class Program { static void Main(string[] args) { LinkListQueue p = new LinkListQueue(); p.Enqueue(25); p.Enqueue(12); p.Dequeue(); } }