אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2021 · קיץ מועד א · שאלה 8כל שאלות המבחן ←
מבנים לינאריים›רשימות מקושרות

נתונה המחלקה IntList הבאה:

public class IntList {
    private Node<int> head;
    public IntList( ) {
        head = null;
    }
    public void Add(int a) {
       head = new Node<int> (a, head);
    }
    public override string ToString(){
        string s = "{";
        Node<int> h = head;
        while(h.GetNext()!= null){
            s += h.GetValue()+",";
            h = h.GetNext();
        }
        return s + h.GetValue()+"}";
    }
    public  bool What1 (IntList list) {
        Node<int> h1 = head;
        Node<int> h2 = list.head;
        while ((h1 != null) && (h2 != null))  {
            if (h1.GetValue()!= h2.GetValue())
                return false;
            h1 = h1.GetNext();
            h2 = h2.GetNext();
        }
        return true;
    }
    public bool What2 (IntList list) {
        Node<int> h1 = head;
        while (h1 != null) {
            bool found = false;
            Node<int> h2 = list.head;
            while ((h2 != null) && (!found)) {
                if (h1.GetValue()== h2.GetValue())
                    found = true;
                h2 = h2.GetNext();
            }
            if (!found)
                return false;
            h1 = h1.GetNext();
        }
        return true;
    }
}
סעיף א

נתון קטע קוד של הפעולה main במחלקה Program: IntList testList = new IntList(); testList.Add(2); testList.Add(3); testList.Add(6); testList.Add(1); testList.Add(4); Console.WriteLine(testList); מהו הפלט של קטע הקוד (אין צורך במעקב מפורט)?

סעיף ב

בסעיף א' נוצר העצם testList בזימונים testList.Add(2); testList.Add(3); testList.Add(6); testList.Add(1); testList.Add(4);

בעבור העצם testList שנוצר בסעיף א', הביאו דוגמה של עצם אחר מטיפוס IntList בשם list, הכולל ארבעה איברים לפחות, כך ש:

  1. זימון testList.What1(list) יחזיר true והזימון testList.What2(list) יחזיר true. אם אי אפשר להביא דוגמה כזו, ציינו זאת והסבירו מדוע.
  2. זימון testList.What1(list) יחזיר true והזימון testList.What2(list) יחזיר false. אם אי אפשר להביא דוגמה כזו, ציינו זאת והסבירו מדוע.
  3. זימון testList.What1(list) יחזיר false והזימון testList.What2(list) יחזיר true. אם אי אפשר להביא דוגמה כזו, ציינו זאת והסבירו מדוע.
  4. זימון testList.What1(list) יחזיר false והזימון testList.What2(list) יחזיר false. אם אי אפשר להביא דוגמה כזו, ציינו זאת והסבירו מדוע.
סעיף ג

מה מבצעות הפעולות What1 ו-What2 באופן כללי?

שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי