In: Computer Science
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;
}
}
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