ניצור פונקציית עזר בשם IsExist אשר מקבלת תור ומספר שלם. הפונקציה מחזירה אמת אם המספר נימצא בתור, אחרת שקר. הפונקציה לא פוגעת בתור.
לפונקציה אשר פותרת את השאלה נקרא: RemoveDuplication
פתרון בשפת C#
public static bool IsExist( Queue<int> q , int n)
{
Queue<int> temp = new Queue<int>();
bool result = false;
while( !q.IsEmpty())
{
if(q.Head() ==n)
result = true;
temp.Insert(q.Remove());
}
while( !temp.IsEmpty())
{
q.Insert(temp.Remove());
}
return result;
}
public static void RemoveDuplication(Queue<int> q)
{
Queue<int> temp = new Queue<int>();
while( !q.IsEmpty())
{
int x = q.Remove();
if( ! IsExist(temp, x))
temp.Insert(x);
}
while( !temp.IsEmpty())
{
q.Insert(temp.Remove());
}
}