[go: nahoru, domu]

Linked list: Difference between revisions

Content deleted Content added
m Reverted 1 edit by 14.139.114.4 using STiki
Line 134:
 
===Using sentinel nodes===
[[Sentinel node]] may simplify certain list operations, by ensuring that the next or previous nodes exist for every element, and that even empty lists have at least one node. One may also use a sentinel node at the end of the list, with an appropriate data field, to eliminate some end-of-list tests. For example, when scanning the list looking for a node with a given value ''x'', setting the sentinel's data field to ''x'' makes it unnecessary to test for end-of-list inside the loop. Another example is the merging two sorted lists: if their sentinels have data fields set to +∞, the choice of the next output node does not need special handling for empty lists.
 
However, sentinel nodes use up extra space (especially in applications that use many short lists), and they may complicate other operations (such as the creation of a new empty list).