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)
- Suppose that stack is an object of type stackType<int>. What is the difference between stack.top and stack.top - 1.arrow_forwardThere is a data structure called a drop-out stack that behaveslike a stack in every respect except that if the stack size is n,then when the n+1 element is pushed, the first element is lost.Implement a drop-out stack using linksarrow_forwardWhat is the difference between the bounded and unbounded implementations of the stack?arrow_forward
- Explain why it is not essential to perform the operation to detect whether the stack is full in the linked implementation of a stack.arrow_forwardimplement a queue with 2 stacks so that each queue operation take a constant amortized number of stack operations (using java)arrow_forwardWhat distinguishes the stack's restricted and unbounded implementations?arrow_forward
- Can you explain what the difference is between the constrained and unbounded version of the stack?arrow_forwardWrite a java code example for Casting With a Generic Stack?arrow_forwardImplement 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. package stacks;public class LinkedStack<E> implements Stack<E> {/** The primary storage for elements of the stack */private SinglyLinkedList<E> list = new SinglyLinkedList<>(); // an empty list/** Constructs an initially empty stack. */public LinkedStack() { } // new stack relies on the initially empty list@Overridepublic int size() { return list.size(); }@Overridepublic boolean isEmpty() { return list.isEmpty(); }@Overridepublic void push(E element) { list.addFirst(element); }@Overridepublic E top() { return list.first(); }@Overridepublic E pop() {…arrow_forward
- Implement a queue with three stacks so that, in the worst-case scenario, each queue operation requires a fixed number of stack operations. High level of difficulty alert.arrow_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
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning