public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next; return item; } public static void main(String[] args) { LinkedStackOfStrings stack = new LinkedStackOfStrings(); // See Program 4.3.1 for the test client. } }
first | first node on list
item | stack item next | next node on list
Modify the “LinkedStackOfStrings.java” program given above (program 4.3.2) by adding a method “find()” that takes a string as an argument. It should return true if the string argument is in the linked stack and false otherwise. [MO6.1]
The main method should also be modified so that all the strings (other than the search string) inputted by the user are stored on the stack before calling the find() method.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// LinkedStackOfStrings.java
import java.util.Scanner;
public class LinkedStackOfStrings {
private Node first;
private class Node {
private String item;
private Node next;
}
public boolean isEmpty() {
return (first == null);
}
public void push(String item) { // Insert a new node at the beginning of the
// list.
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
}
public String pop() { // Remove the first node from the list and return
// item.
String item = first.item;
first = first.next;
return item;
}
// required method to check if an item exists on stack
public boolean find(String item) {
// looping through each node on the stack
for (Node node = first; node != null; node = node.next) {
// checking if node's item equals target item
if (node.item.equals(item)) {
// found
return true;
}
}
// not found
return false;
}
public static void main(String[] args) {
LinkedStackOfStrings stack = new LinkedStackOfStrings();
//if no search string is given, displaying usage and quit
if(args.length==0){
System.out.println("Usage: java LinkedStackOfStrings <word_to_search>");
System.exit(0);
}
//finding search term
String searchTerm=args[0];
//scanner for user input
Scanner scanner=new Scanner(System.in);
String input;
//looping until EOF is triggered (ctrl+D or ctrl+Z)
while(scanner.hasNext()){
//reading next word, adding to stack
input=scanner.next();
stack.push(input);
}
//checking if searchTerm exists
if(stack.find(searchTerm)){
//exists
System.out.println(searchTerm+" exists in the linked stack");
}else{
//does not exist
System.out.println(searchTerm+" does not exist in the linked stack");
}
}
}
/*OUTPUT when ‘hello’ is passed as command line argument*/
this
is a
test run
for the
hello program
hello exists in the linked stack
Get Answers For Free
Most questions answered within 1 hours.