首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为: The element of stack are:4 3 2 1 注意:请勿修改主函数main和其他函数中的任何内容,
请打开考生文件夹下的解决方案文件proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为: The element of stack are:4 3 2 1 注意:请勿修改主函数main和其他函数中的任何内容,
admin
2021-02-22
72
问题
请打开考生文件夹下的解决方案文件proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为:
The element of stack are:4 3 2 1
注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
//proj2.cpp
#include<iostream>
using namespace std;
const int Size=5;
class Stack;
class Item
{
public:
//********found********
Item(const int&val):_______{}//构造函数对item进行初始化
private:
int item;
Item*next;
friend class Stack;
};
Class Stack
{
public:
Stack( ):top(NULL){}
~Stack( );
int Pop( );
void Push(const int&);
private:
Item*top;
};
Stack::~Stack( )
{
Item*p=top,*q;
while(p!=NULL)
(
q=p->next;
//********found********
_______;//释放p所指向的节点
p=q;
}
}
int Stack::Pop( )
{
Item*temp;
int ret;
//********found********
_______;//使temp指向栈顶节点
ret=top->item;
top=top->next;
delete temp;
return ret;
}
void Stack::Push(const int&val)
{
Item*temp=new Item(val);
//********found********
_______;
//使新节点的next指针指向栈顶数据
top=temp;
}
int main( )
{
Stack s;
for(int i=1;i<Size;i++)
s.Push(i);
cout<<"The element of stack are:";
for(i=1;i<Size;i++)
cout<<s.top( )<<’\t’;
return0;
}
选项
答案
(1)item(val) (2)delete[ ]p (3)temp=top (4)temp->next=top
解析
(1)主要考查构造函数,对私有成员进行初始化,即item(val)。
(2)主要考查使用delete语句释放指针,一般格式为:delete[ ]+指针。
(3)指向栈顶节点的是top指针,要使temp指向栈顶节点,故使用语句temp=top;。
(4)指向栈顶节点的是top指针,要使新节点的next指针指向栈顶数据,故使用语句temp->next=top;。
转载请注明原文地址:https://kaotiyun.com/show/Pofp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列叙述中正确的是
下面不属于结构化程序设计原则的是
数据流图(DFD)中的有向箭头(→)表示
在数据库系统中,用户所见的数据模式为
在数据管理技术发展的三个阶段中,数据共享最好的是
下列叙述中正确的是
下列结构中为非线性结构的是
在软件开发中,需求分析阶段产生的主要文档是
下列关于常成员的叙述中,正确的是()。
随机试题
下列不属于库存现金内部控制活动的是()
FrankLloydWrightprobablyisthegreatestarchitectthattheUnitedStateshaseverproduced.Hewasvery【21】andhadanatural
A.血管紧张素ⅠB.血管紧张素ⅡC.去甲肾上腺素D.醛固酮E.肾素
2007年甲公司委托乙公司加工商品一批(属于应税消费品)。有关经济业务如下:(1)1月5日,发出材料一批,计划成本为400000元,材料成本差异率为2%。(2)2月8日,支付商品加工费20000元,支付应当交纳的消费税43000元。该
()根据托运人的要求,将同一船舶的同一装货港、同一卸货港、同一收货人的两批或两批以上相同或不同的货物合并签发的一套提单。
社会总需求是指()。
下列场景最不可能发生的是()。
Amajorreasonforconflictintheanimalworldisterritory.Themaleanimal【1】anarea.Thesizeoftheareaissufficienttop
Ifyou’vegotanearforlanguages,askillofcodingorasteadyhandanddon’tfaintatthesightofbloodthenyourcareerlo
ScientistssaytherehasbeenaseveredecreaseintheamountofwaterinLakeChadinnorthernAfricainthelastthirtyyears.
最新回复
(
0
)