| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 846 人关注过本帖
标题:简单栈的操作
只看楼主 加入收藏
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
 问题点数:0 回复次数:6 
简单栈的操作
#include<iostream>
#include<conio.h>
using namespace std;
class Stack {
int *base,*top;
public:
void initstack(Stack s){
s.base=new int[10];
if(! s.base) exit(0);
s.top=s.base=0;
}
void push(Stack p,int n){
if(p.top-p.base>=10) cout<<"栈溢出!"<<endl;
else
{ *p.top++=n;
cout<<*(p.top--);}
}
void pop(Stack s){
if(s.top==s.base) cout<<"空栈"<<endl;
else {int e= * --s.top; cout<<"栈顶元素为:"<<e<<endl;}
}
};
int main()
{
Stack s;
s.initstack(s);
s.push(s,10);
s.push(s,12);
s.push(s,14);
s.pop(s);
getche();
}
内存不可读,是哪儿出了问题了,看了好办天没看出什么东西来,请大家帮忙~~
搜索更多相关主题的帖子: base Stack int void top 
2007-03-31 15:05
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
可以运行啊
DEV

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2007-03-31 16:26
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
得分:0 
不是吧,我用的也是DEV C++啊,但我就不能,就是出现内存不能读的错误

2007-03-31 16:58
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
得分:0 
编译没得问题,就是一运行就出现内存不可读的问题,郁闷得很啊/////
我担心是不是我在建立空栈的时候分配内存地址的时候出了什么问题而导致内存不可读

2007-03-31 17:01
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

<1>function should return a value;
<2>local variable 's' used without having been initialized;


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-03-31 18:25
游乐园
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:671
专家分:0
注 册:2006-11-1
收藏
得分:0 

这样试试吧 ...注意指针


#include<iostream>
using namespace std;

class Stack {
int *base,*top;
public:
Stack()
{
base=new int[10];
if(!base) exit(0);
top=base;
}
void push(int n){
if(top-base>=10) cout<<\"栈溢出!\"<<endl;
else
{ *top++=n;
cout<<*(top-1)<<endl;
}
}
void pop(){
if(top==base) cout<<\"空栈\"<<endl;
else {int e= * --top; cout<<\"栈顶元素为:\"<<e<<endl;}
}
~Stack() {delete []base;}
};
int main()
{
Stack s;
s.push(10);
s.push(12);
s.push(14);
s.pop();

return 0;
}


unicorn-h.spaces. ◇◆ sava-scratch.spaces.
2007-03-31 19:57
bill8888
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-3-10
收藏
得分:0 
谢谢大家了哈
我就觉得应该是我的内存空间没有处理好

2007-03-31 21:53
快速回复:简单栈的操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016997 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved