Linked List Linear list concepts Linked List Complex linked list Faculty of Computer Science and Engineering – HCMUT Slide 1 Linear List Concepts A linear list is a data structure each element of which has a unique successor element 1 element 2 element 3 Array is the simplest linear list Faculty of Computer Science and Engineering – HCMUT Slide 2 Linear List Concepts General list: no restrictions on where data can be inserted/deleted, and on which operations can be used on the list – Random list: there is no ordering on data – Ordered list: data are arranged according to a key Restricted list: data can be inserted/deleted and operations are performed only at the ends of the list – FIFO (First-In-First-Out): queue – LIFO (Last-In-First-Out): stack Faculty of Computer Science and Engineering – HCMUT Slide 3 Linear List Concepts Basic operations – Insertion – Deletion – Retrieval – Traversal Faculty of Computer Science and Engineering – HCMUT Slide 4 Insertion Random list: insertion can be made at the beginning, the middle or the end of the list Ordered list: the data must be inserted so that the ordering of the list is maintained Array requires physical shifting 25 10 20 30 10 20 25 Faculty of Computer Science and Engineering – HCMUT 30 Slide 5 Deletion Deletion from a general list requires searching the list in order to locate the data being deleted Array requires physical shifting after the data is deleted Faculty of Computer Science and Engineering – HCMUT Slide 6 Retrieval and Traversal Retrieval also requires list searching, but does not change the contents of the list Traversal is retrieval of all elements in sequence Faculty of Computer Science and Engineering – HCMUT Slide 7 Linked Lists A linked list is an ordered collection of data in which each element contains the location of the next element Element = Data + Link head data link Empty linked list Faculty of Computer Science and Engineering – HCMUT Slide 8 Nodes A node with one data field number A node with three data fields name id number A node with one structured data field name id number Faculty of Computer Science and Engineering – HCMUT Slide 9 Nodes Linked List Structure count list count <integer> head <pointer> end list head Data node structure data link dataType node key <keyType> data <dataType> field1 <…> link <pointer> field2 <…> end node … fieldN <…> end dataType Faculty of Computer Science and Engineering – HCMUT Slide 10 Linked List Algorithms Create list Insert node Delete node Search list Retrieve node Destroy list Faculty of Computer Science and Engineering – HCMUT Slide 11 Create List Before list ? ? count head list.head = null list.count = 0 After list 0 count head Faculty of Computer Science and Engineering – HCMUT Slide 12 Create List Algorithm createList (ref list <metadata>) Initializes metadata for a linked list Pre list is a metadata structure passed by reference Post metadata initialized 1 list.head = null 2 list.count = 0 3 return End createList Faculty of Computer Science and Engineering – HCMUT Slide 13 Insert Node Allocate memory for the new node and set up data Point the new node to its successor Point the new node's predecessor to it Faculty of Computer Science and Engineering – HCMUT Slide 14 Insert into Empty List Before 75 0 count head list pNew pNew -> link = list. head list.head = pNew After 75 1 count head list pNew Faculty of Computer Science and Engineering – HCMUT Slide 15 Insert at the Beginning Before 75 1 count head list 39 pNew pNew -> link = list.head list.head = pNew After 2 75 count head list 39 pNew Faculty of Computer Science and Engineering – HCMUT Slide 16 Insert in Middle Before 2 39 75 head count list 52 pPre pNew pNew -> link = pPre -> link pPre -> link = pNew After 39 3 count 75 head list 52 pPre pNew Faculty of Computer Science and Engineering – HCMUT Slide 17 Insert at End Before 3 39 52 75 head count list 134 pPre pNew -> link = pPre -> link pPre -> link = pNew After 4 39 count pNew 52 75 head list 134 pPre Faculty of Computer Science and Engineering – HCMUT pNew Slide 18 Insert Node Algorithm Algorithm insertNode (ref list <metadata>, val pPre <node pointer>, val dataIn <dataType>) Inserts data into a new node in the linked list Pre list is metadata structure to a valid list pPre is pointer data’s logical predecessor dataIn contains data to be inserted Post data have been inserted in sequence Return true if successful, false if memory overflow Faculty of Computer Science and Engineering – HCMUT Slide 19 Insert Node Algorithm 1 allocate(pNew) 2 if (memory overflow) 1 return false 3 pNew -> data = dataIn 4 if (pPre = null) Adding before first node or to empty list 1 pNew -> link = list.head 2 list.head = pNew 5 else Adding in middle or at end 1 pNew -> link = pPre -> link 2 pPre -> link = pNew 6 list.count = list.count + 1 7 return true End insertNode Faculty of Computer Science and Engineering – HCMUT Slide 20 Delete Node Locate the node to be deleted. Point the node predecessor's link to its successor. Release the memory for the deleted node Faculty of Computer Science and Engineering – HCMUT Slide 21 Delete First Node Before 3 39 52 75 head count list pPre pLoc list.head = pLoc -> link recycle(pLoc) After recycled 2 count 52 75 head list pPre pLoc Faculty of Computer Science and Engineering – HCMUT Slide 22 General Delete Case Before 3 39 count 52 75 head list pPre pLoc pPre -> link = pLoc -> link recycle (pLoc) After 2 count 39 recycled pPre pLoc 75 head list Faculty of Computer Science and Engineering – HCMUT Slide 23 Delete Node Algorithm Algorithm deleteNode (ref list <metadata>, val pPre <node pointer>, val pLoc <node pointer> ref dataOut <dataType>) Delete data from a linked list and returns it to calling module Pre list is metadata structure to a valid list pPre is a pointer to predecessor node pLoc is a pointer to node to be deleted dataOut is variable to receive deleted data Post data have been deleted and returned to caller Faculty of Computer Science and Engineering – HCMUT Slide 24 Delete Node Algorithm 1 dataOut = pLoc -> data 2 if (pPre = null) Delete first node 1 list.head = pLoc -> link 3 else Delete other nodes 1 pPre -> link = pLoc -> link 4 list.count = list.count - 1 5 recycle (pLoc) 6 return End deleteNode Faculty of Computer Science and Engineering – HCMUT Slide 25 Search List Sequential search has to be used List may be ordered according to a key field Faculty of Computer Science and Engineering – HCMUT Slide 26 Successful Searches Located first target Located middle target Located last target 5 10 15 20 95 100 pPre pLoc pPre pLoc pPre pLoc Faculty of Computer Science and Engineering – HCMUT Slide 27 Unsuccessful Searches Less than first Greater than last target < 5 target > 15 target < 20 target > 100 5 10 15 20 95 100 pPre pLoc pPre pLoc pPre pLoc Faculty of Computer Science and Engineering – HCMUT Slide 28 Search List Algorithm Algorithm searchList (val list <metadata>, ref pPre <node pointer>, ref pLoc <node pointer> val target <keyType>) Searches list and passes back address of node containing target and its predecessor Pre list is metadata structure to a valid list pPre is a pointer to predecessor node pLoc is a pointer to current node target is the key being sought Post pLoc points to smallest node with equal or greater key - or - null if target > key of last node pPre points to largest node with smaller key - or - null if target < key of first node Return true if found, false if not found Faculty of Computer Science and Engineering – HCMUT Slide 29 Search List Algorithm 1 pPre = null 2 pLoc = list.head 3 loop (pLoc not null AND target > pLoc -> data.key) 1 pPre = pLoc 2 pLoc = pLoc -> link Set return value 4 if (pLoc = null) 1 found = false 5 else 1 if (target equal pLoc -> data.key) 1 found = true 2 else 1 found = false 6 return found End searchList Faculty of Computer Science and Engineering – HCMUT Slide 30 Retrieve Node Using search list to locate the node Retrieving data from the node Faculty of Computer Science and Engineering – HCMUT Slide 31 Retrieve Node Algorithm Algorithm retrieveNode (val list <metadata>, val key <keyType>, ref dataOut <dataType>) Retrieves data from a linked list Pre list is metadata structure to a valid list key is target of data to be retrieved dataOut is variable to receive retrieved data Post data placed at location specified in dataOut - or - error returned if not found Return true if successful, false if data not found Faculty of Computer Science and Engineering – HCMUT Slide 32 Retrieve Node Algorithm 1 found = searchList (list, pPre, pLoc, key) 2 if (found) 1 dataOut = pLoc -> data 3 return found End retrieveNode Faculty of Computer Science and Engineering – HCMUT Slide 33 Traverse List Traverse module controls the loop: calling a user-supplied algorithm to process data pWalker = list.head loop (pWalker not null) process (pWalker -> data) pWalker = pWalker -> link Faculty of Computer Science and Engineering – HCMUT Slide 34 Traverse List User controls the loop calling traverse algorithm to get the next element in the list success = getNext (list, 0, dataOut) loop (success) process (dataOut) success = getNext (list, 1, dataOut) Faculty of Computer Science and Engineering – HCMUT Slide 35 Traverse List list N count pos head 5 10 15 20 Faculty of Computer Science and Engineering – HCMUT ... 95 100 Slide 36 Traverse List Algorithm Algorithm getNext (ref list <metadata>, val fromWhere <Boolean>, ref dataOut <dataType>) Traverses a linked list. Each call returns the location of an element in the list. Pre list is metadata structure to a valid list fromWhere is 0 to start at the first element dataOut is variable to receive data Post dataOut contains data and true returned - or if end of list, return false Return false if end of list, true otherwise Faculty of Computer Science and Engineering – HCMUT Slide 37 Traverse List Algorithm 1 if (fromWhere is 0) Start from first 1 if (list.count is 0) 1 success = false 2 else 1 list.pos = list.head 2 dataOut = list.pos -> data 3 success = true 2 else Start from pos 1 if (list.pos -> link = null) End of list 1 success = false 2 else 1 list.pos = list.pos -> link 2 dataOut = list.pos -> data 3 success = true 3 return success End getNext Faculty of Computer Science and Engineering – HCMUT Slide 38 Destroy List Algorithm Algorithm destroyList (ref list <metadata>) Deletes all data in list. Pre list is metadata structure to a valid list Post all data deleted 1 loop (list.head not null) 1 dltPtr = list.head 2 list.head = head -> link 3 recycle (dltPtr) No data left in list. Reset metadata 2 list.count = 0 3 return End destroyList Faculty of Computer Science and Engineering – HCMUT Slide 39 Sparse Matrices students 1 2 8,000 A 1 courses 2 300 A C B B A C B A B C A Faculty of Computer Science and Engineering – HCMUT Slide 40 Sparse Matrices student array of linked list course array of linked lists 1 2 8,000 1 2 A A C B B A C B 300 A B C A Faculty of Computer Science and Engineering – HCMUT Slide 41 Sparse Matrices student array of linked list course array of linked lists 1 8,000 2 A 1 2 A B 300 C B student no. course no C grade row linkB colum link B A A C A Faculty of Computer Science and Engineering – HCMUT Slide 42 Sparse Matrices Why two arrays of linked lists? How about 3-D sparse matrices? Faculty of Computer Science and Engineering – HCMUT Slide 43 Complex Linked Lists Circularly-linked lists Doubly-linked lists Multi-linked lists Faculty of Computer Science and Engineering – HCMUT Slide 44 Circularly-Linked Lists list N 5 10 ... 95 count pos rear link To process data in round turns Faculty of Computer Science and Engineering – HCMUT Slide 45 Doubly-Linked Lists list N count pos 5 10 ... ... 95 ... rear head To process data sequences from both directions Faculty of Computer Science and Engineering – HCMUT Slide 46 Multi-Linked Lists list ... N count pos ... ... link1 link2 To process multiple data sequences each of which has a distinct key type Faculty of Computer Science and Engineering – HCMUT Slide 47 Reading Drozdek’s textbook – Operations on complex linked lists – Self-organizing lists – Case study: a library Faculty of Computer Science and Engineering – HCMUT Slide 48