顺序栈的实现
#ifndef STACK_H
#define STACK_H
#define NUM 100
class tack {
public:
int* stase;
int* top;
int sizestack;
tack(int* stase=0,int* top=0,int sizetack=0);
};
#endif
#ifndef AA_H
#define AA_H
#include "lrd.h"
#include <iostream>
using std::cout;
using std::endl;
double GetTop(tack& S) {
int e=0;
if(S.top==S.stase) exit(1);
e=*(S.top-1);
cout<<endl;
cout<<"栈的头元素是 "<<e<<endl;
return e;
}
void Push(tack& S,int e) {
if(S.top-S.stase>=S.sizestack) {
cout<<"栈元素已满 "<<endl;
exit(1);
}
else *S.top++=e;
}
double Pop(tack& S,int e=0) {
if(S.top ==S.stase) {
cout<<"栈中已无元素!"<<endl;
exit(1);
}
else e=*--S.top;
return e;
}
#endif
#include <iostream>
#include "lrd.h"
tack::tack(int* stase,int* top,int sizetack) {
stase=new(NUM*sizeof(int));
if(!stase) exit(1);
stase=top;
sizetack=NUM;
}
#include <iostream>
#include "lrd.h"
#include "aa.h"
showtack(tack& S);
int main() {
tack we;
while((!we.stase)&&(we.stase-we.top)<=NUM) {
static int i=0;
Push(we, ++i);
}
showtack(we);
return 0;
}
showtack(tack& S) {
int t=0;
while(S.stase!=S.top) {
if(++t%5==0) cout<<'\n'<<Pop(S);
else cout<<' '<<Pop(S);
}
}
编译时无错误
连接是:--------------------Configuration: stack - Win32 Debug--------------------
Compiling...
lrd.cpp
C:\yanlian\stack\lrd.cpp(5) : error C2059: syntax error : ';'
C:\yanlian\stack\lrd.cpp(9) : error C2143: syntax error : missing ';' before '}'
C:\yanlian\stack\lrd.cpp(9) : error C2143: syntax error : missing ';' before '}'C:\yanlian\stack\lrd.cpp(9) : fatal error C1003: error count exceeds 100; stopping compilation
执行 cl.exe 时出错.
stack.exe - 1 error(s), 0 warning(s)