New knots inserted into a single javascript
-
Handbag, help a handicapped. We need a method to insert a new node into the middle of a single contact list. stuck at this stage (UPD: self-sufficient part of the code):
// превращает массив в список function arrayToList() { var number; var list = {number: number, next: list};
for (var i = arrayToList.arguments.length; i > -1; i--) { if (i < arrayToList.arguments.length - 1) list = {number: arrayToList.arguments[i], next: list}; else list = {number: arrayToList.arguments[i], next: null}; } return list;
}
var list = arrayToList(1,2,3,4,5,7,8,9,10,11,12);
// должна вставлять 6 между 5 и 7, но не вставляет
function insert(node, list)
{
if (list.number < node && list.next.number > node) // вставка в середину
{
var newnode = {number: node, next: list.next};
list.next = newnode;
return list;
}
else if (list.number == node) // узел уже есть
return "node " + node + " already exists in the list!";
else if (list.next != null)
return insert(node, list.next); // собственно, источник проблемы
else // вставка в конец
{
var newnode = {number: node, next: null};
list.next = newnode;
return list;
}
}console.log(insert(6, list));
It's working, but it's not that, instead of the list, the part that starts with the knot that goes before the one I'm gonna insert. That is, instead of a knot1 - knot2 - ... - knot(n-1) - ... only the knot(n-1) - knot(n) - ... (where the knot(n) is the emplaced knot. It's a recurring challenge that starts a function with a short list every time. I got the problem, but I didn't find the solutions. Maybe a cycle? Or do you remember the whole list from the beginning to the end? We need help.
-
Because only the result is important.
insert
At the first level, you don't have to retrieve the results of internal calls, you'll always have to return it.list
// превращает массив в список function arrayToList(...numbers) { return numbers.reduceRight(function(list, number) { return { number: number, next: list }; }, null); }
var list = arrayToList(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12);
// должна вставлять 6 между 5 и 7, но не вставляет
function insert(node, list) {
if (list.number == node) // узел уже есть
return "node " + node + " already exists in the list!";if (list.number < node && list.next.number > node || list.next == null) // вставка в середину
{
var newnode = {
number: node,
next: list.next
};
list.next = newnode;
} else {
insert(node, list.next); // собственно, источник проблемы
}
return list;
}console.log(insert(6, list));
The cycle makes it even easier to run on the list until the conditions are met, add a new element to the right place and then return the questionnaire
// превращает массив в список
function arrayToList(...numbers) {
return numbers.reduceRight(function(list, number) {
return {
number: number,
next: list
};
}, null);
}var list = arrayToList(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12);
// должна вставлять 6 между 5 и 7, но не вставляет
function insert(node, list) {
if (list.number == node) // узел уже есть
return "node " + node + " already exists in the list!";var cur = list;
while (!(cur.number < node && cur.next.number > node || cur.next == null)) cur = cur.next;
cur.next = {
number: node,
next: cur.next
}return list;
}console.log(insert(6, list));