jasonsjones / doubly-linked-list

Javascript implementation of a doubly linked list data structure.
Other
30 stars 6 forks source link

Reverse iteration (tail -> head) + interrupt iteration #2

Closed nicolascouvrat closed 6 years ago

nicolascouvrat commented 6 years ago

Firstly, thanks for the nice module. I was working with it on a pretty large list where elements are appended by ascending order of date (the oldest first) and I needed to iterate only over the most recent part of the list. I therefore took the opportunity to edit the code ^.^ and added a reverse iteration functionality as well as an interrupt.

It works like this:

// assuming list is a dbly-linked-list containing [1,2,3,4,5]
list.forEach(function(node) {
  console.log(node.getData())
});
//will print 1, 2, 3, 4, 5
list.forEach(function(node) {
  console.log(node.getData())
}, true);
//will print 5, 4, 3, 2, 1
list.forEach(function(node) {
  console.log(node.getData());
  if (node.getData() === 2) {
     list.interruptEnumeration();
  }
}, true);
//will print 5, 4, 3, 2

Essentially, this adds an optional reverse boolean parameter in the forEach() function, so that it defaults to head -> tail enumeration if not specified.

I'm using it as it now, but I figured perhaps some other people might like this little modification. (And by the way, this is the first time I submit a pull request, so if I'm doing something wrong, I'll be glad to receive feedback !!)

jasonsjones commented 6 years ago

Hey @nicolascouvrat, thanks for the interest and for taking the time to make the PR! It looks like a great feature addition to the project and I'll give it a good look as soon as I get some time. It is feature freeze week here at my 'day job' so things are a bit busy, but I will be sure to give it a good review.

And for your first PR, it looks great. Updated docs and tests...thank you! I won't be too picky on the code review, but I can certainly make some comments to give you a feel for the things that I would look at an comment on in production code, if you like.

I just realized some of these projects could use some updating... ;-)

Oh, and BTW, I spent about 2 1/2 years living in Northern Japan--Misawa to be precise. And we loved it! Thanks again.

nicolascouvrat commented 6 years ago

Thanks ! I'll be glad to receive any comments you might have to give me, as you might have guessed I'm still critically lacking "real" experience so everything is good to take :)

Just took a look at Misawa. Aomori heh? How come you ended up all the way up there? Wanted to go tour there this summer but didn't have the time. God do I love the Japanese countryside for some reason ...

jasonsjones commented 6 years ago

Hey @nicolascouvrat! Excellent work. Congrats on your first PR! Apologies that it took so long to get to.

I ended up in Misawa during one of my active duty tours. I spent a little bit of time in the U.S. Navy. Misawa is a nice contrast from the hustle and bustle of the larger cities. And I agree; the countryside sure is beautiful. Thanks again.