"Algorithmic Refinement _c Language description" list--some questions and answers about the characteristics of the linked list

Source: Internet
Author: User
Tags prev
one, the list and array have their own advantages, which is suitable for the use of arrays, what is the appropriate use of linked lists.

When we expect frequent inserts and deletions, the list is more advantageous than the array. However, arrays are more advantageous when we expect random access to be higher than the number of insert and delete operations. Random access is the strength of arrays, because their elements are sequentially arranged in memory. This sequential arrangement, using any element of the array, can be accessed through its index in the time of O (1).

To access the elements in a linked list, we must have a pointer to the element. If we don't know much about how to access an element, the cost of getting a pointer to a particular element is very high. In practice, for most applications, we should at least traverse a part of the list. If the total amount of data stored is constant, there is a greater advantage in using arrays because they do not need to add extra pointers to "link" all their elements. second, the link table and array elements of the insertion, deletion and access compared to the difference.

In the operation of a linked list, all other operations have an O (1) run-time complexity in addition to destroying the linked list (of course, this performance is really difficult to control). One thing is certain, for many linked lists, the cost of getting pointers to a particular element in a list is high. For example, you might want to traverse the entire list in the worst case scenario. The cost at this point is O (n), where n represents the number of elements in the list. On the other hand, in a well-designed application, such as memory page frame management, there is no additional performance overhead. Therefore, it is very important to observe the characteristics of the application.

For arrays, inserts and Deletes are all O (n)-level operations, because in the worst case, inserting or deleting an element with an index of 0 requires that the other elements move one position to adjust the layout of the array. If we know the index value, then accessing the element in the array is the operation of O (1).
List_rem_next is used to remove the next element of the element specified by the parameter from a single linked list in the implementation of a single linked list. Why is a single list not able to provide an operation to directly remove an element specified by a parameter? And the Dlist_remove in the double linked list can be done, why. (The same question is true for the implementation of the circular chain list)

In the implementation of a single linked list and a cyclic list, each element does not have a pointer to its predecessor node. Therefore, we have no way to point the next pointer of the predecessor node to the successor node of the removed element. In the implementation of a two-way list, we can precisely remove the element specified by the parameter, because each element contains a prev pointer to its predecessor node. four, in the linked list head insert elements, if it is a single linked list, you need to set the parameter element elements to NULL, and two-way linked lists and circular linked lists do not need, for what reason.

The head insert element in a doubly linked list can be done by the Prev pointer of the head node itself. Inserts an element into the table head of a circular list, and you can use the Clist_ins_next function to insert the new node after the trailing element, because in the loop list, the next pointer to the tail node points back to the head node.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.