Question
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function,
Implement the following in java.
1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list.
2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root).
3. A traverse function, that iterates the list and prints the elements in the linked list.
For the insertAtBeginning(Node newNode) function:
1. Check if the root is null. If it is, just assign the new element to the root.
2. If the root is not null, assign the root to a temporary node, say oldRoot. Then, assign the newNode as root. At last, oldRoot is the next node of the new root.
For the removeFromBeginning() function:
1. Check if the root is null. If it is, then you can throw an exception. i.e., throw new RuntimeException(the list is empty)
2. Root should be erased. How to know the new root? Assign the next node of the root to the root itself
public class NodeList { private int size = 0; private Node root = null; /* * It has to take a new Node and add that to the beginning of the linked list. * If the list is empty, assign it as the "root". * @Param - Node */ public void insertAtBeginning(Node newNode) { /* *Implement This Method!!!!!! */ } /* * It has to take a Node and remove that node if you find it in the list * from the existing nodes otherwise dont do anything. * * @return void (if the list is empty, throw an error) */ public void removeFromBeginning(){ /* *Implement This Method!!!!!! */
} /* * It has to return the size of the NodeList * * @return size */ public int size() { return size; } /** * Start with the head and traverse, print till you reach null. */ public void iterate(){ /* *Implement This Method!!!!!! */ } }
public class Node {
private int id = 0; private String name = ""; private Node next; public Node(int id, String name) { this.id = id; this.name = name; this.next = null; } public Node getNext() { return next; }
public void setNext(Node node) { this.next = node; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; } public String toString() { return "ID : "+this.id+" Name : "+this.name; } }
public class Main {
/** * @param args the command line arguments */ public static void main(String[] args) { NodeList list = new NodeList(); Node node = new Node(1, "Book"); Node node2 = new Node(2, "Binder"); list.insertAtBeginning(node); list.insertAtBeginning(node2); System.out.println("Length : "+list.size()); Node node3 = new Node(3, "Glass"); list.insertAtBeginning(node3); Node node4 = new Node(4, "Pen"); list.insertAtBeginning(node4); System.out.println("Length : "+list.size()); System.out.println("List with all elements: "); list.iterate(); list.removeFromBeginning(); System.out.println("Length : "+list.size()); System.out.println("List with root removed: "); list.iterate();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started