returning junk values in pop function of a stack

Write#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
class integers
{
public:
int row;
int col;

};
class isFull
{
char * data = "the stack is full \n";
public:
isFull()
{
data;
}
void print()
{
cout << data;
}
};
class isEmpty
{
char * data = "the stack is already empty \n";
public:
isEmpty()
{
data;
}
void print()
{
cout << data;
}
};
class stack
{
public:
int top, size;
integers *arr;
public:
stack()
{
top = 0;
arr = nullptr;
size = 3;
}
stack(int s)
{
arr = new integers[s];
size = s;
}
bool isfull()
{
if (top == size)
return true;
else
return false;
}
bool isempty()
{
if (top == 0)
return true;
else return false;

}
bool half()
{
if (size < size / 2)
return true;
else return false;
}
integers * push(integers data)
{
/* if (half())
{
size = size / 2;
integers *temp = new integers[size];
for (int i = 0; i < top; i++)
{
temp[i] = arr[i];
}
temp[top].row = data.row;
temp[top].col = data.col;
top++;
delete[]arr;
arr = nullptr;
arr = temp;
return temp;
}*/
if (isfull())
{
size = size * 2;
integers *temp = new integers[size];
for (int i = 0; i < top; i++)
{
cout << arr[i].row <<" "<<arr[i].col<< endl;
temp[i] = arr[i];
}
temp[top].row = data.row;
temp[top].col = data.col;
cout << temp[top].row << endl;
cout << temp[top].col << endl;
top++;
delete[]arr;
arr = nullptr;
arr = temp;
return temp;
}
else if (isempty())
{
arr = new integers[size];
arr[top].row = data.row;
arr[top].col = data.col;
cout << arr[top].row << endl;
cout << arr[top].col << endl;
top++;
return arr;
}
else if (!isempty())
{
arr[top].row=data.row ;
arr[top].col=data.col ;
cout << arr[top].row << endl;
cout << arr[top].col << endl;
top++;
return arr;
}
else
{

arr[top].row = data.row=-1;
arr[top].col = data.col=-1;
return arr;
}

}
integers pop()
{
integers data;
if (!isempty())
{
data = arr[top];
data.row = arr[top].row;
data.col = arr[top].col;

delete[]arr;
arr = nullptr;
cout << data.row << endl;
cout << data.col << endl;
top--;
return data;
}
else
{
integers data;
cout << "stack is empty " << endl;
data.row = -1;
data.col = -1;
return data;
}
}
/*void print()
{
for (int i = 0; i < top;i++)
cout << arr[i] << endl;
}*/
~stack()
{
delete[]arr;

}

};
//I am trying to implement a stack class to solve maze problem but my pop function is returning junk values. I am using integers class as a struct for my rows and columns variables in which I will be storing the indexes of a found path.

Last edited on
delete[]arr; looks wrong in pop.
arr appears to be the stack's container, and ALL the data (?) so if you delete it subsequent pops will be junk.

you want to only delete the top element, somehow...

I think print has the same problem.

Last edited on
Topic archived. No new replies allowed.