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.
search(int x) This function returns the index of the first occurrence of the element x
in the stack, if it exists. If x is not in the stack, it should return -1.
Contains(int x) This function should return true if x is in the stack and returns false
if x 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 push(String s){
top = new Node(s, 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;
Node(String e, Node n){
element = e;
next = n;
}
}
 Please help impliment the methods from the screenshot into the LinkedStack

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!