[试题]

试题七

阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。

(说明)

以下程序的功能是设计一个栈类stack<T>,并建立一个整数栈。

(程序)

#include<iostream.h>

#include<stdli

B.h>

const int Max=20;∥栈大小

template<class T>

class stack{∥栈元素数组

T s[Max];∥栈顶下标

int top;

public:

stack()

{

top=-1;∥栈顶初始化为-1

}

void push(const T &item);∥item入栈

T pop();∥出栈

int stackempty()const;∥判断栈是否为空

};

template<class T>

void stack<T>::push(const T &item)

{

if(top== (1) )

{

cout<<"栈满溢出"<<endl;

exit (1) ;

}

top++;

s[top]=item;

}

template<class T>

T stack<T>::pop()

{

T temp;

if(top== (2) )

{

cout<<″栈为空,不能出栈操作″<<endl;

exit (1) ;

}

temp=s[top];

top--;

return temp;

}

template<class T>

int stack<T>::stackempty()const

{

return top==-1;

}

void main()

{

stack<int>st;

int a[]={1,2,3,4,5 };

cout<<"整数栈"<<endl;

cout<<"入栈序列:"<<endl;

for(int i=0;i<4;i++)

{

cout<<a[i]<<" ";(3) ;

}

cout<<endl<<"出栈序列:";

while( (4) )

cout<< (5) <<" ";

cout<<endl;

}

参考答案与解析: