1 / 18

Chapter 3 – Part 2

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>)

ozzie
Download Presentation

Chapter 3 – Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linear Lists Chapter 3 – Part 2

  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

  3. 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 (pListcount not zero) 1 dltPtr = pListhead 2 pListhead = dltPtrlink 3 pListcount = pListcount – 1 4 release (dltPtr) No data left in list 2 release(pList) 3 retun null pointer Destroy List

  4. Linear List ApplicationsAppend Linked Lists Figure 3-19

  5. 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

  6. 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

  7. 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

  8. 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 pLocdata.key AND pLoclink not equal to startAddress) Figure 3-22

  9. 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

  10. 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

  11. Doubly Linked List -Insertion Figure 3-24

  12. 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

  13. 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 pNewuserData = dataIn 3 pNewback = pPre 4 pNewfore = pPrefore Test for insert into null list or at end of list 5 if (pPrefore null) –Inserting at end of list, set rear pointer 1 pListmetadata.rear = pNew 6 else – Inserting in middle of list, point successor to new 1 pSuccback = pNew 7 pPrefore = pNew 8 pListmetadata.count = pListmetadata.count +1 9 return 1 Dublicate data. Key already exist. 4 return 2 end insertDbl Doubly Linked List -Insertion

  14. Doubly Linked List -Deletion Figure 3-25

  15. 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

  16. 1 if (pDlt null) 1 abort 2 pListmetadata.count =pListmetadata.count – 1 3 pPred = pDltback 4 pSucc = pDltfore 5 pPredfore = pSucc 6 if (pSucc not null) 1 pSuccback = pPred 7 recycled pDlt 8 return 2 end deleteDbl Doubly Linked List -Deletion

  17. 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

  18. 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.

More Related