Singly-Linked Circular List List Representations

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

A singly-linked circular list is formed by setting the link in the last node to point back to the first node. It is illustrated in the following figure: Figure 1.108 Singly-Linked Circular List Data Structures 129 J.E.D.I Take note the the pointer to the list in this representation, points at the last elment in the list. With circular list, there is the advantage of being able to access a node from any other node. Circular list could be used to implement a stack. In such a case, insertion push could be done at the left end of the list, and deletion pop at the same end. Similarly, queue could also be implemented by allowing insertion at the right end of the list and deletion at the left end. The following code snippets show the three procedures: Inserts element x at the left end of circular list L void insertLeftObject x{ Node alpha = new Nodex; if L == null{ alpha.link = alpha; L = alpha; } else{ alpha.link = L.link; L.link = alpha; } } In insertLeft, if the list is initially empty, it will result to the following circular list: Figure 1.109 Insertion into an Empty List Figure 1.110 Otherwise, Data Structures 130 J.E.D.I Figure 1.111 Insertion into a Non-Empty List Inserts element x at the right end of circular list L void insertRightObject x{ Node alpha = new Nodex; if L == null alpha.link = alpha; else{ alpha.link = L.link; L.link = alpha; } L = alpha; } If the list is initially empty, the result of insertRight is similar to insertLeft but for a non- empty list, Figure 1.112 Insertion at the Right Deletes leftmost element of circular list L and stores in x Object deleteLeft{ Node alpha; Object x; if L == null throw new Exception“CList empty.”; else{ alpha = L.link; x = alpha.info; if alpha == L L = null; else L.link = alpha.link; } } Data Structures 131 J.E.D.I Performing deleteLeft on a non-empty list is illustated below: Figure 1.113 Delete from Left All these three procedures have O1 time complexity. Another operation that takes O1 time in circular lists is concatenation. That is, given two lists L 1 = a 1 , a 2 , ..., a m and L 2 = b 1 , b 2 , ..., b n where m, n is ≥ 0, the resulting lists are L 1 = a 1 , a 2 , ..., a m , b 1 , b 2 , ..., b n L 2 = null This could be done by simple manipulations of the pointer field of the nodes: Concatenate list L2 to this list L void concatenateL2{ if L2 = null if L = null{ Node alpha = L.link; L.link = L2.link; L2.link = alpha; } } If L2 is non-empty, the process of concatenation is illustrated below: Data Structures 132 J.E.D.I Figure 1.114 Concatenation of Two Lists

7.4.4 Singly-Linked List with Header Nodes