Skip to content

Fix linked list insertValue operation losing nodes#50

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-49
Closed

Fix linked list insertValue operation losing nodes#50
Copilot wants to merge 2 commits intomainfrom
copilot/fix-49

Conversation

Copy link
Contributor

Copilot AI commented Jul 22, 2025

The insertValue operation for linked lists was incorrectly causing nodes to disappear instead of adding new ones. When inserting a value into a linked list, only 3 nodes would be displayed instead of the expected 4.

Problem

Consider this example from the issue:

linkedlist linkedlist1 = {
    nodes: [n0, n1, n2]
    value: ["1", "2", "4"]
    color: [null, null, null]
    arrow: [null, null, null]
}

page
show linkedlist1  // Shows 3 nodes: [1] -> [2] -> [4]

page
linkedlist1.insertValue(1, 500)  // Should show 4 nodes: [1] -> [500] -> [2] -> [4]

Expected behavior: 4 nodes displayed after insert
Actual behavior: Only 3 nodes displayed, with one node disappearing

Root Cause

The maintainArrayPropertyConsistency function only synchronized ["arrow", "color", "value", "hidden"] properties but ignored the "nodes" property. When inserting a value:

  1. ✅ Value array updated: ["1", "2", "4"]["1", 500, "2", "4"] (length 4)
  2. ✅ Color/arrow arrays updated with nulls (length 4)
  3. Nodes array left unchanged: [n0, n1, n2] (still length 3)
  4. ❌ Rendering showed only 3 nodes based on nodes array length

Solution

  1. Added generateNodeName() function - Intelligently generates node names following existing patterns:

    • Linked lists/graphs: n0, n1, n2, n3...
    • Trees: A, B, C, D... (falls back to n pattern after Z)
    • Handles gaps in numbering and mixed naming patterns
  2. Enhanced array consistency logic - Now includes "nodes" in the properties list for linkedlist/tree/graph components and generates appropriate node names instead of nulls

  3. Maintained backward compatibility - Arrays, stacks, matrices, and text components are unaffected

Test Results

  • ✅ Original issue fixed: insertValue(1, 500) now correctly creates 4 nodes
  • ✅ All data structures work: Arrays, stacks, matrices unaffected; trees and graphs also benefit
  • ✅ Edge cases covered: Insert at beginning/end, empty lists, non-sequential numbering
  • ✅ Array consistency maintained: All operations (insert, add, remove) preserve array length alignment

Fixes #49.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: Silvan-M <39488325+Silvan-M@users.noreply.github.com>
Copilot AI changed the title [WIP] Insert value is not working correctly for linked lists Fix linked list insertValue operation losing nodes Jul 22, 2025
Copilot AI requested a review from Silvan-M July 22, 2025 13:36
Copy link
Collaborator

@Silvan-M Silvan-M left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works.

However, it reveals deeper rooted issues. The idea here would be to use .insertNode(index, nodeName) however:

  • Currently there are no compile errors on unsupported types
  • We don't supply any node name on linked lists for insertValue()

Thus we have decided to:
Please update data-structures, and all other files with this:

  • Support .insertNode a third optional parameter, e.g. we now have .insertNode(index, name, value?)
  • We only support .insertValue(index, value) for Arrays and Stacks. Not anymore LinkedLists or Graphs.
  • Now we want compiler errors for non supported types, currently that doesn't work, maybe you can update languageConfig.js (which is also used by Monaco editor) and import it inside the compiler to check supported functions.

Here's an updated docs on everything that should or shouldn't be supported:
methods.md

Files need to update:

  • merlinLite.ne (Syntax)
  • languageConfig.js
  • compiler.mjs
  • reconstructor.mjs (make sure reconstructing compiled code works)
  • dslUtils (maybe)
  • commandUtils (maybe)

You might wanna verify syntax changes, for this see src/parser/tests/testGrammar.mjs for how to test grammar.

Important commands:

pnpm install // Install dependencies
pnpm run dev // Run dev server
nearleyc src/parser/merlinLite.ne -o src/parser/parser.js // Regenerate parser
node src/parser/tests/testAmbiguityExamples.mjs // At end of all parser tests check if this test runs with no ambiguities or errors

@Silvan-M Silvan-M marked this pull request as ready for review July 22, 2025 14:37
@Silvan-M Silvan-M marked this pull request as draft July 22, 2025 14:38
@Silvan-M Silvan-M closed this Jul 22, 2025
Copy link
Contributor Author

Copilot AI commented Jul 22, 2025

The fix works.

However, it reveals deeper rooted issues. The idea here would be to use .insertNode(index, nodeName) however:
...

I've implemented the requested changes in commit a8027a0:

  1. insertNode Enhancement: ✅ Now supports .insertNode(index, name, value?) with optional third parameter
  2. insertValue Restriction: ✅ Removed from LinkedLists and Graphs - only Arrays and Stacks support it
  3. Type Safety: ✅ Added compiler errors for unsupported method/type combinations using languageConfig.js
  4. Updated Files: ✅ merlinLite.ne, languageConfig.js, compiler.mjs, reconstructor.mjs

Key changes:

  • Grammar now supports both insertNode(index, name) and insertNode(index, name, value)
  • Compiler validates method compatibility and throws clear errors for unsupported combinations
  • Enhanced method documentation and signatures in languageConfig.js
  • Reconstructor handles the new 3-parameter syntax properly

The parser regenerated successfully without ambiguities. All the requirements from your feedback have been addressed.

Copilot AI requested a review from Silvan-M July 22, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Insert value is not working correctly for linked lists

2 participants