Explanation of Solution
The given program is for implementing stack using an array.
Compile-time error:
An error which occurs on the source code of a program when user violate the rules of writing syntax are called as compile-time error. If the code fails to prove its language syntax format, then the compiler will throw an error.
Error in the given code:
The flow of execution never reaches the statement top--; The execution of code will never reach the line written after the return statement. So the top--; should give before return statement to get affected.
Correct statement:
//Decrementing the value of top by 1
top--;
//returning the value at position top-1 in the stack
return s[top];
Corrected code:
/*An array implementation of a stack
function for popping a value from the stack.*/
int pop()
{
/*checks whether the value of top is equal to zero*/
if (top == 0)
/*if the value of top is equal to zero function throws an exception to indicate the stack is empty*/
throw new EmptyStackException( );
...
Want to see the full answer?
Check out a sample textbook solutionChapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- A drop-out stack is a data structure that acts just like a stack except that if the stack size is n, and the n + 1 element is pushed, the first element is lost. Using an array, create a drop-out stack. (Hint: A circular array implementation would be appropriate.)arrow_forward6. Given a Stack of holding 7 objects. Show the final contents of the array after the following code is executed: for (int k = 1; k <= 7; k++) S.Push(k); for (int k = 1; k<= 4; k++) { S.Push(S.Pop()); S.Pop(); }arrow_forwardOption+Explanationarrow_forward
- Subject: Java Programmingarrow_forwardvoid stack:do(0{for(int i=0;i<=topindex/2;i++) {T temp=entry[i]; entry[i]=entry[topindex-i-1]; entry[topindex-1-i]=temp;}} What is this method do? O a. Doesn't do any thing O b. Replace each item with next item value O C. Swap the first item with last item O d. Reverse the stackarrow_forwardWrite the methods bool Stack::Push(Object* element) Object* Stack::Pop() From Scratch using fixed size arraysarrow_forward
- PROBLEM STATEMENT: Return a sublist of a given list from the range of the two integerinputs from, to. public class FindSublistFromRange{public static ArrayList<Integer> solution(ArrayList<Integer> elms, int from, int to){// ↓↓↓↓ your code goes here ↓↓↓↓return new ArrayList<>();} Can you help me with this question The language is Java Please use the code i provided above to answer this questionarrow_forwardhelllparrow_forwardIf the elements “A”, “B”, “C” and “D” are placed in a stack and are removed one at a time, in what order will they be removed?arrow_forward
- Look at how well the stack works when allowed to do what it does best.arrow_forwardjavaarrow_forwardA data structure known as a drop-out stack functions exactly like a stack, with the exception that if the stack size is n, the first element is lost when the n + 1 element is pushed. Use an array to implement a drop-out stack. (Hint: It would make sense to implement a circular array.)arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning