Data Structures: A Pseudocode Approach with C

Similar presentations

1 Data Structures: A Pseudocode Approach with CChapter 5ListObjectivesExplain the design, use, and operation of a linear listImplement a linear list using a linked list structureUnderstand the operation of the linear list ADTWrite application programs using the linear list ADTDesign and implement different link-list structuresData Structures: A Pseudocode Approach with C

2 Data Structures: A Pseudocode Approach with C5-1 Basic OperationsA general linear list is a list in which update operations can be done anywhere in the list. Insertions and deletions are more efficient than arrays. Makes use of dynamically allocated storage allocation at runtime. Basic list operations:InsertionDeletionRetrievalTraversalData Structures: A Pseudocode Approach with C

3 Data Structures: A Pseudocode Approach with CInsertionOrdered lists are maintained in sequence according to the data or a key that identifies the data. Example: ssn#In Random list or chronological lists, there is no sequential relationship between two elements. Generally found in data-gathering applications. Data Insertion is at the end.Data Structures: A Pseudocode Approach with C

4 Data Structures: A Pseudocode Approach with CDeletionDeletion requires the list be searched to locate data being deleted.The predecessor of the element to be deleted has its link member assigned the address of the successor to the deleted element.Data Structures: A Pseudocode Approach with C

5 Data Structures: A Pseudocode Approach with CRetrievalRequires data to be searched and located in a list and presented to the calling module without the change in contents of the list.TraversalList traversal processes each element in a list in sequence. Looping algorithm used and each execution of the loop processes one element in the list.Data Structures: A Pseudocode Approach with C

7 Data Structures: A Pseudocode Approach with CHead NodeHead node structure stores the head pointer and other data about the list. Data are known as Metadata.Data NodeData Node includes data fields. One of the data fields can be a key field when application requires searching by key.Data Structures: A Pseudocode Approach with C

9 Data Structures: A Pseudocode Approach with CInsert into Empty ListWhen the head pointer of the list is null, the list is empty.To add a new node, the list header pointer should be assigned the address of the allocated node and make sure that it’s link field is a null pointer.Data Structures: A Pseudocode Approach with C

10 Data Structures: A Pseudocode Approach with CInsert at BeginningA new node is added before the first node of the list.Determine that addition is at the beginning of the list. If the predecessor pointer is a null pointer, there is no predecessor, so we are at the beginning of the list.Point the new node to the first node of the list and then set the head pointer to point to the new first node.Logically inserting into an empty list is same as inserting a the beginning.Data Structures: A Pseudocode Approach with C

11 Data Structures: A Pseudocode Approach with CInsert in MiddleWhen we add a node anywhere in the middle of the list, the predecessor pointer contains an address.Point the new node to it’s successor and then point it’s predecessor to the new node. The address of the new node’s successor can be found in the predecessor’s link field.Data Structures: A Pseudocode Approach with C

12 Data Structures: A Pseudocode Approach with CInsert at EndWhen adding at the end of the list, we only need to point the predecessor to the new node. There is no successorThe new node’s link field should be set to null pointer. Also, the last node in the list has a null pointer. If we use this pointer rather than a null pointer constant, the code becomes same as the inserting in the middle.Data Structures: A Pseudocode Approach with C

13 Data Structures: A Pseudocode Approach with CInsert Node AlgorithmWe are given a pointer to the list, the predecessor, and the data to be inserted.We allocate memory for the new node, set data, and adjust the link pointers.If true, insert successful.Data Structures: A Pseudocode Approach with C

14 Data Structures: A Pseudocode Approach with CDelete Node AlgorithmRemoves a node from the list by changing pointers and then physically deleting the node from dynamic memory.Locate the node to be deleted by knowing it’s address and it’s predecessor’s address.Change the predecessor’s link field to point to the deleted node’s successor. We then recycle the node back to dynamic memory.If deleting the only node in the list, results in empty list and set the head to a null pointer.Data Structures: A Pseudocode Approach with C

15 Data Structures: A Pseudocode Approach with CDelete First NodeWhen deleting first node, the head pointer must be reset to point to the node’s successor and then recycle the memory for the deleted node.Set list head to pLoc linkRecycle (pLoc)Data Structures: A Pseudocode Approach with C

16 Data Structures: A Pseudocode Approach with CGeneral Delete CaseDeleting in the middle or at the end is a general case.Simply point the predecessor node to the successor of the node being deleted.When the node being deleted is the last node of the list, it’s null pointer is moved to the predecessor’s link field, making the predecessor the new logical end of the list.Set pPre link to pLoc linkRecycle (pLoc)Data Structures: A Pseudocode Approach with C

17 Data Structures: A Pseudocode Approach with CDelete Node AlgorithmWe are given a pointer to the list, pointer to the node to be deleted, and pointer to the delete node’s predecessor.We may copy the deleted node’s data to a data out area in the calling program.Adjust the pointers before releasing the node’s memory.Data Structures: A Pseudocode Approach with C