大家来帮帮我,栈的问题

鹏割 2014-12-26 11:08:28
刚才写的代码,用静态数组实现的栈, 写的比较简单, 有错误。。直接上代码,注释里写了
#ifndef STACK_H_
#define STACK_H_
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef unsigned int Item;
class Stack
{
private:
enum {MAX=10};
Item items[MAX];
int top;
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push( const Item & item );
bool pop(Item & item );
int getLength ( );
void getTop ( int & e );
friend Stack Randcreat();
Stack( Stack& otherS );
friend void Display ( Stack& A );
Stack operator = ( Stack & Rights );
};

int main()
{
Stack pg;
unsigned int a,b,c;
Stack gp = Randcreat();
Display(gp); //程序运行时不显示,help


pg.push(1);
pg.push(2);
pg.push(3);

int d=pg.getLength();
cout<<"栈的长度为:"<<d;
cout<<endl;
int f;
pg.getTop ( f );
cout<<"栈顶元素为:"<<f<<endl;

cout<<"出栈:";

pg.pop(a);
pg.pop(b);
pg.pop(c);
cout<<a<<b<<c<<endl;
cout<<pg.isempty();

int e=pg.getLength();
cout<<"栈的长度为:"<<e;
system("pause");
return 0;
}


Stack::Stack()
{
top = 0;
}

bool Stack:: isempty() const
{
return top==0;
}

bool Stack:: isfull() const
{
return top==MAX;
}

bool Stack:: push(const Item& item)
{
if(isfull())
return false;
else
{
items[top++]=item;
return true;
}
}

bool Stack :: pop (Item& item )
{
if(isempty())
return false;
else
{
item= items[--top];
return true;
}

}

int Stack :: getLength ()
{
return top;
}

void Stack :: getTop ( int & e )
{
e = items [top-1];
}
Stack Randcreat ()
{
Stack pp;
int n = rand() % 10+1;
for(int i=0; i<n; i++ )
{
int k =rand () % 50 + 1;
pp.push(k);
}
return pp;

}


Stack::Stack( Stack & otherS ) //这里拷贝构造函数, 参数我加上const 就不行, 里面的判段语句我也删了不然会报错
{

for(int i=0; i<otherS.getLength(); i++ )
items[i]=otherS.items[i];



}

void Display( Stack & A ) //这里加了const 也会报错
{
for( int i=0; i<A.getLength() ; i++ )
{
cout<<i+1<<" ";
}
cout<<endl;
for( int j=0; j<A.getLength(); j++ )
{
cout<<A.items[j]<<" ";
}
cout<<endl;
}

Stack Stack:: operator = ( Stack & Rights )
{

if( this != & Rights )
{
for( int i=0; i<Rights.getLength(); i++ )

items[i]=Rights.items[i];

}

return *this;
}
#endif
...全文
142 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
鹏割 2014-12-28
  • 打赏
  • 举报
回复
引用 7 楼 brookmill 的回复:
main的开头加上 srand(time(NULL)); 拷贝构造函数和赋值运算符里面都要加上 top = otherS.top;
谢谢啊,你最棒了
fly_dragon_fly 2014-12-27
  • 打赏
  • 举报
回复
1凡是不改变类成员的都应该声明为const , 所以getlength 应该声明const 2 对于copy ctor应该声明为const 引用,那个display也应该这样
brookmill 2014-12-27
  • 打赏
  • 举报
回复
main的开头加上 srand(time(NULL)); 拷贝构造函数和赋值运算符里面都要加上 top = otherS.top;
鹏割 2014-12-27
  • 打赏
  • 举报
回复
引用 2 楼 brookmill 的回复:
1楼的说法不准确。 getLength不是const。如果otherS是const,那么就不允许调用otherS.getLength() 要改成 int getLength ( ) const;
我修改了之后为什么Display()里面的两个循环直接跳过不执行啊
鹏割 2014-12-27
  • 打赏
  • 举报
回复
引用 3 楼 fly_dragon_fly 的回复:
1凡是不改变类成员的都应该声明为const , 所以getlength 应该声明const 2 对于copy ctor应该声明为const 引用,那个display也应该这样
我修改了之后为什么Display()里面的两个循环直接跳过不执行啊
鹏割 2014-12-27
  • 打赏
  • 举报
回复
@brookmill 我修改了之后为什么Display()里面的两个循环直接跳过不执行啊
brookmill 2014-12-26
  • 打赏
  • 举报
回复
1楼的说法不准确。 getLength不是const。如果otherS是const,那么就不允许调用otherS.getLength() 要改成 int getLength ( ) const;
brookmill 2014-12-26
  • 打赏
  • 举报
回复
const函数不能调用非const函数。 拷贝构造函数和Display里面都调用了getLength,要把他也声明为const函数。

64,685

社区成员

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

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