Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

The models used to analyse different national cultures.

Answered: 1 week ago

Question

1. How do most insects respire ?

Answered: 1 week ago

Question

Who is known as the father of the indian constitution?

Answered: 1 week ago

Question

1.explain evaporation ?

Answered: 1 week ago

Question

Who was the first woman prime minister of india?

Answered: 1 week ago

Question

Explain the concept of going concern value in detail.

Answered: 1 week ago

Question

=+country competitive advantages? Why? Support your point of view.

Answered: 1 week ago

Question

=+from: a) a MNEs perspective? and b) the HRM managers perspective?

Answered: 1 week ago