I’m currently learning about linked and doubly linked lists. In both topics, I don’t understand one thing. Why should we return the removed item while removing head or tail? For example,
// Create your .removeHead() method below:
removeHead() {
const removedHead = this.head;
if (!removedHead) {
return;
}
this.head = removedHead.getNextNode()
if (this.head) {
this.head.setPreviousNode(null)
}
if (removedHead === this.tail) {
this.removeTail();
}
return removedHead.data;
}
Why do return removedHead.data? Please, help me to understand. I just don't get it why by returning the item, we are removing it and how the programm actually interprets it under the hood? Thanks
The way I understand your question is that you think that by returning the head of the list, the function removes it from the list. That is not the case.
First of all try to read the code line by line and write each state of an imaginary linked list on a piece of paper. It will help you analyze the code:
The function starts by taking the existing head and saving it in removedHead.
Then it finds the next item in the list (next node) starting from the current head. Since the current head will be removed, this next node will become the new head and the function sets the previous node of this new head to null. A head does not have a previous node (the head is the first node and has nothing that comes before it). Please note that if the next node of the head to be removed is null, then there is no next node and the list is empty.
Finally, if the removed head is the last element in the list, that means that the head is also the tail (a list with one element has 1 head which is also the tail). In this case we need to remove the tail as well which sets the tail pointer to null somewhere else in the code. The function removeTail() accomplishes that.
As a final step the function gives you the data of the discarded head. This is not necessary and if that last line of code is removed, it will have no impact on the rest of the code. It is there as an added feature in case you need to do something with it.
Because this.head seems to have a data object which probably contains information on the removed item and potentially also information on whether the delete was successful or not. By returning it the calling routine gets this information which it may use - or simply ignore because the returned value doesn't need to be processed.
Just use your debugger and take a look at the object to understand its contents.
Related
Per the discussion here, currently the ivh-treeview Angular library does not expand collapsed nodes when the tree is filtered and a leaf node matches the filter.
Would the right way to implement this behavior be to add an ng-change handler to the input element used for the filter text and then query the tree in the containing controller (thus, doing it manually)?
There doesn't appear to be any other mechanism by which to detect if the filter is changed.
BTW, there exists no ivh-treeview tag so I can't really tag this properly. Apologies if I've hijacked someone else's tag. That said it appears to be the tag used for other ivh-treeview related questions.
There's really no way to do this elegantly I'm afraid. The best you can do is expand the entire tree (with expandRecursive) when you detect the filter is non-empty (or past some threshold). By completely expanding the tree you get to "show filtered nodes" by virtue of the fact that everything else is hidden by ivhTreeview. That's the easy part. The hard part is when there's no longer a filter how do you restore the tree to it's previous partially-expanded state.
If I had to do it I'd probably end up walking the tree to grab the expanded state from each node before the recursive expand then restore that state when the filter is removed. This would clobber collapse/expand actions while there was a filter in play... you could handle that too but things start to get hairy very quickly. E.g.:
var isExpanded = ivhTreeviewOpts().expandedAttribute
// When the filter becomes non-empty
ivhTreeviewBfs(myTree, function(node) {
node.savedExpandedState = n[isExpanded]
})
ivhTreeviewMgr.expandRecursive(tree)
// When the filter becomes empty
ivhTreeviewBfs(myTree, function(node) {
node[isexpanded] = node.savedExpandedState
})
Here's a rough demo: http://jsbin.com/jijoma/3/edit?html,js,output
Hope that helps!
I used the bson_iter_type() to get the type of iter. But the results (0x27, 0x17,0x1a,0x1b) are not listed in the official document mongodb-api-document. Why?
After bson_iter_init (&iter, my_bson_doc), iter does not point at the first element, but something ahead of the first element. After bson_iter_init (&iter, my_bson_doc) and bson_iter_next (&iter), iter will point at the first element if there are at least one element in bson. So, my problem stems from that the return of bson_iter_type() is not the type of the first element, but other things.
Now, bson_iter_next (&iter) is called to make sure iter point at the first element before bson_iter_type(). My problem is solved.
First let me explain what I want to do. I have n data elements. Every element needs to be checked with every other element but not with itself. The function that checks the elements returns true if everything is ok. If something is wrong than the function deletes both elements and replace them with new ones. But the new ones need to be checked with every other element too. This will be repeated until every element was checked with every other and all checks are fine.
I am asking how to setup the data structure in a most efficient way. When I test ele1 with all other n-elements and all are fine and then I test ele2 with ele84 and both get replaced I need to check ele1 and ele2 again, if these are now not fine I need to check all for ele1 again. But how to remember in the most efficient way which elements need to be check and which don’t to avoid double checking of elements?
You can use three lists: Main, CurrentNew, and NextNew
Main is initialized with the elements - use a nested loop to check them all, if you delete any elements then add the new elements to NextNew
On the next iteration, NextNew becomes CurrentNew - allocate a new NextNew list. First use a nested loop to check all elements of CurrentNew with the other elements of CurrentNew, then check the elements of Main with the elements of CurrentNew. New elements go to NextNew. Note that you're not checking the elements of Main with the other elements of Main - you already know that they're valid with each other.
On the next and each subsequent iteration, merge CurrentNew into Main, then NextNew becomes CurrentNew, repeat until all elements are valid.
every time i want to get the Value of my DomAttr i get an TypeError:
My Code:
Wanted = page.getByXPath("//span[contains(.,'Some')]/parent::a/#href");
return this
[DomAttr[name=href value=URLSTRING]]
Now i want to geht the value (=URLSTRING) with Wanted.getNodeName();
but every Time i get the Error
Cannot find function getNodeValue in object [DomAttr[name=href value=
same when i use getValue
please help me
There are some things that make no sense in the code (particularly, because it is not complete). However, I think I can guess what the issue is.
getByXPath is actually returning a List (funny thing you missed the part of the code in which you specify it as a list and replaced it with a Wanted).
Note you should probably also have type warnings in the code too.
Now, you can see that the returned value is in square brackets. That means it is a List (confirming first assumption).
Finally, although you happened to miss that part of the code too, I guess you are directly applying the getValue to the list instead of the DomAttr elements in the list.
How to solve it: If you need more than 1 result iterate over the elements of the list (that Wanted word over there). If you need 1 result then user the getFirstByXPath method.
Were my guesses right?
When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but still get only one element back?
Changing the structure of the source is not possible, so I can't just select it with the id. It needs to get selected with CSS, but should just return one element.
You can also do
$$('.foo').first()
It looks cleaner than $$('.foo')[0] for my taste :)
It does not make sense to return a single element when selecting by class name because potentially there could be many elements in the DOM that have this class. So you could always use the first element of the returned array if you are sure that it will be unique.
$$('.foo')[0]