Tuesday, November 25, 2014

Queue Lab 2



namespace WorkingWithQueues_Lab
{
    public partial class Form1 : Form
    {

        Queue<int> q1 = new Queue<int>(10);
        Queue<int> q2 = new Queue<int>(10);
        Queue<int> q3 = new Queue<int>(20);
        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        private void Display(Queue<int> q, ListBox where)
        {
            if (q != null)
            {
                where.Items.Clear();
                foreach (int i in q)
                {
                    where.Items.Add(i);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Load 1 and 2 with random numbers positive and negative.
            for (int i = 0; i < 10; i++)
            {
                q1.Enqueue(rand.Next(-9, 10));
                q2.Enqueue(rand.Next(-9, 10));
            }
            Display(q1, lsbQ1);
            Display(q2, lsbQ2);
        }

        private void btnCombineQueues_Click(object sender, EventArgs e)
        {
            if (q3 != null)
            {
                q3.Clear();
            }
            else
                q3 = new Queue<int>(20);
            if (q1.Count == 0 || q2.Count == 0)
            {
                MessageBox.Show("WARNING: At least one queue is empty");
            }

            // pull values from queue 1 and put into queue 3
            while (q1.Count > 0)
            {
                q3.Enqueue(q1.Dequeue());
            }
            // pull values from queue 2 and put into queue 3
            while (q2.Count > 0)
            {
                q3.Enqueue(q2.Dequeue());
            }
            Display(q1, lsbQ1);
            Display(q2, lsbQ2);
            Display(q3, lsbQ3);
        }

        private void btnRemoveNegs_Click(object sender, EventArgs e)
        {
            if (q3 != null)
            {
                int[] array = q3.ToArray();
                q3.Clear();


                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] > 0)
                    {
                        q3.Enqueue(array[i]);
                    }
                }
                Display(q3, lsbQ3);
            }
        }

        private void btnReverseOrder_Click(object sender, EventArgs e)
        {
            if (q3 != null)
            {
                int[] array = q3.ToArray();
                q3.Clear();
                Array.Reverse(array);
                for (int i = 0; i < array.Length; i++)
                {
                    q3.Enqueue(array[i]);
                }
                Display(q3, lsbQ3);
            }

        }
        /// <summary>
        /// Adds the values at the head of each queue to the tail of third queue. Returns the new Queue. Returns null if no values exist in either of the first Queues to add.
        /// </summary>
        /// <param name="First">The Queue you wish to use for adding values.</param>
        /// <param name="Second">The Queue you wish to use for adding values.</param>
        /// <returns>The new queue with values added, or null if no values exist in Queue one and two.</returns>
        private Queue<int> AddQueueValues(Queue<int> First, Queue<int> Second)
        {
            int QueueLength = Math.Max(First.Count, Second.Count);
            Queue<int> Third = new Queue<int>(QueueLength);
            int q1Value;
            int q2Value;

            while (First.Count > 0 || Second.Count > 0)
            {
                if (First.Count > 0)
                    q1Value = First.Dequeue();
                else
                    q1Value = 0;

                if (Second.Count > 0)
                    q2Value = Second.Dequeue();
                else
                    q2Value = 0;

                Third.Enqueue(q1Value + q2Value);
            }

            if (Third.Count > 0)
            {
                return Third;
            }
            return null;
        }

        private void btnAddQueueValues_Click(object sender, EventArgs e)
        {
            if (q3 != null)
            {
                q3.Clear();
            }
           
            q3 = AddQueueValues(q1, q2);
            if (q3 == null)
            {
                MessageBox.Show("Queue 3 is null because Queue 1 and 2 were empty");
                lsbQ3.Items.Clear();
            }
            Display(q3, lsbQ3);
            if (lsbQ1.Items.Count > 0 || lsbQ2.Items.Count > 0)
            {
                MessageBox.Show("Queue 1 and 2 will be cleared. Visually verify that the numbers were added correctly from listbox 1 and 2 to listbox 3.\nPress Ok to clear and continue.");
                lsbQ1.Items.Clear();
                lsbQ2.Items.Clear();          
            }          
        }

        private void btnRepopQ1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                q1.Enqueue(rand.Next(-9, 10));
            }
            Display(q1, lsbQ1);
        }

        private void btnRepopQ2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                q2.Enqueue(rand.Next(-9, 10));
            }
            Display(q2, lsbQ2);
        }
    }
}
/// Lab:
///
/// 1. Create and Populate two Queues. Merge both into a single third Queue. Display all 3 Queues
///
/// 2. Create and populate a queue with positive and negative numbers.
///     Define a method that:
///         Removes all the negative values from the queue while maintaining only the positives without changing their order. Test it and display.
///
/// 3. Define a method that reverses the order of a queue. Test it/Display
///
/// 4. Define a method that takes two queues and returns a single queue.
///     queue should hold the digits of a large number (example: 980345699 - Queue should contain 9,9,6,5,4,3,0,8,9 (read backward)
///    
///     Your code should add each digit from queue 1 and 2, put the result in queue 3 such that the number in queue 3 should be the sum of the 2 initial numbers

No comments:

Post a Comment