Singly Linked List In Javascript - How to implement when given functions and tests to pass
-------------------------------------------------------------------------------------------------------------------------
So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command.
Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you are doing so I can understand and write this myself. I think a join method may need to be used in the linked list section but i'm not sure.
I am currently using node.js to run and test this code by calling the 'runTests()' command to run the given tests.Thank you!
//--------------------------------
//FUNCTION
//--------------------------------
function join(list, delim) {
var retval = "[";
while (list instanceof Cons &&
!(list.tail() instanceof Nil)) {
retval += list.head().toString() + delim;
list = list.tail();
}
if (list instanceof Cons) {
retval += list.head().toString();
}
retval += "]";
return retval;
} // join
//___________________
//LINKED LIST IMPLEMENTATION WOULD BE HERE (NEEDS 'Cons', 'Nils', and 'List" to be declared
//-------------------------------
//--------------------------------
//TESTS THE LIST SHOULD PASS (Run Program with Node.js, Load it and use command 'runTests()' to test these
//--------------------------------
function runTest(test) {
process.stdout.write(test.name + ": ");
try {
test();
process.stdout.write("pass\n");
} catch (error) {
process.stdout.write("FAIL\n");
console.log(error);
}
} // runTest
function assertEquals(expected, received) {
if (expected !== received) {
throw ("\tExpected: " + expected.toString() + "\n" +
"\tReceived: " + received.toString());
}
} // assertEquals
function test_nil_join() {
let nil = new Nil();
assertEquals("[]",
nil.join(", "));
} // test_nil_join
function test_nil_toString() {
let nil = new Nil();
assertEquals("[]",
nil.toString());
} // test_nil_toString
function runTests() {
// ---begin tests for nil---
// instanceof
runTest(test_nil_instanceof_list);
// join
runTest(test_nil_join);
// toString
runTest(test_nil_toString);
} //runTests()
//-------------------------------------
//IMPORTANT TO NOTE
//-------------------------------------
// singly-linked list. Lists are constructed with two kinds of objects: // - A `Cons` object represents a non-empty list. It holds a single // element of the list, along with the rest of the list. // - A `Nil` object represents an empty list, containing no elements, // and no other elements. // These objects are not wrapped around anything; if you take a list, // you take `Cons` or `Nil` objects.
Here i am writing a code for singly linked list. You can edit code according to your need.
// User defined class node
class Node {
// constructor
constructor(element)
{
this.element = element;
this.next = null
}
}
// linkedlist class
class LinkedList {
constructor()
{
this.head = null;
this.size = 0;
}
// functions to be implemented
// add(element)
// insertAt(element, location)
// removeFrom(location)
// Helper Methods-
// size_Of_List
// PrintList
}
1. add(element)=>
// adds an element at the end
// of list
add(element)
{
// creates a new node
var node = new Node(element);
// to store current node
var current;
// if list is Empty add the
// element and make it head
if (this.head == null)
this.head = node;
else {
current = this.head;
// iterate to the end of
the
// list
while (current.next) {
current =
current.next;
}
// add node
current.next = node;
}
this.size++;
}
2. insertAt(element, index)
// insert element at the position index
// of the list
insertAt(element, index)
{
if (index > 0 && index >
this.size)
return false;
else {
// creates a new node
var node = new Node(element);
var curr, prev;
curr = this.head;
// add the element to the
// first index
if (index == 0) {
node.next =
head;
this.head =
node;
} else {
curr =
this.head;
var it = 0;
// iterate
over the list to find
// the position
to insert
while (it <
index) {
it++;
prev = curr;
curr = curr.next;
}
// adding an
element
node.next =
curr;
prev.next =
node;
}
this.size++;
}
}
3.removeFrom(index)=>
// removes an element from the
// specified location
removeFrom(index)
{
if (index > 0 && index >
this.size)
return -1;
else {
var curr, prev, it = 0;
curr = this.head;
prev = curr;
// deleting first element
if (index == = 0) {
this.head =
curr.next;
} else {
// iterate over
the list to the
// position to
removce an element
while (it <
index) {
it++;
prev = curr;
curr = curr.next;
}
// remove the
element
prev.next =
curr.next;
}
this.size--;
// return the remove
element
return curr.element;
}
}
4. size_of_list()
// gives the size of the list
size_of_list()
{
console.log(this.size);
}
5. printList()
// prints the list items
printList()
{
var curr = this.head;
var str = "";
while (curr) {
str += curr.element + " ";
curr = curr.next;
}
console.log(str);
}
Get Answers For Free
Most questions answered within 1 hours.