I need to write a program which reverse an array using a stack.I have one error which says that can not convert from void to int and I don't know how to approach the error.
the functions. (pop and inverseOrder)
void pop(Stack& S)
{
Stack aux = S;
S = S->link;
delete(aux);
}
void inverseOrder(int v[], int n)
{
Stack S;
initStack(S);
for (int i = 0; i < 0; ++i)
{
push(S, v[i]);
}
for (int i = 0; i < n; ++i)
{
v[i] =pop(S); // here is the error.How to approach this?
}
cout << "Reversed array is: ";
display(v, n);
}
v[i] =pop(S);The return type of your function, pop, is void. That means it doesn't return anything. Yet you are trying to assign the would-be return value of pop(S) to v[i].
Here is an example of a function that returns something:
1 2 3 4 5 6 7 8
int something()
{
return 43;
}
// ...
int thing = something();
What is the definition of "Stack" in your code. If it isn't a pointer, then calling delete aux; doesn't make sense.
Okay so your "Stack" is actually an individual Element pointer that you typedef'd.
So perhaps you want to return something from your pop() function, as I mentioned before.
1 2 3 4 5 6 7 8 9 10
int pop(Stack& S)
{
int value = S->data;
// your existing code:
Stack aux = S;
S = S->link;
delete aux;
// return:
return value;
}