Tuesday, November 25, 2014
Queue Lab
namespace Zau_Queue_Lab_11_13_14
{
public partial class Form1 : Form
{
Queue<int> intqueue1 = new Queue<int>(16);
Queue<int> intqueue2 = new Queue<int>(16);
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btnEnqueue1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
//get a random value
int num = rand.Next(110000000, 999999999);
//add it to the back (tail) of the intqueue
intqueue1.Enqueue(num);
//Display queue
Display1(intqueue1);
}
private void Display1(Queue<int> queue)
{
listBox1.Items.Clear();
foreach (int num in queue)
{
listBox1.Items.Add(num);
}
}
private void Display2(Queue<int> queue)
{
listBox2.Items.Clear();
foreach (int num in queue)
{
listBox2.Items.Add(num);
}
}
private void btnEnqueue2_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
//get a random value
int num = rand.Next(110000000, 999999999);
//add it to the back (tail) of the intqueue
intqueue2.Enqueue(num);
//Display queue
Display2(intqueue2);
}
private void btnDisplayQ1andQ2_Click(object sender, EventArgs e)
{
if (intqueue1.Count > 0 && intqueue2.Count > 0)
{
listBox3.Items.Clear();
int[] a = CopyQueueToArray(intqueue1);
int[] b = CopyQueueToArray(intqueue2);
int[] c = new int[a.Length + b.Length];
a.CopyTo(c, 0);
b.CopyTo(c, a.Length);
Queue<int> intqueue3 = new Queue<int>(c);
foreach (int num in intqueue3)
{
listBox3.Items.Add(num);
}
}
else
MessageBox.Show("Sorry!..one of the queues is empty or both");
}
private int[] CopyQueueToArray(Queue<int> queue)
{
int[] array = new int[queue.Count];
queue.CopyTo(array, 0);
return array;
}
private void btnEnqueueWithPosNNeg_Click(object sender, EventArgs e)
{
ClearListBoxes();
//get a random value
int num = rand.Next(-20, 20);
//add it to the back (tail) of the intqueue
intqueue1.Enqueue(num);
//Display queue
Display1(intqueue1);
}
private void btnRemoveNegNum_Click(object sender, EventArgs e)
{
if (intqueue1.Count > 0 )
{
listBox3.Items.Clear();
int[] a = CopyQueueToArray(intqueue1);
int[] c = new int[a.Length];
a.CopyTo(c, 0);
Queue<int> intqueue3 = new Queue<int>(c);
listBox3.Items.Clear();
foreach (int num in intqueue3)
{
if (num > 0)
{
listBox3.Items.Add(num);
}
}
}
else
MessageBox.Show("Sorry!..one of the queues is empty or both");
}
private void ClearListBoxes()
{
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
}
private void btnClearAllListBoxes_Click(object sender, EventArgs e)
{
intqueue1.Clear();
intqueue2.Clear();
ClearListBoxes();
}
private void btnReverseQueue2_Click(object sender, EventArgs e)
{
int[] arr = CopyQueueToArray(intqueue2);
Array.Reverse(arr);
Queue<int> intqueueReverse = new Queue<int>(arr);
foreach(int num in intqueueReverse)
{
listBox3.Items.Add(num);
}
}
private void btnAddQ1andQ2_Click(object sender, EventArgs e)
{
if (intqueue1.Count > 0 && intqueue2.Count > 0)
{
Queue<int> addedQueue = new Queue<int>();
addedQueue = AddValues(intqueue1, intqueue2);
foreach (int num in addedQueue)
{
listBox3.Items.Add(num);
}
}
else
MessageBox.Show("You need to create two queues in order to add them.");
}
private Queue<int> AddValues(Queue<int> q1, Queue<int> q2)
{
int v1, v2;
Queue<int> q3 = new Queue<int>();
while (q1.Count > 0 || q2.Count > 0)
{
if (q1.Count > 0)
v1 = q1.Dequeue();
else
v1 = 0;
if (q2.Count > 0)
v2 = q2.Dequeue();
else
v2 = 0;
q3.Enqueue(v1 + v2);
}
return q3;
}
}
}
//1. Create and populate 2 queues (queue1, queue2).
// Merge both queues into a single third queue. Display all 3
//2. Create and populate a queue with positive and negative numbers.
// Define a method that:
// Find a way to remove all the negative values from the queue while maintaining only the positives
// without changing their orders. Test it and display result
//3. Define a method that reverse the order of a queue. Test it and display result.
//4. Define a method that takes 2 queues (queue1, queue2) and return a queue.
// Queue should hold the digits of a large number (example number 980345699,
// a queue should contain 9, 9, 6, 5, 4, 3, 0, 8, 9 (read backward) (from smaller weight to the larger weight))
// http://www.dotnetperls.com/queue
//
// Your code is to add each digit from queue1, queue2, put result in queue3
// such that number in queue3 should be the sum of the 2 initial numbers
// Test it and display all queues.
Labels:
CSI-155
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment