# Lists List Implementations Linked List Review Recall from

• Slides: 21

Lists List Implementations

Linked List Review • Recall from CMSC 201 – “A linked list is a linear collection of selfreferential structures, called nodes, connected by pointer links. A linked list is accessed by keeping a pointer to the first node of the list. This pointer to the first node of a list is typically named head. Subsequent nodes are accessed via a link pointer member that is stored in each node. ” 2

OO Linked List • In the OO world, the Linked List (LL) is a class with the public interface described last time. • The pointer to the first node is a private data member of the LL class • Other private data members may be defined if needed (tail, number of elements, etc) 3

Class Relationships • The List class contains a pointer to the first Node • The Node class contains a Data object as a data member (composition/aggregation) – The List and Node cannot/should not/will not know anything about the Data. – The List and Node MUST use only the Data’s public interface 4

LL diagram List Object Other Private Data members head Data Object Data next Data Node Object A singly linked list 5

Inserting into a Linked List • If the list is not sorted, why not always insert at the head? • If the list is sorted, search to find the proper place in the list, then insert. 6

Other Private Data members 10 42 63 head next Other Private Data members head Insert 17 at the head 99 17 next 10 42 63 next 99 7

Other Private Data members 10 42 63 head next Other Private Data members 10 17 42 63 head next Insert 17 in sorted order 99 99 8

Finding an element • Starting with the head pointer in the List, follow the “next” links until you find the element in question or get to the end of the list. • How many nodes will be accessed – In the best case? – In the worst case? – On average? • How is the performance different in a sorted linked list? 9

Pseudo-code for find( ) Node. Ptr find (const Data& d) { Node. Ptr Nodep = list. m_head; while(Nodep not NULL && not the one you want) go to the next element If Nodep not NULL, return success; If Nodep is NULL, return failure } 10

removal in Linked List • Find the node that contains the data element you want to remove. Make the node before the node to be removed point to the node after the node to be removed. (NULL if the last element is being deleted), then free the memory for the node to be removed 11

Diagram for remove( ) List Other Private Data members head To remove( 17 ), make the node with 10 point to the node with 42, then free the memory for the node with 17. 10 17 42 63 next 99 12

Thinking about remove( ) • A problem – we can’t use find( ) because we need to modify the node before the one that has the element we want to delete. Besides, find() doesn’t return a pointer to a node. • Two solutions – The code in remove( ) keeps track of two pointers – one used to find the node to be removed and one to “trail behind” and point to the one before the one to be removed – Write a private method called “find. Previous( )” that is called from remove( ) that finds the node before the one with the specified element and returns a pointer to it 13

remove (cont’d) • Problem – there may not be a node after the one being removed (we’re removing the last node in the list) • Solution – not a problem. The “next” pointer of the node before the one being removed will become NULL without any special code. 14

remove( ) (cont’d) • Another problem -- there is no node before the node being removed. This can only occur when we are removing the first node. • Solution – Special code to handle this case 15

remove( ) (cont’d) • What do we do if the element we are asked to delete is not in the list? • What do we do if duplicates are allowed in the list? 16

remove( ) pseudo-code remove (const Data& d) { if (removing first element) get a pointer to node being removed modify head pointer to point to 2 nd node (if no 2 nd element, head pointer = NULL) else get a pointer to the node being removed get a pointer to the node before the one to be removed (special code or find. Previous() ) modify next pointer of this “previous” node free memory of node being removed } 17

Making remove() easier • The code that makes remove( ) somewhat complex is the code for finding the node that comes before the node you want to remove. You either have an extra pointer or write a special (private) function that we’ve called find. Previous() • An alternative is to build the necessary pointer into the linked list. This pointer (usually called “prev”) points to the node that comes before this node. Then we can write an alternative version of find( ) (let’s call it find. It() ) that returns a pointer to the node and use “prev” to access the previous node. 18

Double Linked List Other Private Data members 10 17 42 63 99 head next prev Now when we want to remove( 17 ), we can call find. It( 17 ) and use the “prev” pointer as the one that points to the node before the one we want to remove. Care must be taken to perform the pointer modifications in the correct order. 19

Circular Linked List Some applications require a more sophisticated list. In this circular list, the “next” pointer of the last node points back to the first node instead of being NULL. This adds a little complication to find( ). Other Private Data members 10 17 42 63 99 head next next 20

A Doubly Linked Circular List The most general kind of list is not only circular, but doubly linked. The “next” pointer of the last node points to the first node and the “prev” pointer of the first node points to the last node. Other Private Data members head 10 17 42 63 99 next prev next prev 21