Sequential Representation of Singly-Linked Linear List Linked Representation of Singly-Linked Linear List

J.E.D.I Figure 1.103 A Generalized List Figure 1.104 In the example, list L has four elements. The first element is the list a, b, c, , the second is the atom d, the third is the null set and the fourth is the list e, , f, g, a, d.

7.4 List Representations

A way to represent a list is to organize the elements one after another in a sequential structure like an array. Another way to implement it is to chain nodes containing the elements of the list using linked representation.

7.4.1 Sequential Representation of Singly-Linked Linear List

In sequential representation, the elements are stored contiguously. There is a pointer to the last item in the list. The following shows a list using sequential representation: Figure 1.105 Sequential Representation of List With this representation, it would take O1 time to access and update the i th element in the list. By keeping track of the last item, it would take O1 time to tell if the list is empty and to find the length of the list. It has a condition, though, that the first element should always be stored at the first index L0. In that case, insertion and deletion would require shifting of elements to ensure that the list satisfies this condition. In the worst case, this would entail shifting all the elements in the array, resulting to time complexity of On for insertion and deletion, for n elements. In combining two lists, a larger array is required if the combined size will not fit in any of the two lists. That would entail copying all the elements of the two lists into the new list. Duplicating a list would require traversing the entire list, hence, a time complexity of On. Searching for a particular value would take O1 time if the element is the first one in the list; however, worst case is when it is the last, where traversal of the entire list would be necessary. In such a case, the time complexity is On. Sequential allocation, being static in nature, is a disadvantage for lists with unpredictable size, i.e., the size is not known at the time of initialization, and with a lot of insertions and deletions, would eventually need to grow or shrink. Copying the overflowed list into a larger array and discarding the old one would do, but it would be a waste of time. In such cases, it would be better to use linked representation. Data Structures 128 J.E.D.I

7.4.2 Linked Representation of Singly-Linked Linear List

A chain of linked nodes could be used to represent a list. The following is an illustration: Figure 1.106 Linked Representation of List Figure 1.107 To access the i th element with linked allocation, the list has to be traversed from the first element up to the i th element. The worst case is when i = n, where n is the number of elements. Hence, the time complexity of accessing the i th element is On. Similarly, finding the length would entail traversing the entire list, so a time complexity of On. If insertion is done at the beginning of the list, it would take O1 time. However, with deletion and update, search has to be performed, resulting to a time complexity of On. To tell if the list is empty, it would take constant time, just like in sequential representation. To copy a list, every node is copied as the original list is traversed. The following table summarizes the time complexities of the operations on lists with the two types of allocation: Operation Sequential Representation Linked Representation Determining if a list is empty O1 O1 Finding the length O1 On Accessing the i th element O1 On Updating the i th element O1 On Deleting the i th element On On Inserting a new element On O1 Sequential representation is appropriate for lists that are static in nature. If the size is unknown beforehand, the use of link allocation is recommended. In addition to singly-linked linear, there are more varieties of linked representation of lists. Singly-linked circular, doubly-linked and list with header nodes are the most common of these varieties.

7.4.3 Singly-Linked Circular List