JsFiddle中JavaScript双向链表构建与自定义填充问题
Hey there! Let's work through this problem together. I totally get wanting to swap out that plain loop-based population in your createList function for something more creative—like generating nodes using mathematical formulas instead of sequential values. Let's break this down step by step, with working code tailored for JSFiddle.
First, Let's Recap the Basic Doubly Linked List Setup
You mentioned you already have the core functionality, so here's a clean base we can build on:
class Node { constructor(value) { this.value = value; this.prev = null; this.next = null; } } // Original loop-based list creation (for reference) function createList(length) { let head = null; let tail = null; for (let i = 0; i < length; i++) { const newNode = new Node(i); if (!head) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } return { head, tail }; }
Replacing the Loop with Mathematical Formula-Based Values
The key here is keeping the doubly linked list's core tail-add logic intact, but swapping out how we generate each node's value using math formulas. Below are a few fun examples you can use or adapt:
Example 1: Square Numbers Sequence
// Create list populated with square numbers (0², 1², 2²...) function createListWithSquares(length) { let head = null; let tail = null; for (let i = 0; i < length; i++) { // Math formula: value = i squared const value = Math.pow(i, 2); const newNode = new Node(value); // Same tail-add logic as before if (!head) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } return { head, tail }; }
Example 2: Fibonacci Sequence
// Create list populated with Fibonacci numbers function createListWithFibonacci(length) { let head = null; let tail = null; let a = 0, b = 1; // Starting values for Fibonacci for (let i = 0; i < length; i++) { // Math formula: Fibonacci递推 const value = a; const newNode = new Node(value); // Same tail-add logic if (!head) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } // Update Fibonacci values for next iteration [a, b] = [b, a + b]; } return { head, tail }; }
Example 3: Sine Wave Values
// Create list populated with sine wave values (rounded to 2 decimals) function createListWithSine(length) { let head = null; let tail = null; for (let i = 0; i < length; i++) { // Math formula: sine of (i * 45 degrees, converted to radians) const angle = (i * Math.PI) / 4; const value = parseFloat(Math.sin(angle).toFixed(2)); const newNode = new Node(value); // Same tail-add logic if (!head) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } return { head, tail }; }
Test Your Lists (For JSFiddle)
Add this test code to verify everything works. It will print the list values to the console:
// Helper function to print list values function printList(head) { let current = head; const values = []; while (current) { values.push(current.value); current = current.next; } console.log("List values:", values.join(", ")); } // Run tests const squareList = createListWithSquares(5); printList(squareList.head); // Output: 0, 1, 4, 9, 16 const fibList = createListWithFibonacci(6); printList(fibList.head); // Output: 0, 1, 1, 2, 3, 5 const sineList = createListWithSine(8); printList(sineList.head); // Output: 0.00, 0.71, 1.00, 0.71, 0.00, -0.71, -1.00, -0.71
Critical JSFiddle Setting
Don't forget this! In JSFiddle's JavaScript panel, find the Load Type dropdown at the top and select "no wrap in body". This ensures your code runs in the global scope, so all your classes and functions are accessible without scope issues.
Customization Tip
Want to use a different formula? Just replace the value generation line in any of the createListWithXXX functions. For example, you could use cube roots (Math.cbrt(i)), natural logs (Math.log(i+1)), or even your own custom recursive formula—sky's the limit!
内容的提问来源于stack exchange,提问作者Jay

