國立暨南國際大學 101 學年度第一學期小考試卷

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2013.1.11
系所別:
年級:
學號:
姓名:
考試時間 08:10-08:20

  1. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Traverse a linked-list
    #include <iostream>

    struct ListElement
    {
        int value;
        ListElement* pNext;
    };

    void Print(ListElement* p)
    {
        while (p != NULL)
        {
            std::cout << p->value;
            p = p->pNext;
        }
        std::cout << std::endl;
    }

    int main()
    {
        ListElement LE1 = { 1, NULL };
        ListElement LE2 = { 4, NULL };
        ListElement LE3 = { 3, NULL };
        ListElement LE4 = { 2, NULL };

        LE1.pNext = &LE4;
        LE3.pNext = &LE2;
        LE4.pNext = &LE3;

        Print(&LE1);
        return 0;
    }


  2. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Insert a node into a linked-list
    #include <iostream>

    struct ListElement
    {
        int value;
        ListElement* pNext;
    };

    void Print(ListElement* p)
    {
        while (p != NULL)
        {
            std::cout << p->value;
            p = p->pNext;
        }
        std::cout << std::endl;
    }

    int main()
    {    
        ListElement LE2 = { 4, NULL };
        ListElement LE3 = { 3, &LE2 };
        Print(&LE3);

        ListElement* p = new ListElement;
        p->value = 8; p->pNext = LE3.pNext;
        LE3.pNext = p;
        Print(&LE3);
        return 0;
    }