במסגרת פרויקט עיר ירוקה הוחלט לחשב את פחי האשפה בעיר. לשם כך הוגדרה המחלקה "פח אשפה" (Garbage). להלן תכונות המחלקה:

תכונה 1: מספר הפח מסוג מספרי
תכונה 2: מיקום הפח (location) מסוג מחרוזת
תכונה 3: קיבולת מקסימלית בק"ג (capacity) מסוג מספרי
תכונה 4: כמות ק"ג נוכחית בפח (quantity) מסוג מספרי
תכונה 5: שכונה (neighborhood) מסוג מחרוזת

כמו כן הוגדרו פעולות get לכל הנתונים:

סעיף 1: כתבו פעולה חיצונית בוליאנית המקבלת רשימה של פחי אשפה ומחזירה רשימה של פחים שיש בהם ריקון. יש לרוקן פח רק אם הוא מכיל לפחות 80% מקיבולת המקסימלית

סעיף 2: הרשימה שחוזרת צריכה לכלול את שם השכונה ולהחזיר רשימה של פחים בשכונה מסוימת

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

סעיף 4: כתבו פעולה חיצונית המחזירה רשימה של פחים עם קיבולת קטנה מ-1000 ק"ג

סעיף 1 פתרון בשפת C#

public static Node<Garbage> Empty(Node<Garbage> lst)

{

Node<Garbage> result = new Node<Garbage>(new Garbage("1","1",1.1."1"),null);

Node<Garbage> tail = result;

Node<Garbage> p = lst;

while(p!=null)

{

if(p.GetValue().GetQuantity() / p.GetValue().GetCapacity() >= 0.80)

{

        // tail is the last

        Node<Garbage> newNode = new Node<Garbage>(p.GetValue() ,null) );

        

        //tail is one before the last

        tail.SetNext(newNode);

        

        // tail is last

        tail=tail.GetNext();

        

}

        

        p=p.GetNext();

}

return result.GetNext();

}

סעיף 2 פתרון בשפת C#

public static Node<Garbage> SameNeighborhood (Node<Garbage> lst , string neighborhood)

{

Node<Garbage> result = new Node<Garbage>(new Garbage("1","1",1.1."1"),null);

Node<Garbage> tail = result;

Node<Garbage> p = lst;

while(p!=null)

{

if(p.GetValue().GetNeighborhood().Equals(neighborhood))

{

        // tail is the last

        Node<Garbage> newNode = new Node<Garbage>(p.GetValue() ,null) );

        

        //tail is one before the last

        tail.SetNext(newNode);

        

        // tail is last

        tail=tail.GetNext();

        

}

        

        p=p.GetNext();

}

return result.GetNext();

}

סעיף 3

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

public static int Size(Node<Garbage> lst)

{

int counter=0;

Node<Garbage> p = lst;

while(p!=null)

{

counter++;

p=p.GetNext();

}

return counter;

}

public static string MaxGarbage(Node<Garbage> lst)

{

Node<Garbage> p = lst;

string neighborhoodM = p.GetValue().GetNeighborhood();

Node<Garbage> result = SameNeighborhood (neighborhoodM );

int max= Size(result);

while(p!=null)

{

        string neighborhoodName = p.GetValue().GetNeighborhood();

Node<Garbage> r= SameNeighborhood (neighborhood );

        int size = Size(result);

        if(size>max)

        {

                max = size;

                neighborhoodM  =neighborhoodName ;

        }

        

p = p.GetNext();

}

return neighborhoodM ;

}

סעיף 4  פתרון בשפת C#

public static Node<Garbage> LessCapacity(Node<Garbage> lst)

{

Node<Garbage> result = new Node<Garbage>(new Garbage("1","1",1.1."1"),null);

Node<Garbage> tail = result;

Node<Garbage> p = lst;

while(p!=null)

{

if(p.GetValue().GetCapacity <1000 )

{

        // tail is the last

        Node<Garbage> newNode = new Node<Garbage>(p.GetValue() ,null) );

        

        //tail is one before the last

        tail.SetNext(newNode);

        

        // tail is last

        tail=tail.GetNext();

        

}

        

        p=p.GetNext();

}

return result.GetNext();

}