顺序栈的实现!!大家帮帮忙吧!!!
//栈类型的定义 lrd.h
#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
//aa.h
#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
//构造一个空栈,也是给构造函数的定义 lrd.cpp
#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;
}
//main.cpp
#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);
}
}