Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 20, Problem 3FTE

Explanation of Solution

Program purpose:

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*/

        ...

Blurred answer
Students have asked these similar questions
Implement 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.
Examine the stack's performance when it is allowed to operate in its natural state.
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 CountPosNeg
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning