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)
- Assume an implementation of a STACK that holds integers. Write a method public void CountPosNeg (Stack S) that takes Stack S as a parameter. After the call, the method prints the count of positive integers and negative integers on the stack. Note, the original must not be destroyed. public void CountPosNegarrow_forwardDescribe how we might efficiently implement a Queue as a pair of Stacks, called a “stack pair.” (Hint: Think of one of the stacks as the headof the queue and the other as the tail.)arrow_forwardSuppose that stack is an object of type stackType<int>. What is the difference between stack.top and stack.top - 1.arrow_forward
- Write a java program that allows the user: 1- Implement a queue in an array. 2- Enter the following numbers 3,4,6,7,2,1,0 in the queue. 3- Reverse the queue elements using a stack to display them in this order (i.e. 0,1,2,7,6,4,3).arrow_forwardDescribe how we might efficiently implement a Queue as a pair of Stacks, called a “stack pair.” (Hint: Think of one of the stacks as the head of the queue and the other as the tail.)arrow_forwardWrite a Java program that allow the user 1.Implement a queue in an array2. Enter the following number 3,4,6,7,2,1,0) in the queue 3. Reverse the queue element using a stack to display them in this order (i.e. 0,1,2,7,6,4,3).arrow_forward
- Recall the array-based implementation of the Stack class: public class Stack<E> { private void grow() {...} private void push(E data) {...} private Object[] stack = new Object[10]; private int top; } Implement the "grow" method so that when it is called by the "push" method when the stack array is full, it doubles the size of the stack array.arrow_forwardWhat is the time complexity for efficiently removing m items from a stack with n size that is implemented as a linked list (m < n)?arrow_forwardWrite in Java a stack that uses linked list and array together. No libraries may be used such as util*arrow_forward
- Implement generic class ArrayStack as discussed in the lectures having following methods: constructor, push, pop, peek, isEmpty, copy constructor, reallocate (private method). Also add following methods to this class: size: returns number of elements in the stack. contains: Search an element in the stack. If found returns true, else returns false. clear: Deletes all elements of the stack and make it empty stack.arrow_forwardThere is a data structure called a drop-out stack that behaves likea stack in every respect except that if the stack size is n, whenthe n + 1 element is pushed, the first element is lost. Implement adrop-out stack using an array. (Hint: A circular array implementation would make sense.)arrow_forwardImplement a method transfer in class LinkedStack. This method should transfer all elements of a stack sourceS to another stack targetS so that the element that starts at the top of sourceS is the first one to be inserted in targetS, and the element at the bottom of sourceS ends up at the top of targetS. The operation should result in sourceS being an empty stack. Test this method in the main method of LinkedStack.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning