שאלון בגרות מדעי המחשב, קיץ תשע"ו, מועד ב, מס' 899...

שאלון בגרות מדעי המחשב, קיץ תשע"ו, מועד ב, מס' 899381 פתרון מלא

שאלה מספר 4

פתרון בשפת C# סי שארפ
תחילה נבצע פעולת עזר אשר סופרת את כמות החוליות ברשימה:
public static int Size ( Node<int> lst)
{
int counter=0;
while(lst!=null)
{
counter++;
lst = lst.GetNext();
}
return counter;
}

public static bool IsTripple(Node<int> lst)
{
int size = Size(lst);
if(size%3!=0)
return false;
//first jump size/3
//second jump (size/3)*2
Node<int> p1 = lst;
Node<int> p2 = lst;
Node<int> p3 = lst;
for(int i=0; i<size/3;i++)
p2=p2.getNext();

for(int i=0; i<(size/3)*2;i++)
p3=p3.getNext();

while(p3!=null)
{
if(p1.GetValue()!=p2.GetValue() ||p1.GetValue()!=p3.GetValue() || p2.GetValue()!=p3.GetValue() )
return false;

p1 = p1.getNext();
p2 = p2.getNext();
p3 = p3.getNext();
}
return true;
}