Please write the code in Java. Week 2 is not necessary at this point. 1 Overview
ID: 3679855 • Letter: P
Question
Please write the code in Java. Week 2 is not necessary at this point. 1 Overview Over the next 2 weeks we'll be writing a doubly linked list whose nodes hold an integer. They can hold anything else you want to use. The requirements are split into week 1 and week 2. You are free to work ahead and submit more than the minimal requirement for week 1. Remember: it is better to submit a program that works and is missing a few features than to try to implement all the features at once and having a program that doesn't work. week 1 Create a node class that has the necessary fields for a singly linked list and a data field that holds a number. Create methods to insert, remove, and edit nodes Create a method to traverse the list and print all the data. Create a main method that instantiates a list and demonstrates that it works. week 2Explanation / Answer
/**
*Exception to indicate that Singly LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException{
public LinkedListEmptyException(){
super();
}
public LinkedListEmptyException(String message){
super(message);
}
}
/**
*Node class, which holds data and contains next which points to next Node.
*/
class Node {
public int data; // data in Node.
public Node next; // points to next Node in list.
/**
* Constructor
*/
public Node(int data){
this.data = data;
}
/**
* Display Node's data
*/
public void displayNode() {
System.out.print( data + " ");
}
}
/**
* Singly LinkedList class
*/
class LinkedList {
private Node first; // ref to first link on list
/**
* LinkedList constructor
*/
public LinkedList(){
first = null;
}
/**
* Inserts new Node at last of Singly LinkedList.
*/
public void insertLast(int data){
Node newNode = new Node(data); //Creation of New Node.
if(first==null){ //means LinkedList is empty, make first point to new Node.
first=newNode; //first ---> newNode
return;
}
Node tempNode = first; // save reference to first Node in tempNode- so that we could return saved reference.
while(tempNode.next!=null){ //Executes until we don't find last Node of LinkedList.
//If next of some Node is pointing to null, that means it's a last Node.
tempNode=tempNode.next; //move to next Node.
}
tempNode.next=newNode; //make last's Node next point to new Node
}
/**
* Deletes last Node from Singly LinkedList
*/
public Node deleteLast(){
//Case1: when there is no element in LinkedList
if(first==null){ //means LinkedList in empty, throw exception.
throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
}
//Case2: when there is only one element in LinkedList
if(first.next==null){ //means LinkedList consists of only one element, delete that.
Node tempNode = first; // save reference to first Node in tempNode- so that we could return saved reference.
first=first.next; // delete firstNode (make first point to secondNode)
return tempNode; //return deleted Node.
}
//Case3: when there are atLeast two elements in LinkedList
Node previous=null;
Node current=first;
while(current.next!=null){//Executes until we don't find last Node of LinkedList.
//If next of some Node is pointing to null, that means it's a last Node.
previous=current;
current=current.next; //move to next node.
}
previous.next=null; //Now, previous is pointing to second last Node of LinkiedList,
//make it point to null [it byPasses current Node(last Node of LinkedList) which was in between]
return current;
}
/**
* Display LinkedList
*/
public void displayLinkedList() {
System.out.print("Displaying LinkedList [first--->last]: ");
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null){ // Executes until we don't find end of list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
}
System.out.println();
}
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* Main class - To test Singly LinkedList.
*/
public class SinglyLinkedList {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList(); // creation of Linked List
System.out.println("Inserting 11");
linkedList.insertLast(11);
System.out.println("Inserting 21");
linkedList.insertLast(21);
System.out.println("Inserting 59");
linkedList.insertLast(59);
System.out.println("Inserting 14");
linkedList.insertLast(14);
System.out.println("Inserting 139");
linkedList.insertLast(39);
linkedList.displayLinkedList(); // display LinkedList
System.out.print("Deleted Nodes: ");
Node deletedNode = linkedList.deleteLast(); //delete Node
deletedNode.displayNode(); //display deleted Node.
deletedNode = linkedList.deleteLast(); //delete Node
deletedNode.displayNode(); //display deleted Node.
System.out.println();// sysout used to format output
linkedList.displayLinkedList(); //Again display LinkedList
}
}