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();
}
}
//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();
}
}
}
Get Answers For Free
Most questions answered within 1 hours.