Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 21, 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
What distinguishes the restricted from the unbounded versions of the stack, please?
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{     }…
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