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
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 peekNxt. If the stack has more than one entry, peekNxt returns the second entry from the top without altering the stack. If the stack has fewer than two entries, peekNxt throws an exception. Write a linked implementation of a stack class call LinkedStack.java that includes a method peekNxt.   Implement pimport java.util.EmptyStackException;import java.util.NoSuchElementException;public final class LinkedStack<T> implements StackInterface<T>{private Node topNode; public YourFirst_YourLast_LinkedStack(){topNode = null;}public void push(T newEntry){Node newNode = new Node(newEntry, topNode);topNode = newNode;} // end pushpublic T peek(){if (isEmpty())throw new EmptyStackException();elsereturn topNode.getData();} // end peekpublic T peekNxt()  // Code here{     }…
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…
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.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE 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