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_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_forward
- Write the methods bool Stack::Push(Object* element) Object* Stack::Pop() From Scratch using fixed size arraysarrow_forwardBook: Java Software Structures Author: John Lewis; Joe Chase Javascript 1. Create a generic type java interface StackADT<T>with the following methods: a.public void push(T element);//Push an element at the top of a stack b.public T pop();//Remove and return an element from the top of a stack c.public T peek();//return the top element without removing it d.public booleanisEmpty();//Return True if a stack is emptye.public int size();//Return the number of elements in a stack f.public String toString();//Print all the elements of a stack 2. Define ArrayStack<T>class which will implementStackADT<T>interface 3. Use your ArrayStack<T>class to compute a postfix expression, for instance, 3 4 * will yield12;3 4 6 + *will yield 30 etc. During computing a postfix expression: a.While you perform a push operation, if the stack is full, generate an exception b.While you perform a pop operation, if the stack is empty, generate an exception c.If two operands are not available…arrow_forwardhelllparrow_forward
- A 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_forwardTask:implement a function that reverses a list of elements by pushing them onto a stack in one order and writing them back to the list in reversed order P.S:Answer must be in pythonarrow_forwardThe CopyTo method copies the contents of a stack into an array. The arraymust be of type Object since that is the data type of all stack objects. Themethod takes two arguments: an array and the starting array index to beginplacing stack elements. The elements are copied in LIFO order, as if they werepopped from the stack. write a short code fragment demonstrating a CopyTomethod call:arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning