כתוב פעולה המקבלת שני תורים המכילים מספרים שלמים. הפעולה תחזיר אמת אם התור הראשון מוכל כולו: באותו אורך ובאותו סדר - בתור השני, ותחזיר שקר אם לא.

פתרון בשפת C#
ראשית נבנה פעולת עזר אשר מקבלת תור וסופרת כמה איברים יש בו.

public static int Size(Queue<int> q)

{

        Queue<int> temp = new Queue<int>();

        int counter=0;

        while ( ! q.IsEmpty())

        {

                temp.Insert(q.Remove());

                counter++;

        }

        while ( ! temp.IsEmpty())

                q.Insert(temp.Remove());

        return counter;

        

}

נבנה פונקציה אשר מקבלת תור של מספרים שלמים ומחזירה שכפול שלה

public static int Duplicate(Queue<int> q)

{

        Queue<int> temp = new Queue<int>();

        Queue<int> d= new Queue<int>();

        while ( ! q.IsEmpty())

        {

                int value = q.Remove();

                d.Insert(value)

                temp.Insert(value);

        }

        while ( ! temp.IsEmpty())

                q.Insert(temp.Remove());

        return d;

        

}

נבנה פונקציה אשר מקבלת שני תורים ובודקת האם הם שווים

public static bool IsSame(Queue<int> q1, Queue<int>q2 )

{

        

        Queue<int> d1 = Duplicate(q1);

        Queue<int> d2 = Duplicate(q2);

        while(!d1.IsEmpty() && !d2.IsEmpty() )

        {

                if( d1.Head() != d2.Head())

                        return false;

                

                d1.Remove();

                d2.Remove();

        }

        return d1.IsEmpty() && d2.IsEmpty();        

        

}

פתרון השאלה עצמה:

//check if q2 contains q1

public static bool Contains (  Queue<int> q1 , Queue<int> q2  )

{

        int size1 = Size(q1);

        int size2 = Size(q2);

        if (size1 > size2)

                return false;

        Queue<int> d2 = Duplicate(q2);

        Queue<int> temp = new Queue<int>();

        

        for(int i=0;i <size1-1 ; i++)

                temp.Insert(d2.Remove());

        

        int lives =2 ;

        while ( live!=0 )

{

        if(  IsSame(q1, temp))

                return true;

        if( !d2.IsEmpty())

        {

temp.Remove();

        temp.Insert(d2.Remove());

        

        }

if( d2.IsEmpty())

{

lives–;

}

}

return false;

}

סעיף ב בשפת C#

public static Queue<int> Common (Queue<int> q1, Queue<int> q2)

{

        Queue<int> result = new Queue<int>();

        while( !q1.IsEmpty() && !q2.IsEmpty() )

        {

                if( q1.Head() == q2.Head() )

                {

                        result.Insert(q1.Remove());

                        q2.Remove();

                }

                if( q1.Head() < q2.Head() )

                        q1.Remove());

                else

                        q2.Remove();

        }

        return result;

}