Hi everyone. I have an assignment, which is to Design and Implement a Stack Class such that if the Stack gets full (default size = 100), it doubles the size of its Dynamically Allocated Array, copies the data into the newly allocated larger array, and releases the old array. Please note that the stack should be intact and contain the element that was to be pushed onto the stack when it ran out of memory. Display all the elements on the stack by popping until the stack is empty. The stack is empty now but it has memory to hold 100 elements,push 250 pseudo random integ er elements onto the stack. The memory should get doubled twice; once when the 101st element is being pushed, and then when the 201st one comes along. 5. Put a “cout statement” in the appropriate method to flag when the memory is getting Doubled.
I've been able to fill the stack with 250 numbers, and it doubles and everything works fine. But I have a feeling I'm doing something , notably in my push function. I kinda just made up some code and it worked . but can you tell if it contains a bug or I'm actually right?
public:
Stack();
Stack(int);
Stack(const Stack& copyee);
Stack& operator =(const Stack& rightElement);
void push(int Value);
void pop();
~Stack();
//private functions are left public for testing purposes (i.e. to show stack size etc. im kinda lazy to make accesor and mutator functions lol)
//private:
int *stackPtr;
int top;
int size;
bool isEmpty() const {return -1 == top;}
bool isFull() const {return size == top;}
};
int main()
{
Stack list; //initializes the stack to empty but with 100 memory //slots(i.e.array)
list.push(185); // pushing 185 random integers onto the stack of size 100 //(so it should get double in the function)
//just shows the index of the top variable on the stack, the stack size, nd last //element (normally would be private though)
cout << "stack position, stack size, last element in stack";
cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
list.pop();
cout << "stack position, stack size, value of data in that stack";
cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
return 0;
}
//default constructor initializes the variable to 100;
Stack::Stack()
{
size = DEFAULT_STACK_SIZE;
top = -1;
stackPtr = new int[size];
}
//constructor to set to required size
Stack::Stack(int newsize)
{
size = newsize;
top = -1;
stackPtr = new int[size];
}
//copy constructor
Stack::Stack(const Stack& copyee)
{
size = copyee.size;
top = copyee.top;
stackPtr = new int[size];
for (int i =0; i<top; i++)
stackPtr[i] = copyee.stackPtr[i];
}
//destructor
Stack::~Stack(){
delete [] stackPtr;
cout << endl << "destructor";
}
//= operator overloaded, commented out bcoz code works w/o it
/*Stack& Stack::operator=(const Stack& rightElement)
{
if (size != rightElement.size)
{
delete [] stackPtr;
stackPtr = new int[rightElement.size];
}
size = rightElement.size;
top = rightElement.top;
for (int i=0; i< top; i++)
stackPtr[i] = rightElement.stackPtr[i];
return *this;
}*/
void Stack::push(int numbers2bpushed)
{
for (int i =0; i<= numbers2bpushed; i++)
{
stackPtr[top++] = rand()/100;
//im not sure i understand this loop even though i wrote it, how does it keep my //data intact
if (isFull())
{
Stack* newobj;
newobj = new Stack[size+=100];
delete [] stackPtr;
cout << endl << "Doubling " << endl;
}
}
}
//remove from stack
void Stack::pop()
{
while (!isEmpty())
{
cout << stackPtr[top] << endl;
top--;
}
}
p.s. lost a couple good hours of sleep trying to figure out if there is a bug so any help is greatly appreciated.
#include <cstdlib>
#include <iostream>
usingnamespace std;
#define DEFAULT_STACK_SIZE 100
#define SECOND_TEST_SIZE 250
class Stack
{
public:
Stack();
Stack(int);
Stack(const Stack& copyee);
Stack& operator =(const Stack& rightElement);
void push(int Value);
void pop();
~Stack();
//private functions are left public for testing purposes (i.e. to show stack size etc. im kinda lazy to make accesor and mutator functions lol)
//private:
int *stackPtr;
int top;
int size;
bool isEmpty() const {return -1 == top;}
bool isFull() const {return size == top;}
};
int main()
{
Stack list; //initializes the stack to empty but with 100 memory //slots(i.e.array)
list.push(185); // pushing 185 random integers onto the stack of size 100 //(so it should get double in the function)
//just shows the index of the top variable on the stack, the stack size, nd last //element (normally would be private though)
cout << "stack position, stack size, last element in stack";
cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
list.pop();
cout << "stack position, stack size, value of data in that stack";
cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
return 0;
}
//default constructor initializes the variable to 100;
Stack::Stack()
{
size = DEFAULT_STACK_SIZE;
top = -1;
stackPtr = newint[size];
}
//constructor to set to required size
Stack::Stack(int newsize)
{
size = newsize;
top = -1;
stackPtr = newint[size];
}
//copy constructor
Stack::Stack(const Stack& copyee)
{
size = copyee.size;
top = copyee.top;
stackPtr = newint[size];
for (int i =0; i<top; i++)
stackPtr[i] = copyee.stackPtr[i];
}
//destructor
Stack::~Stack()
{
delete [] stackPtr;
cout << endl << "destructor";
}
//= operator overloaded, commented out bcoz code works w/o it
/*Stack& Stack::operator=(const Stack& rightElement)
{
if (size != rightElement.size)
{
delete [] stackPtr;
stackPtr = new int[rightElement.size];
}
size = rightElement.size;
top = rightElement.top;
for (int i=0; i< top; i++)
stackPtr[i] = rightElement.stackPtr[i];
return *this;
}*/
void Stack::push(int numbers2bpushed)
{
for (int i =0; i<= numbers2bpushed; i++)
{
stackPtr[top++] = rand()/100;
//im not sure i understand this loop even though i wrote it, how does it keep my //data intact
if (isFull())
{
Stack* newobj;
newobj = new Stack[size+=100];
delete [] stackPtr;
cout << endl << "Doubling " << endl;
}
}
}
//remove from stack
void Stack::pop()
{
while (!isEmpty())
{
cout << stackPtr[top] << endl;
top--;
}
}
Please do post your code within code tag next time.