我正在练习一些实现不同数据结构的代码。对于这个例子,我试图实现一个简单的堆栈数据结构。到目前为止,它按预期工作,但在尝试显示我的堆栈时,我一直得到Hex字符。任何人都可以帮我弄清楚为什么会这样吗?
此外,我正在努力更好地构建我的代码,任何已经参与该行业的人都可以给我一些建设性的批评,到目前为止我编码的内容。谢谢。
#include <iostream>
using namespace std;
// stack_MAX == maximum height of stack
const int stack_MAX = 10;
class stack{
public:
stack(){
//Constructor initializes top of stack
top = -1;
}
bool isFull(int top){
//isFull() will check to make sure stack is not full
//Will return TRUE if stack is FULL, and FALSE if
//stack is NOT FULL
if(top == stack_MAX - 1)
return true;
else
return false;
}
bool isEmpty(int top){
//isEmpty() will check to make sure stack is not empty
//Will return TRUE if stack is EMPTY, and FALSE if
//stack is NOT EMPTY
if(top == -1)
return true;
else
return false;
}
void push(int x){
//push() will push new element on top of stack
if(isFull(top)){
cout << "Sorry, but the stack is full!" << endl;
exit(1);
}
else{
top++;
x = stk[top];
}
}
void pop(){
//pop() will pop the top element from the stack
if(isEmpty(top)){
cout << "Sorry, but the stack is empty!" << endl;
exit(1);
}
else{
cout << stk[top] << " is being popped from stack!" << endl;
top--;
}
}
void display_stack(){
//diplay_stack() will show all elements currently in the stack
int temp; //will temporarily hold position of stack
temp = top;
while(!isEmpty(temp)){
cout << stk[temp] << endl;
temp--;
}
}
private:
int top;
int stk[stack_MAX];
};
int menu(){
int choice;
cout << "Welcome to my stack!" << endl;
cout << "What would you like to do? (select corresponding #)" << endl << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Quit" << endl;
cin >> choice;
return choice;
}
int main()
{
int selection, x;
stack myStack;
selection = menu();
while(selection != 4)
{
switch(selection){
case 1:
cout << "please enter number to be pushed: ";
cin >> x;
myStack.push(x);
selection = menu();
break;
case 2:
myStack.pop();
selection = menu();
break;
case 3:
myStack.display_stack();
selection = menu();
break;
default:
cout << "Oops that's not a selection, try again" << endl;
selection = menu();
break;
}
}
cout << "Thank you for stopping by and using my stack!" << endl;
system("pause");
return 0;
}
答案 0 :(得分:3)
push
功能区中的陈述错误,修改如下:
void push(int x)
{
//push() will push new element on top of stack
if(isFull(top))
{
cout << "Sorry, but the stack is full!" << endl;
exit(1);
}
else
{
top++;
/***************************
x = stk[top];
****************************/
stk[top] = x;
}
}
建议:
cstdlib
,请包括头文件exit
代码答案 1 :(得分:0)
史前企鹅指出,你的push()函数不正确:
x = stk[top];
应改为:
stk[top] = x;
无论如何,我想发表评论,按照您的要求提供一些一般性评论:
如果这样的陈述可以用一行代码替换:
if(top == stack_MAX - 1)
return true;
else
return false;
变为:
return (stack_MAX - 1 == top);
将常量表达式放在比较表达式的左侧。例如:
(top == stack_MAX - 1)
变为:
(stack_MAX - 1 == top)
原因是有一天你会不小心输入类似的东西:
(top = stack_MAX - 1)
你或其他人会浪费大量时间调试它:)
你的isFull()和isEmpty()函数不应该带参数。他们应该只使用私有成员变量top。有人如何在没有访问top的情况下调用这些函数,你已经正确地成为私人成员?
一般情况下,请避免using
。在我看来,它破坏了名称空间的整个目的。使用namespace std是一个常用的例外,但即便如此,输入std :: cout是如此困难吗?
总是在if语句的子句中加上大括号,即使它们只是一行。如果以后需要在子句中添加更多语句,很容易忘记添加大括号,这可能会让人很困惑。
您的代码格式非常好,但选择一种支架样式并保持一致。要么总是将开括号大括号放在与函数标题/控制语句等相同的行上,要么总是把它放在行后面。
希望有所帮助。