Question

In: Computer Science

JAVA: You're given two classes List.java and Node.java. In your List class you're supposed to implement...

JAVA:

You're given two classes List.java and Node.java. In your List class you're supposed to implement the methods toFront, print, and toBack. The Node class is used as a reference no edit necessary.

LIST:

public class List {

protected Node head, tail;

/**

* Initialize the list to empty. Both head and tail

* are null references in this case.

*/

public List() {

// TODO Auto-generated constructor stub

head = tail = null;

}

/**

* Add nodeToAdd to the front (head) of this list.

* @param nodeToAdd to the front of this list.

*/

public void toFront(Node nodeToAdd) {

}

/**

* Print all nodes from the list to System.out

*/

public void print( ) {

}

/**

* Add nodeToAdd to the back (tail) of this list.

* @param nodeToAdd node to add at the back (tail) of this list.

*/

public void toBack (Node nodeToAdd) {

}

}

NODE:

public class Node {

protected String contents; // the contents of this LLNode

protected Node next; // Reference to next LLNode in list.

/**

* The constructor -- The default constructor will

* build a node with a null reference for the string

* and a null reference for the next item in the list.

*/

public Node() {

this(null, null);

}

/**

* Initializes this linked list node to the string

* given and sets the next reference to null.

* @param contents The string to store in this LLNode

*/

public Node(String contents) {

this (contents, null);

}

/**

* Initializes this linked list node to the string and

* LLNode reference passed in.

* @param contents The string to store in this LLNode

* @param next The reference to the next item in the List.

*/

public Node(String contents, Node next) {

this.contents = contents;

this.next = next;

}

/**

* @return the contents of this linked list node.

*/

protected String getContents() {

return contents;

}

/**

* @param contents the string to store in this linked list node.

*/

protected void setContents(String contents) {

this.contents = contents;

}

/**

* @return the reference to the next item in the Linked List

*/

protected Node getNext() {

return next;

}

/**

* @param next the Node to set as the node to follow this item in the list.

*/

protected void setNext(Node next) {

this.next = next;

}

}

Solutions

Expert Solution

I have implemented toFront(),toBack() and print() methods in List as per the given description.
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE.

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

1.CODE SCREENSHOT :

2.OUTPUT :

3.CODE:

List.java

public class List {

protected Node head, tail;

/**

* Initialize the list to empty. Both head and tail

* are null references in this case.

*/

public List() {



head = tail = null;

}

/**

* Add nodeToAdd to the front (head) of this list.

* @param nodeToAdd to the front of this list.

*/

public void toFront(Node nodeToAdd) {
        if(head == null) {    
            //If list is empty, both head and tail will point to new node    
            head = nodeToAdd;    
            tail = nodeToAdd;    
        }    
        else {    
            //nodeToAdd will be added after head such that head will point by nodeToAdd    
            nodeToAdd.setNext(head);    
            //nodeToAdd will become new head of the list    
            head = nodeToAdd;    
        }    
}

/**

* Print all nodes from the list to System.out

*/

public void print( ) {
                Node current = head;    
            
        if(head == null) {    
            System.out.println("List is empty");    
            return;    
        }    
        System.out.println("Nodes of list: ");    
        while(current != null) {    
            //Prints each node by incrementing pointer    
            System.out.print(current.getContents());    
            current = current.getNext();
                        if(current!=null)
                                System.out.print(" , ");                        
        }    
        System.out.println(); 
}

/**

* Add nodeToAdd to the back (tail) of this list.

* @param nodeToAdd node to add at the back (tail) of this list.

*/

public void toBack (Node nodeToAdd) {
        //Checks if the list is empty    
        if(head == null) {    
            //If list is empty, both head and tail will point to new node    
            head = nodeToAdd;    
            tail = nodeToAdd;    
        }    
        else {    
            //newNode will be added after tail such that tail's next will point to newNode    
            tail.setNext(nodeToAdd);    
            //newNode will become new tail of the list    
            tail = nodeToAdd;    
        }    

}

}

Node.java

public class Node {

protected String contents; // the contents of this LLNode

protected Node next; // Reference to next LLNode in list.

/**

* The constructor -- The default constructor will

* build a node with a null reference for the string

* and a null reference for the next item in the list.

*/

public Node() {

this(null, null);

}

/**

* Initializes this linked list node to the string

* given and sets the next reference to null.

* @param contents The string to store in this LLNode

*/

public Node(String contents) {

this (contents, null);

}

/**

* Initializes this linked list node to the string and

* LLNode reference passed in.

* @param contents The string to store in this LLNode

* @param next The reference to the next item in the List.

*/

public Node(String contents, Node next) {

this.contents = contents;

this.next = next;

}

/**

* @return the contents of this linked list node.

*/

protected String getContents() {

return contents;

}

/**

* @param contents the string to store in this linked list node.

*/

protected void setContents(String contents) {

this.contents = contents;

}

/**

* @return the reference to the next item in the Linked List

*/

protected Node getNext() {

return next;

}

/**

* @param next the Node to set as the node to follow this item in the list.

*/

protected void setNext(Node next) {

this.next = next;

}

}

Main.java

public class Main{
        public static void main(String[] args) {    
            
        List lst= new List();    
            
        //Add nodes to the list   
                
        lst.toBack(new Node("Smith"));     //   Smith
        lst.toFront(new Node("John"));     // John , Smith 
        lst.toFront(new Node("Neo"));      // Neo , John , Smith
            lst.toBack(new Node("Andrewson")); // Neo , John , Smith , Andrewson
        
        //Displays the nodes present in the list    
        lst.print();    
                // Neo , John , Smith , Andrewson
    }    
}

ANY CLARIFICATIONS/MODIDICATIONS REQUIRED LEAVE A COMMENT

IF YOU ARE SATISFIED WITH THE ANSWER PLEASE LIKE THE POST


Related Solutions

JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface,...
JAVA: You're given 3 files. Demo.java, SampleInterace.java, and OverflowException.java. Use your Demo class to implement SampleInterface, and the OverflowException class should handle any errors that may come from the addNum method. Demo.java: public class Demo implements SampleInterface { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated constructor stub } @Override public void addNum(int value) throws OverflowException { // TODO Auto-generated method stub } @Override public void removeNum(int value) { // TODO Auto-generated method stub...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class...
JAVA PROGRAM Question : Design and implement two classes called InfixToPostfix and PostFixCalculator. The InfixToPrefix class converts an infix expression to a postfix expression. The PostFixCalculator class evaluates a postfix expression. This means that the expressions will have already been converted into correct postfix form. Write a main method that prompts the user to enter an expression in the infix form, converts it into postfix, displays the postfix expression as well as it's evaluation. For simplicity, use only these operators,...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT