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
A Stack is a data structure for storing a list of elements in a LIFO (last in, first out) fashion. Design a class called Stack with three methods. void Push(object obj)object Pop()void Clear() We should be able to use this stack class as follows:var stack = new Stack();stack.Push(1);stack.Push(2);stack.Push(3);Console.WriteLine(stack.Pop());Console.WriteLine(stack.Pop());Console.WriteLine(stack.Pop());   The output should be 3, 2, 1
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{     }…
**Use the Stack interface to implement the LinkedStack.java class. Stack.java public interface Stack<E> {/*** Push an element onto the stack.** @param element, a value to be pushed on the stack*/public void push(E element);/*** Pop the top element off the stack.*/public E pop();/*** Return the top element on the stack.*/public E top();/*** Return True if the stack contains no elements.** @return true if there are no elements in the stack*/public boolean isEmpty();} ***In LinkedStack.java, change only The methods with empty bodies Do not use a sentinel node. In this implementation you use an inner class That is, a class that is declared inside of another class. The Node class isn't needed by any other class, so it is declared as a private class inside the LinkedStack class. The Node class has two fields: element and next. Things to note in the class LinkedStack: The EmptyStackExceptionis being used, same as in the ArrayStack. The class StringJoineris imported for use in the…
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