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...

  • 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 =...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • How can I solved my Java Program for DelivC

    //Graph Class: import java.util.ArrayList; //Graph is a class whose objects represent graphs.  public class Graph {     ArrayList<Node> nodeList;     ArrayList<Edge> edgeList;         public Graph() {         nodeList = new ArrayList<Node>();         edgeList = new ArrayList<Edge>();         }         public ArrayList<Node> getNodeList() {         return nodeList;    }    public ArrayList<Edge> getEdgeList() {         return edgeList;    }    public void addNode(Node n) {         nodeList.add(n);    }    public void addEdge(Edge e) {         edgeList.add(e);    }    public String toString() {         String s = "Graph g.\n";         if (nodeList.size() > 0) {             for (Node n : nodeList) {         // Print node info         String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";         s = s.concat(t);         }         s = s.concat("\n");             }         return s;     }  } // Node Class: import java.util.ArrayList;  // Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.  public class Node {    String name;     String val; // The value of the Node     String abbrev; // The abbreviation for the Node     ArrayList<Edge> outgoingEdges;     ArrayList<Edge> incomingEdges;             String color; //Create the color of the TYPE Node List     int start; //Create the Starting Time     int end; //Create the Ending Time             public Node( String theAbbrev ) {         setAbbrev( theAbbrev );         val = null;         name = null;         outgoingEdges = new ArrayList<Edge>();         incomingEdges = new ArrayList<Edge>();     }         public String getAbbrev() {         return abbrev;     }         public String getName() {         return name;     }         public String getVal() {         return val;     }         public ArrayList<Edge> getOutgoingEdges() {         return outgoingEdges;     }         public ArrayList<Edge> getIncomingEdges() {         return incomingEdges;...

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
ADVERTISEMENT