Q1
java :
If in theArrayStack, the first element that we push to the stack, is stored in locationcapacity– 1 (capacityis the capacity of the arraytheData) in the arraytheDataand the subsequent elements that we push, we keep on adding before the last element added in the array. That is, in the constructor of classArrayStack,we initialize thetopOfStacktocapacityand we write thepushmethod as follows:
public E push(E obj)
{
if (topOfStack == 0)
reallocate();
topOfStack--;
theData[topOfStack] = obj;
return obj;
}
This push method can work but the if statement should be:
if (topOfStack == theData.length - 1) reallocate();
This push method will work provided we modify the reallocate method.
This push method can work, but topOfStack-- ; should come after
This push method can work, but topOfStack-- ; should be topOfStack++;
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here