Question: java code 1 Sparse vectors as linked lists Your first step is to download the Node and SparseVector classes from Canvas. Some of the methods
java code
1 Sparse vectors as linked lists Your first step is to download the Node and SparseVector classes from Canvas. Some of the methods are already implemented, such as toString and trivial getter/setters. In milestone one, you will implement SparseVector::addElement. For now, we'll assume that addElement is valid for increasingly larger indices. That is, every time you add a node to the SparseVector, it will be added to the end of the linked list (so long as it does not exceed the vector's length). You should check the index is valid before adding a node, and alert the user if they've tried to add an element incorrectly. Implement the SparseVector::addElement method so that the following code SparseVector vec = new SparseVector(6); vec.addElement(0, 10.0); vec.addElement(3, -1.1); vec.addElement(5, 32.0); System.out.println(vec); outputs the result: 10.0, 0, 0, -1.1, 0, 32.0 Hint: There are three important cases to account for: (1) invalid index, (2) head is null, (3), head is not null. Be aware that this: Node a = b.getNext(); a = new Node (index,value); does not set b's next pointer. Why not? 1 Sparse vectors as linked lists Your first step is to download the Node and SparseVector classes from Canvas. Some of the methods are already implemented, such as toString and trivial getter/setters. In milestone one, you will implement SparseVector::addElement. For now, we'll assume that addElement is valid for increasingly larger indices. That is, every time you add a node to the SparseVector, it will be added to the end of the linked list (so long as it does not exceed the vector's length). You should check the index is valid before adding a node, and alert the user if they've tried to add an element incorrectly. Implement the SparseVector::addElement method so that the following code SparseVector vec = new SparseVector(6); vec.addElement(0, 10.0); vec.addElement(3, -1.1); vec.addElement(5, 32.0); System.out.println(vec); outputs the result: 10.0, 0, 0, -1.1, 0, 32.0 Hint: There are three important cases to account for: (1) invalid index, (2) head is null, (3), head is not null. Be aware that this: Node a = b.getNext(); a = new Node (index,value); does not set b's next pointer. Why not
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
