stack using array
Mar 5, 2012 at 5:48pm UTC
Can someone please help me figure out why this outputs nothing when I run this program? The only thing on the screen is "press any key to continue" and once I press a key it crashes.
.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
class arrayStackType
{
private :
int maxStackSize;
int stackTop;
int list[];
public :
arrayStackType(int );
bool isEmptyStack();
bool isFullStack();
void push(int );
int top();
void pop();
};
arrayStackType::arrayStackType(int stacksize)
{
maxStackSize=stacksize;
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;
}
Mar 5, 2012 at 5:53pm UTC
What does this do? int list[];
Mar 5, 2012 at 6:12pm UTC
That was supposed to be int *list
. After I changed that, I ran the program and now it just crashes without showing anything.
Mar 5, 2012 at 6:17pm UTC
You also need to point the list pointer to some valid memory that you can write to.
Mar 5, 2012 at 6:26pm UTC
Ok thanks. I think I can handle it from here.
Topic archived. No new replies allowed.