Question

In: Computer Science

How do i get the code below to add an element before another without making the...

How do i get the code below to add an element before another without making the array off one element? (javascript)

public void addBefore(double element) {
itemCount++;
double data[] = new double[this.data.length];
if(currentIndex <= itemCount) {
if(currentIndex != 0) {
for(int index = currentIndex; index >= itemCount; index ++) {
data[index] = this.data[index];
}
currentIndex--;
data[currentIndex] = element;
}
if(currentIndex == 0) {
data[0] = element;
currentIndex = 0;
}
}
}

Solutions

Expert Solution

We can use linked list conpect to achive this.

Here is code:

// make a class called node.
class Node {
// which has data and next node
constructor(data) {
this.data = data;
this.next = null;
}
}
// alternative of array
class SingleLinkedList {
constructor() {
this.head = null;
this.itemCount = 0; // size of list
}

addBefore(data, indexAt) {
if (indexAt <= this.itemCount) {
let node = new Node(data); // create node
let current, previous;

current = this.head;
if (indexAt != 0) {
current = this.head;
for (let index = 0; index < indexAt; index++) {
previous = current;
current = current.next;
}
// adding an node at the position
node.next = current;
previous.next = node;
} else {
node.next = this.head;
this.head = node;
}
this.itemCount++;
}
}

}

var ll = new SingleLinkedList();

ll.addBefore(10, 0); // add 0th position [10]
ll.addBefore(20, 0); // add 0th position [20, 10]
ll.addBefore(30, 1); // add 0th position [20, 30, 10]
ll.addBefore(40, 0); // add 0th position [40, 20, 30, 10]

Output:


Related Solutions

How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
c++ How do I get it before a particular string when I use <ifstream>? For example,...
c++ How do I get it before a particular string when I use <ifstream>? For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple). And then I want to move on to the next row. How can i do?
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
How do I know if my company is going downhill ?(money wise) before I get bankrupt...
How do I know if my company is going downhill ?(money wise) before I get bankrupt because till this day I had no such problem but when the day comes I want to be prepare!!! What should I review or see.?
How do I know if my company is going downhill ?(money wise) before I get bankrupt...
How do I know if my company is going downhill ?(money wise) before I get bankrupt because till this day I had no such problem but when the day comes I want to be prepare!!! What should I review or see.?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT