IN C#
The fun for today is to build your own circular array to support a queue.
The challenge here is in adjusting your queueFront and queueRear markers (they are just integer array indices), and dealing with what happens when they wrap around the First/last element of the array.
The tricky part here is worrying about the edge cases.
There are only two methods you need to fix up
addBack – this should correctly add an item at the rear of the queue without overwriting any other elements, and then correctly update the queueRear Index
removeFront should remove the first element of the Array (note that I give you code on how to a basic case)
The advanced problem is to figure out when and how to grow the array, if at all.
Here is the code:
public class CircularArray<T>
{
private T[] array;
private int queueFront;
private int queueRear;
// Basic Constructor
// Creates an array and initializes the front and rear
// O(1) in time O (N) in size
public CircularArray(int size)
{
array = new T[size + 1];
queueFront = 0;
queueRear = 0;
}
// NYI fully.
public void addBack(T value) //addBack is enqueue
{
array[queueRear] = value;
queueRear++;// Also inadequate
}
//Also NYI fully
// Note that I've made it return the value being removed, that's
not strictly required but makes the most sense.
public T removeFront() //removeFront is dequeue
{
//Right now calling this on an empty queue will crash
T sample = array[queueFront];
// Because technically T could be not nullable we need to use
default(T), which is null.
array[queueFront] = default(T);
queueFront++; // this is inadequate, need to watch wraparound
return sample;
}
// Just returns the front element O(1)
public T getFront()
{
//Calling this on an empty queue will probably crash too
return array[queueFront];
}
// Same old Grow, bit hard to know where to use it if at all
though...
// O(N)
public void grow(int newsize)
{
Array.Resize(ref array, newsize);
}
}
class Program
{
static void Main(string[] args)
{
CircularArray<String> Demo;
int numelemnts = 10;
Demo = new CircularArray<String>(numelemnts);
Demo.addBack("Richard");
String deleted = Demo.removeFront();
Console.WriteLine("The following was just removed: " +
deleted);
//Demo.removeFront(); works by itself on a line, the result just
doesn't go anywhere.
Demo.addBack("Brian");
Demo.addBack("Bonnie");
Demo.addBack("Sabine");
Demo.addBack("Jamie");
Demo.addBack("Wenying");
Demo.addBack("Omar");
Console.WriteLine(Demo.getFront());
Console.ReadLine();
}
}
using System;
class MainClass {
public static void Main (string[] args) {
CircularArray<String> Demo;
int numelemnts = 3;
Demo = new CircularArray<String>(numelemnts);
Demo.addBack("Richard");
String deleted = Demo.removeFront();
Console.WriteLine("The following was just removed: " + deleted);
//Demo.removeFront(); works by itself on a line, the result just doesn't go anywhere.
Demo.addBack("Brian");
Demo.addBack("Bonnie");
Console.WriteLine(Demo.removeFront());
Console.WriteLine(Demo.removeFront());
Demo.addBack("Sabine");
Demo.addBack("Jamie");
Demo.addBack("Wenying");
Demo.addBack("Omar");
Console.WriteLine(Demo.removeFront());
Console.WriteLine(Demo.removeFront());
Console.WriteLine(Demo.removeFront());
Console.WriteLine(Demo.removeFront());
}
}
///////////////////////////////////////////////
using System;
public class CircularArray<T>
{
private T[] array;
private int queueFront;
private int queueRear;
// Basic Constructor
// Creates an array and initializes the front and rear
// O(1) in time O (N) in size
public CircularArray(int size)
{
array = new T[size];
queueFront = -1;
queueRear = -1;
}
public void addBack(T value) //addBack is enqueue
{
int size = array.Length;
if ((queueFront == 0 && queueRear == size-1) ||
(queueRear == (queueFront-1)%(size-1))) {
//Queue is Full
grow(2 * size);
addBack(value);
return;
} else if(queueFront == -1) {
// empty queue
queueFront = queueRear = 0;
array[queueRear] = value;
} else if(queueRear == size-1 && queueFront != 0) {
queueRear = 0;
array[queueRear] = value;
} else {
queueRear += 1;
array[queueRear] = value;
}
}
//Also NYI fully
// Note that I've made it return the value being removed, that's not strictly required but makes the most sense.
public T removeFront() //removeFront is dequeue
{
if(queueFront == -1) {
return default(T);
}
T data = array[queueFront];
if(queueFront == queueRear) {
// single element
queueFront = -1;
queueRear = -1;
} else if (queueFront == array.Length-1) {
queueFront = 0;
} else {
queueFront++;
}
return data;
}
// Just returns the front element O(1)
public T getFront()
{
if(queueFront != -1) {
return array[queueFront];
} else {
return default(T);
}
}
// Same old Grow, bit hard to know where to use it if at all though...
// O(N)
public void grow(int newsize)
{
T[] newArray = new T[newsize];
int count = 0;
if (queueRear >= queueFront) {
for (int i = queueFront; i <= queueRear; i++) {
newArray[count++] = array[i];
}
} else {
for (int i = queueFront; i < array.Length; i++) {
newArray[count++] = array[i];
}
for (int i = 0; i <= queueRear; i++) {
newArray[count++] = array[i];
}
}
if(count == 0) {
queueFront = queueRear = -1;
} else {
queueFront = 0;
queueRear = count - 1;
}
array = newArray;
}
}
Hi. please find the answer
above.. i have given comments so that it is very easy for you to
understand the flow. In case of any doubts, please ask in comments.
If the answer helps you, please upvote. Thanks!
IN C# The fun for today is to build your own circular array to support a...
I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...
Build and use your own minimal queue class using an array of type String of fixed size to hold the queue. The array should have the default size of 5. The array size should be setable via a constructor. The following methods must be implemented: enqueue – inserts a value at the rear of the queue dequeue – returns the value at the front of the queue, removing the value from the queue isEmpty – returns true if the queue...
public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info; this.link = null; } public CharNode(char c, CharNode link) { this.info = info; this.link = link; } public CharNode getLink() { return link; } public void setLink(CharNode link) { this.link = link; } public char getInfo() { return info; } public void setInfo(char info) { this.info = info; } } //Remember Queue is a first-in-first-out data structure public class CharQueue { // front is...
Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...
//Look for** to complete this program --C+ please --please add comments // using namespace std; #include <iostream> #include <stdlib.h> #include < string> #include "queue.h" //Purpose of the program: ** Algorithm: * int main() /** "A", "B", "C" in the queue //** while loop -- indefinitely { try {//** catches } //* end of loop } |/ II II II II II //File type: ** queue.cpp using namespace std; #include <iostream> #include "queue.h" // constructor queue::queue () } //destructor queue::~queue() queue::queue...
(C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
+array:int[] This is provided to facilitate testing and grading. Use it as your internal array. +size:int i.e. after items are added to the array, or deleted from it. One default constructor (receives nothing and initializes an array of size 0), and another constructor that receives an int[] and initializes the internal array with the input parameter. both of the constructors exist and work fine. In initializing the internal array with the input parameter, you should make a copy of the...
Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...
Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...