But before that I want to define what are "head" and "tail". Head is the node in the linked list, and the tail is linked list which the head refers to.
Back to "walking". It means to access the "head" of each linked list, do something with it's cargo (or don't do anything until you find a specific data value you are looking for) and maybe with the node itself (like change it's reference), and AFTER MAKING SURE YOU ARE DONE WITH THE NODE go to the head of the linked list, previous node refers to. It is important, because one does not simply access a node after starting working with the next one. Getting back to it would require you to start walking from the beginning of the list, and would be super inefficient.
LLists are useful for insertions and deletions, since they don't require changing all the values after the inserted one. For example if we had a list of 10 items, and wanted to insert one more item called Z between items 1 and 2, we had to move all of 2, 3, 4, 5, 6, 7, 8, 9, 10 by one, because of memory addresses they are at. However with linked list, we can just refer 1 to Z, and Z to 2, and everything else can stay unchanged.
On the other side there are 2 drawbacks of using them:
1) is time and amount of work it would take to access a value at node 10, because we would have to walk along all the values from 1 to 9 inclusive.
2) It's easy to make mistakes, like the one described in caps-lock in paragraph 3.
LLists are useful for insertions and deletions, since they don't require changing all the values after the inserted one. For example if we had a list of 10 items, and wanted to insert one more item called Z between items 1 and 2, we had to move all of 2, 3, 4, 5, 6, 7, 8, 9, 10 by one, because of memory addresses they are at. However with linked list, we can just refer 1 to Z, and Z to 2, and everything else can stay unchanged.
On the other side there are 2 drawbacks of using them:
1) is time and amount of work it would take to access a value at node 10, because we would have to walk along all the values from 1 to 9 inclusive.
2) It's easy to make mistakes, like the one described in caps-lock in paragraph 3.
No comments:
Post a Comment