Question

In: Computer Science

In javascript fill in the method: /**    * Gets the node that would come before...

In javascript fill in the method:

/**
   * Gets the node that would come before the new one created to hold a
   * value (also known as the last node that contains a number less than the
   * value).
   *
   * @param value A value that is going to be inserted.
   * @return The last node that contains a number less than the value, or null
   * if no nodes contain numbers less than the value.
   */
   private Node getPrecedingNode(double value) {
       return null; // TODO: Fill in body.

   }

Solutions

Expert Solution

//=================linkedList.js================
class Node {
constructor(element)
{
this.element = element;
this.next = null
}
}
//======================
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
   //____________________
   insert(element){
   // creates a new node
   var node = new Node(element);
  
   // to store current node
   var current;

   if (this.head == null){
           this.head = node;
           this.size++;
           return;
       }
       if(element>=this.head.element){
           node.next=this.head;
           this.head=node;
           this.size++;
           return;
       }
current = this.head;
while (current.next) {
           if(element<=current.element && element>current.next.element){
               node.next=current.next;
               //add node
               current.next=node;
               this.size++;
               return;
           }
current = current.next;
}
// add node
current.next = node;
   this.size++;
   }
   //____________________
   getValue(value){
       var current= this.head;
       if(current==null)
           return null;
       while (current) {
           if(current.element<=value)
               return current.element;
           current=current.next;
       }
       return null;
   }
   //_________________
   print(){
       var current= this.head;
       while (current) {
           console.log(current.element)
           current=current.next;
       }
   }
} //end of class linkedlist


let list = new LinkedList();
list.insert(45);
list.insert(34);
list.insert(25);
list.insert(49);
console.log("Content of list");
list.print();
console.log("Element just less than 38");
console.log(list.getValue(38));
//============================================
//output


Related Solutions

[Javascript] Create a function that gets the total amount and percentage of covid case data for...
[Javascript] Create a function that gets the total amount and percentage of covid case data for each Age Group in the covid cases data set(which contains the property "Age Group" in each case array). Age Groups: 'younger than 18', 'between 19 to 28 Years', 'between 29 to 38 Years','between 39 to 48 Years','between 49 to 58 Years','between 59 to 68 Years','between 69 to 78 Years','between 79 to 88 Years', and'older than 89' the function getSummaryOfAges(data) should return an Object with...
PHP, JAVASCRIPT, MYSQL below is what I have. Can fill in the details for the database...
PHP, JAVASCRIPT, MYSQL below is what I have. Can fill in the details for the database 5. “Login to DB”, “Logout DB”, sub-menus of “File” 5.1 _____ When the user selects “Login to DB”, a window should popup asking the user to enter login and password. Your program should verify the login and password against the DV_User table in the datamining database. A corresponding message should be shown in the message area when the login failed or successful. If it...
-JavaScript task,  just need the layout, I will fill out the citations and details and aparagraphs. Create...
-JavaScript task,  just need the layout, I will fill out the citations and details and aparagraphs. Create a webpage that is a based on your research on The Pros and Cons of Internet Voting. Research the Pros and Cons of Internet Voting and create an informative webpage. Be sure to include: Use at least three references One of the references must be Joshua Conway, an internet voting expert from UJSC. Your sources are to be cited in a list of references...
Describe the sinoatrial node (SA node) membrane potential and action potential.  Describe how these would be affected...
Describe the sinoatrial node (SA node) membrane potential and action potential.  Describe how these would be affected by: 1) atropine, 2) norepinephrine, and 3) acetylcholine
Fill in the blanks. Before considering a product-orientedlayout, it is important to be certain that...
Fill in the blanks. Before considering a product-oriented layout, it is important to be certain that these four things are considered:____________________________________________________________________________________________________________________
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
Insert: this method takes a value as a parameter and adds a node which contains the...
Insert: this method takes a value as a parameter and adds a node which contains the value to the end of the linked list Delete: This method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list Find: this method takes a value as a parameter, and returns the index of the...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT