Linked List Class in MATLAB - Insert node manually without insertAfter() - algorithm

I am trying to use the Linked List Class implementation in MATLAB.
Now, it says the only way to insert a node into the list is by using insertBefore() or insertAfter().
But I want to insert the node manually, by specifying the Next value of the new node, like
newnode = dlnode(new);
ptr.Next = newnode;
newnode.Next=ptrnxt;
Would this work? I cannot use the insertBefore() or insertAfter() in my particular application since I am not maintaining a pointer of the current node.
Details regarding the linked list class is given here.

No, I don't think that would work with just those three lines of code because it ignores all the other logic that occurs to make sure that the Next and Prev are set for newnode and ignores the update to ptrnxt that would need to occur so that its Prev is now newnode. (And the Next property is private so you would have to change that to public…)
It is unclear by what you mean with I cannot use the insertBefore() or insertAfter() in my particular application since I am not maintaining a pointer of the current node. Yet you have the nodes that you want to insert newnode between? I'm guessing that the order of your nodes (prior to the insertion of the new one) is …,ptr,ptrnxt,…. Then why not just use
newnode.insertAfter(ptr);
which would change the order to …,ptr,newnode,ptrnxt,… and all three properties for each node would be set correctly/automatically.
Else, you would have to change your code to something like
newnode = dlnode(new);
ptr.Next = newnode;
newnode.Prev = ptr; % to make sure that newnode points back to ptr
newnode.Next = ptrnxt;
ptrnxt.Prev = newnode; % to make sure that ptrnxt points back to newnode
Much easier and safer to use the insertAfter and insertBefore methods.

Related

The repetitive step in delete a specific node in a Linked List

//Deletes data given from the linked list
public void deleteByValue(T data) {
//if empty then simply return
if (isEmpty())
return;
//Start from head node
Node currentNode = this.headNode;
Node prevNode = null; //previous node starts from null
if(currentNode.data.equals(data)) {
//data is at head so delete from head
deleteAtHead();
return;
}
//traverse the list searching for the data to delete
while (currentNode != null) {
//node to delete is found
if (data.equals(currentNode.data)){
prevNode.nextNode = currentNode.nextNode;
return;
}
prevNode = currentNode;
currentNode = currentNode.nextNode;
}
}
}
Hi all, I am quite new to data structure and I am confused when I learned how to delete one specific value in the single linked list.
So when we traverse the LinkedList, we have a line like this
prevNode.nextNode = currentNode.nextNode;
I think this already means that we have connected "the previous node before the target node" and "the next node before the current node". Why do we still have these two lines after the traversing of the linked list?
prevNode = currentNode;
currentNode = currentNode.nextNode;
Are these two lines mean we are connecting the original previous node with the original next node? I always got lost when the code referred to the "currentNode".How can we tell which is the current "currentNode"?
Could someone help me with this? Visualized answer is appreciated. Thanks so much!
Why do we still have these two lines after the traversing of the linked list?
These are not after traversing; these are the traversing itself.
Some things to note:
An assignment to an attribute will mutate the list, like prevNode.nextNode = currentNode.nextNode does.
An assignment to a variable will not mutate the list, but can make a reference to a different node, like currentNode = currentNode.nextNode does. This really is the core of traversing: it makes the variable currentNode hop from one node to the next -- without changing anything to the list it is traversing.
In this specific algorithm, when the mutation with prevNode.nextNode = currentNode.nextNode is performed, the next thing that happens is a return -- so there is no further traversal happening.

Creating and visualizing a Linked List

I'm still new to coding and I'm trying to learn how to create a linked list. What does this part mean? I can't seem to visualize this in my head.
static class Node {
int data;
Node next;
Node(int d){
data = d;
next = null;}
}
So this is a simple visual of a linked list. Your code represents a node class, so thats just the framework for a node. But you can think of that code representing one node, so it could represent the fourth node in this diagram. Thus, its data value would be {D} and its next value would be null.
In a linked list, the next node object it represented by the next variable. So if the node you are looking at is the second node, then your next variable will be the third node.

Is there any way to delete any node in Binary Tree without having root node refrence?

I was trying for this problem but not able to crack it. We have a Binary Tree and I want delete a given node from the tree but we don't have reference of root node. I am getting an idea how can we do this without having reference of root node. We do have the reference to node5, like in the image.
Update considering the new edit.
Since this is NOT a Binary Search Tree, in this case I'm assuming the node order in the tree is not relevant and the tree will not have duplicates, so removing node5 will be possible with the same data structure we defined above, but we need to include a parent node reference (for step 3 detailed below):
node {
d: data
leftChild: node
rightChild: node
parent: node
}
Now we can remove node5 modifying the tree without accessing/modifying the root (node1).
You'll have to do the following:
(Remember that node 5 is also a Binary subtree, so after
modifying it, we want the whole Binary Tree to still remain one!)
Find node5's deepest node. You need to traverse node5's node reference as a tree.
(*Be careful with how you pass the arguments. root should be a copy,
so that it doesn't mess with node5' as a root, but level and
deepestNode should be references.)
findDeepestNode(root: node, level: integer, deepestNode: node) {
if (root != null) {
level = level + 1
find(root.left, level)
if (level > deepestlevel) {
deepestNode = root; // node reference to the deepestNode
deepestlevel = level;
}
find(root.right, level);
}
}
Once executing this, deepestNode in your case will be node7 or node8. It doesn't matter.
Now make node5's value equal to deepestNode's value. In our pseudocode:
node5.data = deepestNode.data // Replacing node5's value with the deletingNode's one.
The tree now has a duplicate value. You still have deepestNode. So now you assign deepestNode parent's reference to null and delete
deepestNode as a reference. This step is the reason we need to include a parent node reference.
Your removal is complete!
Given nothing else but the tree itself, then you'll always have to search for the node you want to remove before removing it. If you can set yourself up for success by keeping pointers to the nodes you may want to remove beforehand, then the answer could be yes!

Updating value of node in Scala?

I have this code
tree match {
case EmptyNode =>
//do stuff with empty node
case Node(left, right, value) =>
//do stuff with node contents
}
However what if I want to update the value of a node? I tried doing value = newValue inside the case, but it doesn't seem to be allowed.
Structure of my trees/nodes:
sealed trait Tree
case class Node(var left: Tree, var right: Tree, var value: String) extends Tree
case object EmptyNode extends Tree
You can reference the actual node with # notation:
case n#Node(left, right, value) => n.value = "newValue"
Noah's answer does show the way to change the value of the node, but you shouldn't do that. One of the main ideas of scala is to promote immutable data structures, i.e. once you define an object, you don't change it.
This is particularly important in a tree, because by changing the value of the node, you are potentially making your tree inconsistent (values to the left of the node may no longer be smaller). Instead, always keep nodes values immutable and add or delete nodes as needed, minding rebalancing of the tree when a node is deleted.

Removing a single pointer from a boost::ptr_list list?

If I create a bunch of elements in a boost::ptr_list container how can I remove individual pointers from it? Say I do this:
boost::ptr_list intlist;
int *i = new int(3);
intlist.Add(i);
int *i2 = new int(1);
intlist.Add(i2);
int *i3 = new int(6);
intlist.Add(i3);
How can I remove say i3 and not i or i2 from the list?
The pop_back() command deletes the last element of a list. Boost's implementation of ptr_list encapsulates a std::list, so all of the commands on this page are equally valid with Boost's pointer wrappers.
Since you changed your question, see the erase command. You won't find an answer except by using the std::list interface.

Resources