问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程prog2,其中定义了Stack类和Entry类。Stack是一个基于链式存储结构的栈,Entry表示存储在栈中的数据顶。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Entry {
public:
Entry* next;
int data;
//********** found**********
Entry (Entry * n, int d): ______, data(d) {}
};
class Stack {
Entry* top;
public:
Stack() : top(0) {}
~Stack()
{
while (top!= 0)
{
Entry* tmp = top;
//********** found**********
top =______;
delete tmp;
}
}
void push (int data)
{
//********** found**********
top = new Entry(______, data);
}
int pop ( )
{
if (top = 0) return0;
//********** found**********
int result = ______;
top = top->next;
return result;
}
};
int main ( )
{
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Stack s;
int i = 0;
for (i = 0; i < 10; i++) {
cout << a[i] << ";
s.push(a[i]);
}
cout << endl;
for (i = 0; i <10; i++) {
cout << s.pop() <<'';
}
cout << endl;
return 0;
}
【正确答案】(1)next(n)
(2)top->next
(3)top++
(4)top->data
【答案解析】[考点] 本题考查的是Entry类和Stack类,其中涉及指针、构造函数、析构函数和成员函数。
[解析] (1)主要考查考生对构造函数的掌握情况,使用成员列表初始化。
(2)主要考查考生对栈的掌握情况,新元素需要添加到栈顶。
(3)主要考查考生对动态分配的掌握情况,给栈顶添加元素。
(4)主要考查考生对栈的掌握情况,推出栈顶元素。