Norm Radder wrote:What are the links that control the list? What's in the head and tail objects?
when there are no nodes in the Implemented Linked List, the head.next points to tail and tail.prev points to head.
Norm Radder wrote:
when there are no nodes in the Implemented Linked List, the head.next points to tail and tail.prev points to head.
what about head.prev and tail.next?
Where do the links for head and for tail point when there are nodes in the list?
Note: I always use paper and pencil (not pen) to draw out linked lists to see where the links go. Then when changing the links, I erase the current links and draw the new ones, taking notes of the correct order to do it to preserve the linking.
Norm Radder wrote:Where do the links for head and for tail point when there are nodes in the list?
When are the links in the head and tail nodes updated?
Naziru Gelajo wrote:Well tail.prev is always pointing to the newestNode as the node is supposed to be at the end of the list when using the add method.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Naziru Gelajo wrote:Well tail.prev is always pointing to the newestNode as the node is supposed to be at the end of the list when using the add method.
Really? Are you saying that you can ONLY add nodes at the end, because that kind of defeats the goal of a linked List, and actually sounds more like a stack or queue. It also suggests that double-linking may be redundant.
An alternative that may be worth considering is a LinkedRing, where the head and tail Nodes are the same. This has few downsides and one major upside: you don't have to change logic when the ring is "empty".
BTW, such a Node is often called an "anchor".
Winston
Stefan Evans wrote:I said it before. I'll say it again.
If you are adding an item at the tail of the list, you shouldn't need to modify the head node.
The ONLY case where you actually do is where you have an empty list (and even then you never need to actually refer to it as "head").
Every other case, you shouldn't be touching it.
More hints. Consider adding E1 E2 and E3 to an empty list - you would end up with the following progression:
HEAD - TAIL
HEAD - E1 - TAIL
HEAD - E1 - E2 - TAIL
HEAD - E1 - E2 - E3 - TAIL
For that last step adding E3 to the end of the list effectively what you have to do is:
E3.next = TAIL
E3.prev = E2 (tail.prev)
tail.prev = E3 (the new element)
E2.next = E3
But how would you get the reference to E2 to set its next link?
Stefan Evans wrote:I said it before. I'll say it again.
If you are adding an item at the tail of the list, you shouldn't need to modify the head node.
The ONLY case where you actually do is where you have an empty list (and even then you never need to actually refer to it as "head").
Every other case, you shouldn't be touching it.
More hints. Consider adding E1 E2 and E3 to an empty list - you would end up with the following progression:
HEAD - TAIL
HEAD - E1 - TAIL
HEAD - E1 - E2 - TAIL
HEAD - E1 - E2 - E3 - TAIL
For that last step adding E3 to the end of the list effectively what you have to do is:
E3.next = TAIL
E3.prev = E2 (tail.prev)
tail.prev = E3 (the new element)
E2.next = E3
But how would you get the reference to E2 to set its next link?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |