Question

public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...

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.

  • Sample runs would be as follows.

    >java LinkedStackOfStrings hello
    this
    is a
    test run
    for the
    hello program

    hello exists in the linked stack

    >java LinkedStackOfStrings winter
    hello world
    is the first program
    of any programming
    language

    winter does not exist in the linked stack

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class LinkedList {       private Node list;       public LinkedList()       {           list =...
public class LinkedList {       private Node list;       public LinkedList()       {           list = null;       }       public Node getList()       {           return list;       }       . . .//other methods       // insert method printHighEarners here       // insert method lowestPaidEmployeeRec       private class Node       {           public Employee data;           public Node next;           public Node(Employee emp)           {               data = emp;               next = null;           }           public String toString()...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT