Question: Please help impliment the methods from the screenshot into the LinkedStack class. clear ( ) : This function should clear all elements from the stack.
Please help impliment the methods from the screenshot into the LinkedStack class.
clear: This function should clear all elements from the stack.
size This function should return the number of elements in the stack.
searchint x This function returns the index of the first occurrence of the element
in the stack, if it exists. If is not in the stack, it should return
Containsint This function should return true if is in the stack and returns false
if is not in the stack.
toArray This function should return an array containing all the elements in the stack.
Copy This function creates a copy of the current stack with the same elements and order
and return it
replaceAll This function Replaces all occurrences of oldValue with newValue in the stack.
public class LinkedStack
private Node top null;
public boolean empty
return top null;
public void pushString s
top new Nodes top;
public String pop
String retValue;
if empty
throw new IllegalStateException;
else
retValue top.element;
top top.next;
return retValue;
public String peek
if empty
throw new IllegalStateException;
else
return top.element;
public String toString
Node ref top;
String toPrint ;
while ref null
toPrint ref.element;
ref ref.next;
return toPrint;
Node class used for linked list
private class Node
String element;
Node next;
NodeString e Node n
element e;
next n;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
