顺序栈的实现!!大家帮帮忙吧!!!

lrd408 2008-03-31 05:33:50
//栈类型的定义 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);
}
}

...全文
170 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2008-04-01
  • 打赏
  • 举报
回复
//??
stase = new(NUM*sizeof(int));
new [size];
malloc(size);
lrd408 2008-04-01
  • 打赏
  • 举报
回复
编译没问题,但不能运行!!
请大家帮帮忙吧
帅得不敢出门 2008-04-01
  • 打赏
  • 举报
回复
//构造一个空栈,也是给构造函数的定义 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; //------------你这个赋值很妙 都把申请的东东给丢了 改成top=stase;
sizetack=NUM;
}

liveforme 2008-04-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dizuo 的回复:]
C/C++ code//??
stase = new(NUM*sizeof(int));
new [size];
malloc(size);
[/Quote]
lz把new和malloc混淆了吧
gl0312 2008-04-01
  • 打赏
  • 举报
回复
严重关注中……
不想低调 2008-03-31
  • 打赏
  • 举报
回复
问题呢??

65,186

社区成员

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

试试用AI创作助手写篇文章吧