180 likes | 257 Views
Linear Lists. Chapter 3 – Part 2. Deletes any nodes still in the list, releases their memory Releases the head node’s memory. Returns null pointer indicating that the list no longer exist. Destroy List. algorithm destroyList (ref pList <head pointer>)
E N D
Linear Lists Chapter 3 – Part 2
Deletes any nodes still in the list, releases their memory Releases the head node’s memory. Returns null pointer indicating that the list no longer exist. Destroy List
algorithm destroyList (ref pList <head pointer>) Deletes all data in list and then deletes head structure. PRE pList is a pointer to a valid list head structure POST All data and head structure deleted RETURN null head pointer 1 loop (pListcount not zero) 1 dltPtr = pListhead 2 pListhead = dltPtrlink 3 pListcount = pListcount – 1 4 release (dltPtr) No data left in list 2 release(pList) 3 retun null pointer Destroy List
Linear List ApplicationsAppend Linked Lists Figure 3-19
Linear List ApplicationsArray of Linked Lists Each linked list represents one row in the array. The nodes in the linked list represents the columns. Figure 3-20
Linked list structural variations known as a header node. We have three useful linked list variations: Circularly linked list Doubly linked list Multilinked list Complex Linked List Structure
Complex Linked List StructureHeader Nodes A header node structure; • Contains the meta data • Shares a pointer structure with data nodes A list pointer points the head node. A null list is defined as a list with only a header node. Figure 3-21
Complex Linked List StructureCircularly Linked List • The link in the last node points to the firs node. It could also point to the header node. • Insertion and deletion are same with singly linked list except that the last node points to the first node. • Search problem: we save the starting node’s address and stop when we have circled around. loop (target not equal to pLocdata.key AND pLoclink not equal to startAddress) Figure 3-22
Complex Linked List StructureDoubly Linked List • It is a linked list structure in which each node has a pointer to both its successor and its predecessor. • A backward pointer to its predecessor. • A forward pointer to its successor. • Another variation on the doubly linked list is the “doubly linked circularly linked list”. Figure 3-23
node shared structure back <pointer> fore <pointer> variant structure metadata count <integer> pos <pointer> rear <pointer> user data key <key type> ... end node Complex Linked List StructureDoubly Linked List – Head node Structure
Doubly Linked List -Insertion Figure 3-24
algorithm insertDbl (val pList <node pointer>, val dataIn <dataType>) This algorithm inserts data into a doubly linked list. PRE pList is a pointer to a valid doubly linked list. dataIn contains the data to be inserted. POST The data have been inserted in sequence RETURN <integer> 0: failed– dynamic memory overflow 1: successful 2: failed- dublicate key presented Doubly Linked List -Insertion
1 if (full list) 1 return (0) Locate insertion position in list 2 found = searchList(pList, pPre, pSucc, dataIn.key) 3 if (found false) 1 allocate (pNew) 2 pNewuserData = dataIn 3 pNewback = pPre 4 pNewfore = pPrefore Test for insert into null list or at end of list 5 if (pPrefore null) –Inserting at end of list, set rear pointer 1 pListmetadata.rear = pNew 6 else – Inserting in middle of list, point successor to new 1 pSuccback = pNew 7 pPrefore = pNew 8 pListmetadata.count = pListmetadata.count +1 9 return 1 Dublicate data. Key already exist. 4 return 2 end insertDbl Doubly Linked List -Insertion
Doubly Linked List -Deletion Figure 3-25
algorithm deleteDbl ( val pList <node pointer>, val pDlt <node pointer>) This algorithm deletes a node from a doubly linked list. PRE pList is a pointer to a valid doubly linked list. pDlt is a pointer to the node to be deleted. POST node deleted Doubly Linked List -Deletion
1 if (pDlt null) 1 abort 2 pListmetadata.count =pListmetadata.count – 1 3 pPred = pDltback 4 pSucc = pDltfore 5 pPredfore = pSucc 6 if (pSucc not null) 1 pSuccback = pPred 7 recycled pDlt 8 return 2 end deleteDbl Doubly Linked List -Deletion
Complex Linked List StructureMultilinked List • It is a list with two or more logical key sequences. • Data can be processed in multible sequence. • The data are not replicated. Figure 3-26
Create a doubly linked list structure to process the student information. The student number should be the key info. Follow the all remarks and instructions in your text book pages from 91 to 97 and build the your C codes to satisfy the defined requirements under a menu control such as: Create link list Destroy linked list Add node Delete node Search node Display list (traverse list) HW-4 Load your HW-4 to FTP site until 05 Apr. 07 at 17:00.