Question

Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line),...

Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line), insert each separate word into the stack, and read/print on the screen.

So, if the user enters: This is a test

the application output should be: test a is This.

This is SLLSTACK.java below

package linked_lists;

public class SLLStack {

   //instance variabels
   private SLLNode top;
   private int numOfItems;
  
   //constructors
   public SLLStack() {
       top = null;
       numOfItems = 0;
   }
  
   //methods
   public int getNumOfItems() {
       return numOfItems;
   }
  
   public SLLNode getTop() {
       return top;
   }
  
  
   //pop: removes the top item, but it does not return the item.
   public void pop() throws StackEmptyException{
       //stack might be empty
       if(top == null)
           throw new StackEmptyException("Stack is empty! Cannot pop!");
      
       if(top.getNextLink() == null)
           top = null;
       else
           top = top.getNextLink();
   }
  
  
   //top: reads the top item on the stack
   public T top() throws StackEmptyException {
       if(top == null)
           throw new StackEmptyException("Stack is empty! Cannot pop!");
      
       return top.getData();
   }
  
   public void push(T newData) {
       //create the new node obj first
       SLLNode newNode = new SLLNode<>(newData);
      
       //stack is empty
       if(top == null) {
           top = newNode;
           newNode = null;
           numOfItems++;
       }else {
           //stack is not empty
           newNode.setNextLink(top);
           top = newNode;
           newNode = null;
           numOfItems++;
       }
   }
  
   //just for test/debug purposes, implementing toString
   public String toString() {
       String result = "";
       SLLNode curr = top;
      
       while(curr.getNextLink() != null) {
           result += curr.getData().toString();
           curr = curr.getNextLink();
       }
       return result + curr.getData().toString();
   }
}

Homework Answers

Answer #1

//SLLStack.java

package test2;

public class SLLStack <T> {
   //instance variabels
   private SLLNode top;
   private int numOfItems;
  
   //constructors
   public SLLStack() {
   top = null;
   numOfItems = 0;
   }
  
   //methods
   public int getNumOfItems() {
   return numOfItems;
   }
  
   public SLLNode getTop() {
   return top;
   }
  
  
   //pop: removes the top item, but it does not return the item.
   public void pop() throws StackEmptyException{
   //stack might be empty
   if(top == null)
   throw new StackEmptyException("Stack is empty! Cannot pop!");
  
   if(top.getNextLink() == null)
   top = null;
   else
   top = top.getNextLink();
   }
  
  
   //top: reads the top item on the stack
   public T top() throws StackEmptyException {
   if(top == null)
   throw new StackEmptyException("Stack is empty! Cannot pop!");
   return (T) top.getData();
   }
  
   public void push(T newData) {
   //create the new node obj first
   SLLNode<T>newNode = new SLLNode<>(newData);
  
   //stack is empty
   if(top == null) {
   top = newNode;
   newNode = null;
   numOfItems++;
   }else {
   //stack is not empty
   newNode.setNextLink(top);
   top = newNode;
   newNode = null;
   numOfItems++;
   }
   }
  
   //just for test/debug purposes, implementing toString
   public String toString() {
   String result = "";
   SLLNode<T> curr = top;
  
   while(curr.getNextLink() != null) {
   result += curr.getData().toString();
   curr = curr.getNextLink();
   }
   return result + curr.getData().toString();
   }
   }

//SLLNode.java

package test2;

public class SLLNode<T>{
   T data;
   SLLNode<T> next;
  
   public SLLNode(T data){
       this.data = data;
       this.next = null;
   }
  
   public void setNextLink(SLLNode<T> obj){
       this.next = obj;
   }
  
   public SLLNode<T> getNextLink(){
       return this.next;
   }
  
   public T getData()
   {
       return this.data;
   }
  
   public String toString(){
       return "" + this.data;
   }
}

//StackEmptyException.java

package test2;

public class StackEmptyException extends Exception {

   /**
   *
   */
   private static final long serialVersionUID = 1L;
  
   public StackEmptyException(String e)
   {
       System.out.println(e);
   }

}

//LineReverser.java

package test2;
import java.util.Scanner;

public class LineReverser {
   static Scanner in = new Scanner(System.in);
   public static void main(String args[]){
       SLLStack<String> obj = new SLLStack<String>();
       String input;
       input = in.nextLine();
       in = new Scanner(input);
       while(in.hasNext()){
           obj.push(in.next());
       }
      
       for(int i = 0; i < obj.getNumOfItems(); i++)
           try {
               System.out.print(obj.top() + " ");
               obj.pop();
           } catch (StackEmptyException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
   }
}

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
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
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...
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. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL;...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
(JAVA) I have "cannot be resolved or is not a field" error on node.right = y;...
(JAVA) I have "cannot be resolved or is not a field" error on node.right = y; and node.left = x; //BuildExpressionTree.java import java.util.*; import javax.swing.*; import javax.xml.soap.Node; public class BuildExpressionTree {    private Stack stack = new Stack();    private Node node;       public static boolean isOperator(String token){        if(token == "+" || token == "-" || token == "*" || token == "/"){            return true;        }        JOptionPane.showMessageDialog(null, "Invalid token" +...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT