Azzera filtri
Azzera filtri

Inserted value failing to show up in list

1 visualizzazione (ultimi 30 giorni)
Manaal
Manaal il 5 Ott 2022
Risposto: Manas il 8 Set 2023
Hey,
I have created a Nodes2 class which has properties value, next, and previous. I am trying to create an insert function that can insert a node with the corresponding properties at any point in the doubly linked list. The DLL class only has the property head and is a handle class. My issue is that my inserted value won't add to the original list. Assume I have pushed 10 elements into the list, and I want to insert value 0 at position 3,
m = DLL();
for i = 1:10
m.push(i)
end
m.insert(m.head.next,0);
function insert(obj,previous, item)
if (nan(previous))
fprintf("The previous node does not exist.\n")
return;
end
% create new node with item
newNode = Nodes2(item);
% set the next and previous of the new node
newNode.next = previous.next;
previous.next = newNode;
newNode.previous = previous;
% change the next nodes previous node
if (isa(newNode.next, 'Nodes2'))
newNode.next.previous = newNode;
fprintf("\ndone\n");
end
node = obj.head;
fprintf("\nForward Traversal: ");
while (isa(node, 'Nodes2'))
fprintf(" {%d}",node.value);
node = node.next;
end
Note: nan is a function I wrote but works similar to isnan!
I am not sure where my logic is wrong. When I print within the function what the previous, and next values are, everything is in check. But when i print my entire list, the 0 has not been inserted.
Let me know your thoughts,
thanks!
  15 Commenti
dpb
dpb il 5 Ott 2022
Modificato: dpb il 5 Ott 2022
I'm guessing he'd run into the same issue that led to the above -- there is no footprint for his class for ismissing just as there wasn't for isnan.
Now, there's probably some other way to build the class to work around it, but I'm too old a dog for those new tricks so I've never delved into using user-defined classes or formal oop; we designed and built NSS Savannah and the Oconee class reactors with procedural code and FORTRAN; that's good enough for me! <VBG>
Walter Roberson
Walter Roberson il 5 Ott 2022
If Nodes2 were a handle class then I would not have gone anywhere near redefining nan() and isnan() or ismissing(): instead of using isnan(dt) I would simply use is ~isvalid(dt)

Accedi per commentare.

Risposte (1)

Manas
Manas il 8 Set 2023
Hi Manaal,
I understand that you are trying to define a function that takes in a node from a doubly linked list and inserts a new node right after the given node in that doubly linked list.
One aspect of the code you can inspect further are the addresses of the nodes. If the new node is not being inserted in the right place, I suspect the previous and next nodes are not being mapped properly. This would explain why the new node’s value is not printed when you run through the entire doubly linked list.
Another reason could be that you are dealing with variables that have different scope. This could result in the code not updating the main HEAD node object of the doubly linked list.
Also, please note that I can only speculate why the code is not behaving as expected as I don’t have access to the entire code and am not aware of the other operations you might be performing.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by