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)
- The 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_forwardAssume 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_forward
- use 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_forwardplease help in c++arrow_forward
- void 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_forwardQ#01: In the linked implementation of a stack for checking whether, the stack is full, why it is not necessary to implement the operation. Explain Briefly.arrow_forwardThe calculator works as follows: if number, push to a stack if operator, pop two numbers, operate on them, and push the result if I hit an operator and there are not two numbers on the stack, it's a bad expression if I get to the end of the expression and there is not exactly one number on the stack, it's a bad expression. YOUR IMPLEMENTATION, 30 points: 10 - the input to output happening without a crash 10 for implementing LinkedListBasedStack 10 for implementing ArrayList YOUR CALCULATOR ONLY NEEDS TO WORK FOR +, -, and * YOUR CALCULATOR ONLY NEEDS TO WORK ON INTEGERS. RPNStringTokenizer.java package template; import java.util.ArrayList; public interface RPNStringTokenizer { public static ArrayList<String> tokenize (String expression) { // take a string. If it is valid RPN stuff - integers or operators // - then put them in a list for processing. // anything bad (not int or one of the operators we like) and return a null. // YOU WRITE THIS! return null; } }…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning