國立暨南國際大學 101 學年度第二學期小考試卷
(考試時間: 14:10-14:20)
科目名稱:程式設計 |
開課系所:資訊工程
學系 |
任課教師
|
吳坤熹
|
系所別:
|
年級:
|
學號:
|
姓名:
|
考試日期
|
2013.4.23
|
- (10%) Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
// Linked-list
#include <iostream>
using std::cout;
class CList
{
public:
class CNode
{
public:
int value;
CNode* next;
CNode(int v, CNode* p = NULL) : value(v), next(p) {}
};
CNode* front;
CNode* back;
CList()
{
front = back = NULL;
}
CList(int v)
{
front = back = new CNode(v);
}
~CList()
{
cout << "Destroying the object with value "
<< front->value << std::endl;
}
void Print()
{
CNode* p;
for (p=front; p!=NULL; p=p->next)
cout << p->value << " -> ";
cout << "NIL \n";
}
void push_front(int v)
{
front = new CNode(v, front);
}
};
int main()
{
CList a(3);
a.push_front(5);
a.push_front(7);
a.Print();
return 0;
}
- (10%) Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
// Recursive object destruction
#include <iostream>
using std::cout;
class CList
{
public:
class CNode
{
public:
int value;
CNode* next;
CNode(int v, CNode* p = NULL) : value(v), next(p) {}
~CNode()
{
if (next)
delete next;
cout << "Destroying " << value << std::endl;
}
};
CNode* front;
CNode* back;
CList()
{
front = back = NULL;
}
CList(int v)
{
front = back = new CNode(v);
}
~CList()
{
if (front != NULL)
delete front;
}
void push_front(int v)
{
front = new CNode(v, front);
}
};
int main()
{
CList a(3);
a.push_front(5); a.push_front(7); a.push_front(9);
return 0;
}