[说明] 以下程序的功能是实现堆栈的一些基本操作。堆栈类stack共有三个成员函数
问题:
[说明]
以下程序的功能是实现堆栈的一些基本操作。堆栈类stack共有三个成员函数:empty判断堆栈是否为空;push进行人栈操作;pop进行出栈操作。
[C++程序]
#include "stdafx. h"
#include <iostream, h>
eonst int maxsize = 6;
class stack
float data[ maxsize];
int top;
public:
stuck(void);
~ stack(void);
bool empty(void);
void push(float a);
float pop(void);
;
stack: :stack(void)
top =0;
cout < < "stack initialized." < < endl;stack:: ~stack(void)
cout < <" stack destoryed." < < endl;
bool stack:: empty (void)
return (1) ;
void stack: :push(float a)
if(top= =maxsize)
cout < < "Stack is full!" < < endl;
return;
data[top] =a;
(2) ;float stack:: pop (void)
if( (3) )
cout< < "Stack is undcrflow !" < < endl;
return 0;
(4) ;
return (5) ;void main( )
stack s;
coat < < "now push the data:";
for(inti=l;i< =maxsize;i+ +)
cout< <i< <" ";
s. push(i);coat < < endl;
cout< < "now pop the data:";
for(i = 1 ;i < = maxsize ;i + + )
cout< <s. pop()< <" ";