Stacks class error
Feb 27, 2012 at 9:16pm UTC
I'm trying to do some basic functions for stacks but when I run the program it always outputs "empty stack". I think the push function is not working but I can't figure out why.
.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
using namespace std;
#include "arrayStackType.h"
int main()
{
// arrayStackType maxStackSize[100];
arrayStackType stack(100);
//arrayStackType stack2(10);
stack.initializeStack();
stack.push(7);
stack.push(12);
stack.push(8);
stack.top();
//stack1.reverseStack(stack2);
system("pause" );
return 0;
}
.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
class arrayStackType
{
private :
int maxStackSize;
int stackTop;
int list[];
public :
arrayStackType(int );
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void push(int );
int top();
void pop();
};
arrayStackType::arrayStackType(int stacksize)
{
maxStackSize=stacksize;
//stackTop=0;
}
void initializeStack()
{
stackTop=0;
}
void arrayStackType::push(int n)
{
if (!isFullStack())
{
list[stackTop]=n;
stackTop++;
}
else
cout<<"Stack is full" <<endl;
}
bool arrayStackType::isEmptyStack()
{
return (stackTop==0);
}
bool arrayStackType::isFullStack()
{
return (stackTop==maxStackSize);
}
int arrayStackType::top()
{
if (!isEmptyStack())
cout<<"empty stack" <<endl;
else
return list[stackTop-1];
}
void arrayStackType::pop()
{
if (!isEmptyStack())
stackTop--;
else
cout<<"Empty Stack" <<endl;
}
Feb 27, 2012 at 9:41pm UTC
The top() function will return the string "empty stack" when !isEmptyStack() which seems backwards.
Feb 27, 2012 at 10:31pm UTC
thx
Topic archived. No new replies allowed.