undergroundzuloo.blogg.se

Reading linked list stack backwards
Reading linked list stack backwards











reading linked list stack backwards

The client code's concerns now do not include that ugliness. The navigation is all handled within the NodeList class.

Reading linked list stack backwards code#

Note that there is no variable needed in the client code to track what's going on.

reading linked list stack backwards

To the code! Here is the main function which initializes the NodeList with an arbitrary, Java-like line of code and then proceeds to peek and move in the token list. There is no error or bounds checking of any kind but it demonstrates my suggestion fairly well, IMHO. There is a POJO-style class to represent a node or token and finally the utility class which exposes a specialized set of functions, or API. There is a class to hold the main function which demonstrates usage of the collection class and also acts as a poor-man's unit test. It is a bit different from the code shown below due to the complexities imposed by an online Java compiler but the code is easily accessible and fully executable.Ĭode sample notes: This is a full, working sample which contain three classes necessary to demostrate the concepts. Here is a link to an Ideone implementation of my suggested solution. With those collection types you would also be forced to traverse through the collection just to peek at an object which hurts performance further and also makes implementation that much harder. I wasn't considering performance but as it happens that ArrayList's random access functions are mostly O(1) rather than O(n) which you would get if you used an iterator or linked list. I chose ArrayList because of its random access capabilities and chose not to extend to help stay away from manipulating the ArrayList directly from client code and getting back into a spaghetti mess. There are a hundred ways to do this but in my code sample below I chose to compose with rather than extend the ArrayList class. One way to help yourself is to create a specialized collection class that implements functions which, for your problem domain, hide the ugly particulars of array navigation such as tracking the current position, iterating N steps back and forth, "peeking" back and forth, etc. You're getting spaghetti code because you're not following SoC. Is there any built in way in Java saving me from this? I'm thinking of coding my own linked list and just hitting node.next().next() if I want to go two steps forward, or a loop repeatedly hitting it if I want to go longer than that. Right now, I'm trying out iterators but I have the same problem as with indexes: I have to decrease and increase back again to where I was at since next() moves the cursor instead of just "peeking ahead". I want a node and I want to be able to look ahead for two or three positions, or back, and I didn't really find any good tools for that at that place. I then looked into linked lists, but they don't quite work like I want them too. I then tried to use an index, but it was beyond unpleasant to debug that since I had to think about decreasing, increasing and getting the current position in a pinch (I even had the objects store an int of where they were at) all the while keeping within range of the list, so it was an ugly, hard to read mess of spaghetti code too at that. I've tried saving the next and previous item as a copy, but then I figured out that I need to look arbitrarily far ahead or back. I'm coding a lexical analyzer in java and need to look backwards or forwards easily in a list of custom datatypes (my tokens).













Reading linked list stack backwards