Tuesday, November 25, 2014

Generic Collection _ Queue


namespace The_Queue_Collection
{

    public partial class Form1 : Form
    {
        Queue<int> intqueue = new Queue<int>(16); //You can set capacity 16, 32, 64....

        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        //helper method... Display queue
        private void Display(Queue<int> queue)
        {
            listBox1.Items.Clear();
            foreach (int num in queue)
            {
                listBox1.Items.Add(num);
            }
        }

        private void btnEnqueue_Click(object sender, EventArgs e)
        {
            //get a random value
            int num = rand.Next(100);
            //add it to the back (tail) of the intqueue
            intqueue.Enqueue(num);
            //Display queue
            Display(intqueue);
        }

        private void btnDequeue_Click(object sender, EventArgs e)
        {
            //use the Dequeue method to remove the first item in the queue
            //the Dequeue method returns the item removed
            if (intqueue.Count > 0)
            {
                int num = intqueue.Dequeue();
                //process the number num
                MessageBox.Show(String.Format("{0} was removed", num));
                Display(intqueue);
            }
            else
                MessageBox.Show("Sorry!.. the queue is empty");
        }

        private void btnPeek_Click(object sender, EventArgs e)
        {
            //This Peek method returns the item at the head of the queue
            //without removing it.
            //Who is on first?
            //make sure to check that the queue is not empty to avoid an
            //InvalidOeprationException from the system
            if (intqueue.Count > 0)
            {
                int num = intqueue.Peek();
                MessageBox.Show(String.Format("{0} is on first", num));

                //display to prove that the Peek does not remove an item
                Display(intqueue);
            }
            else
                MessageBox.Show("Sorry!.. the queue is empty");

        }

        private void btnCopyToArray_Click(object sender, EventArgs e)
        {
            //Use the copyTo method to copy the content of a queue to an array.
           // int array

            int[] a = CopyQueueToArray(intqueue);

            //display array ti a nessage box
            StringBuilder sb = new StringBuilder();
            foreach (int num in a)
            {
                sb.Append(num);
                sb.AppendLine();

            }
            MessageBox.Show(sb.ToString());

        }
        private int[] CopyQueueToArray(Queue<int> queue)
        {
            int[] array = new int[queue.Count];
            queue.CopyTo(array, 0);
            return array;
        }

    }
}
//Research keyword for generic collection
//[ .net generic collections ]
//Enqueue method //add to the line
//Dequeue method //Romove from the line
//Peek method // look into the line
//Example Use: Printing
//===========================================
//Lab Assignment due Friday Nov 14
//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))
//    
//      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.

No comments:

Post a Comment