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()); }...
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;...
- 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;       ...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT