注册 登录
编程论坛 C++教室

[求助]谁能看一下这个栈的top()函数问题

love154139 发布于 2007-07-03 17:06, 748 次点击

///////////stack.h文件/////
#ifndef STACK_H_
#define STACK_H_
#include "uitily.h"

class stack
{
public:
stack();
void empty();
bool full();
errorcode pop();
int size();
errorcode push(const int &item);
errorcode top(int item);
private:
int count;
int entry[maxsize];
};


stack::stack()
{
count=0;
}

void stack::empty()
{
count=0;
}

bool stack::full()
{
if(count>=maxsize)
return false;
else
return true;
}

int stack::size()
{
return count;
}


errorcode stack::push(const int &item)
{
errorcode outcome=success;
if(count>=maxsize)
outcome=overflow;
else
{
entry[count]=item;
count++;
}
return outcome;
}

errorcode stack::top(int item)
{
int a;
errorcode outcome=success;
if(count<=0)
outcome=underflow;
else
item=entry[count-1];///这里item的值不对的.谁能解释一下
return outcome;
}

errorcode stack::pop()
{
errorcode outcome=success;
if(count==0)
outcome=underflow;
else
--count;
return outcome;
}

#endif

/////////uitily.h文件//////////
#ifndef UITILY_H_
#define UITILY_H_

const int maxsize=10;
enum errorcode{success,overflow,underflow};


#endif




////main.cpp/////////
#include "stack.h"
#include <iostream>
using namespace std;
#include "uitily.h"


void main()
{
int a;
stack s;
cout<<"请输入一个数0结束"<<endl;
cin>>a;
while(a!=0)
{
s.push(a);
cin>>a;
}
while(s.size()!=0)
{
s.top(a);
cout<<a<<" ";
s.pop();
}
}

请调试一下,,,好象是划红线的部分不对......找不出原因.....

2 回复
#2
aipb20072007-07-03 17:21
errorcode stack::top(int &item) //传引用调用
{
int a;
errorcode outcome=success;
if(count<=0)
outcome=underflow;
else
item=entry[count-1];///这里item的值不对的.谁能解释一下
return outcome;
}
#3
love1541392007-07-03 17:26
可以了...谢谢楼上的哦~~~~
1