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 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
- 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 peek2. If the stack has more than one entry, peek2 returns the second entry from the top without altering the stack. If the stack has fewer than two entries, peek2 throws an exception. Write a linked implementation of a stack class call First_Last_LinkStack.java that includes a method peek2arrow_forwardvoid do(stack s){ stacktemp; while(!s.empty()){ temp.push(s.top(); s.pop();} s=temp;} The stack s is a. destroyed (empty) b. reversed c. not changed d. has rubbish dataarrow_forwarduse java pleasearrow_forward
- Here is a method for stack operation: function (int a, int b) { if ( (a less than or equal to zero) || (b less than or equal to zero)) return 0; push (b) to stack; return function (a-1, b-1) + stack.pop; } What is the final integer value returned by function call of call (7, 7) ? Must show all the work Show all the recursive calls each time function is called. Must show all the work.arrow_forwardvoid do(stack<int> s){ stack<int>temp; while(!s.empty()){ temp.push(s.top()); s.pop();} s=temp;} The STL. stack s is a. reversed b. destroyed (empty) c. not changed d. has rubbish dataarrow_forwardExamine the performance of the stack under situations in which it is allowed to act naturally.arrow_forward
- In Java and in C++ the best way to implement a Stack is by deriving from any implementation of the ADT List. For instance: class Queue : public DoublelinkedList { }; True Falsearrow_forwardWrite a method that uses two data structures, a stack and a queue, to split numbers The numbers must be split into even and odd numbers. If the number is odd and divisible by 3, it is added into the queue; otherwise the top element, if any, of the stack is removed and the square of the number is added onto the stack.arrow_forwardThis is java. Write the java code that takes the contents of a stack and places it in a queue and takes the contents of a queue and puts it in a stack. Your code can only use stack and queue data structure. swapStackAndQueue(stack, queue) //method call This is java.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning