Explanation of Solution
The given program is used to implement stack using linked list.
Logical error:
Logical error is a mistake in a program’s source code that leads to produce an incorrect output. This error is a type of run time error, as it cannot be identified during the compilation of the program. This is a logical mistakes created by the programmer and it is determined when the program produces wrong output.
Error in the given code:
The function “pop()” forgets to remove the value from the stack. That is the statement for removing the element is missing in the function definition. The below given statement should be added in the function definition to produce the correct output.
Correct statement:
/*creates a string variable retValue which holds value of the top element*/
String retValue=top.value;
//assigning the top with the link of next node of the list
top=top.next;
//returning the value at position top in the stack
return retValue;
Corrected code:
/*pop method removes the value from the top position of the stack*/
int pop()
{
/*condition for checking whether the stack is empty*/
...
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)
- Book: 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_forward1. Write a generic static method that takes a Stack of any type element as a parameter, pops each element from the stack, and prints it. It should have a type parameter that represents the Stack's element type.arrow_forwardWhat distinguishes the restricted from the unbounded versions of the stack, please?arrow_forward
- Examine the stack's performance when it is allowed to operate in its natural state.arrow_forwarddon't use others answers java 1. Write a generic static method that takes a Stack of any type element as a parameter, pops each element from the stack, and prints it. It should have a type parameter that represents the Stack’s element type.arrow_forwardThe ADT stack lets you peek at its top entry without removing it. For some applications of stacks, you also need to peek at the entry beneath the top entry without removing it. We will call such an operation peek2. If the stack has more than one entry, peek2 returns the second entry from the top without altering the stack. If the stack has fewer than two entries, peek2 throws an exception. Write a linked implementation of a stack class call First_Last_LinkStack.java that includes a method peek2arrow_forward
- 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_forwardAn ADT linked stack with the top node as its last link is a good example of how this stack might be used. The following methods may be defined without traversing. A (a) pop (b) peekarrow_forwardvoid do(stack s){ stacktemp; while(!s.empty()){ temp.push(s.top(); s.pop();} s=temp;} The stack s is a. destroyed (empty) b. reversed c. not changed d. has rubbish dataarrow_forward
- What happens if the stack grows too large?arrow_forwarduse java pleasearrow_forwardvoid stack::do(){ for(int i=0li<=topindex/2;i++){ T temp=entry[i]; entry[i]=entry[topindex-i-1]; entry[topindex-1-i]=temp;} } Assume the stack is array based. What is this method do? a. swap the first item with last item b. doesn't do any thing c. replace each item with next item value d. reverse the stackarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning