Friday, April 8, 2011

have differance two queue

how to update new queue to Queue3

Queue Getupdate(Queue q1,Queue q2) { How... }

From stackoverflow
  • The question is unclear; what is your desired result? The union? The intersection? What? If you want ("differance" [sic] in title) to return those in the original queue but not the second, perhaps (.NET 3.5):

    public static Queue<T> Except<T>(this Queue<T> original, Queue<T> remove)
    {
        return new Queue<T>(Enumerable.Except<T>(original,remove));
    }
    
  • using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestQC
    {
        class Program
        {
            static void Main(string[] args)
            {
                Queue q1 = new Queue();
                Queue q2 = new Queue();
                Queue q3 = new Queue();
                q1.Enqueue(1);
                q1.Enqueue(2);
                q1.Enqueue(3);
                q1.Enqueue(4);
                q2.Enqueue(1);
                q2.Enqueue(2);
                q2.Enqueue(3);
                q2.Enqueue(4);
                q2.Enqueue(5);
                q2.Enqueue(6);
                Console.Write("Q1: ");
                foreach (var o in q1)
                {
                    Console.Write(o.ToString()+ " ");
                }
                Console.WriteLine();
                Console.Write("Q2: ");
                foreach (var o in q2)
                {
                    Console.Write(o.ToString() + " ");
                }
                Console.WriteLine();
                q3 = GetQUpdate(q1, q2);
                Console.Write("Q3: ");
                foreach (var o in q3)
                {
                    Console.Write(o.ToString() + " ");
                }
    
                Console.ReadLine();
            }
            static Queue GetQUpdate(Queue q1, Queue q2)
            {
                Queue q3 = new Queue();
    
                return q3;
            }
        }
    }
    

    I am testing Function For Implement

    Marc Gravell : Yes, but what do you want the result to be? 5,6? then just use the code I posted earlier (perhaps swap the order - q3 = q2.Except(q1);)
    Marc Gravell : Note that using queues in this way is unusual; would a list / etc be more appropriate?
    Brian Rasmussen : You should edit your original question instead of posting updates in an answer.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.