顺序栈的实现

lrd408 2008-03-30 05:07:50
#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)


...全文
179 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lrd408 2008-03-30
  • 打赏
  • 举报
回复
回3楼的:
我是想构造个空栈
ttkk_2007 2008-03-30
  • 打赏
  • 举报
回复

tack::tack(int* stase,int* top,int sizetack) {
stase=new(NUM*sizeof(int)); //new int[NUM];
if(!stase) exit(1); //分配失败会抛异常,这句没用,除非上句式new (nothrow) int[NUM];
stase=top; //怎么又让stase指向top?,你想内存泄漏?
sizetack=NUM;
}

lrd408 2008-03-30
  • 打赏
  • 举报
回复
//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;
}


//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
#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);
}
}

huangxiaofei 2008-03-30
  • 打赏
  • 举报
回复
你的"lrd.h"文件呢?一起贴上来撒,要不然根本无法调试的说。
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-30 05:07
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下