Question

Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

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();

   }

  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for NodeList.java:

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!!!!!!
*/
if(root == null)
   root = newNode;
else
{
    newNode.setNext(root);
    root = newNode;
}
size++;      
}
  
  
/*
* 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(Node nodeKey){
/*
*Implement This Method!!!!!!
*
Node temp = root;
if(root == nodeKey)   //If the first element is the search key.
   root = root.getNext();   //Remove it.
while(temp.getNext() != null)   //Keep reading till the end.
{
    if(temp.getNext() == nodeKey)   //If the next key is the search key.
    {
       temp.setNext(temp.getNext().getNext());   //Remove the next key, and stop.
       return;
    }  
    temp = temp.getNext();    //Keep reading the next node.
}     
}*/
public void removeFromBeginning()
{
    root = root.getNext();
}
  
/*
* 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!!!!!!
*/
Node temp = root;
while(temp != null)
{
   System.out.println(temp.getId() + " " + temp.getName());
   temp = temp.getNext();
}
}
}

And the output screenshot is:

Terminal Shell Edit View Window Help イ ⓖ 1.33 GB 0 < >し令못-)) 94% C Tue 28 Mar 00:07 ANANDA KUMAR THUMMAPUDI Currently Open Do

Add a comment
Know the answer?
Add Answer to:
Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...

    9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...

  • JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...

    JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private Link prev; public CircularList() { // implement: set both current and prev to null } public boolean isEmpty() { // implement return true; } public void insert(int id) { // implement: insert the new node behind the current node } public Link delete() { // implement: delete the node referred by current return null; } public Link delete(int id) { // implement: delete the...

  • Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the...

    Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...

  • Debugging exercise Programmer Began programming the following program, but made some mistakes. Please debug: class Student{...

    Debugging exercise Programmer Began programming the following program, but made some mistakes. Please debug: class Student{     private int id;     private String name;     private String city;       Student(String name, String city){         this.name = name;         this.city = city;     }        Student(int id, String place){         this.id = id;         this.place =name;     }        void Student(int id, String name, String city){         this.id = id;         this.name = name;         this.city =...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } 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 int get Sall) { return sal;...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT