1 / 31

EEE 243B Applied Computer Programming

EEE 243B Applied Computer Programming. Linked list II - Searching, Inserting and Deleting §15.1 – 15.2. Review. Heap No it does not because the memory blocks are added at runtime, there is no way other than using a pointer to refer to the nodes in the linked list.

sybil
Download Presentation

EEE 243B Applied Computer Programming

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. EEE 243BApplied Computer Programming Linked list II - Searching, Inserting and Deleting §15.1 – 15.2

  2. Review Heap No it does not because the memory blocks are added at runtime, there is no way other than using a pointer to refer to the nodes in the linked list. It is the name of the list, the only reference we have for it (unless we declare other pointers and make them point to the list such as a tail pointer). • Where in memory are linked lists stored? • Does each node in a linked list have a symbolic name (variable name)? • What is the utility of the head pointer? Prof S.P. Leblanc

  3. Outline • Traversing/searching in linked lists • Inserting a node • At start of LL • Elsewhere in LL • Deleting a node • At Start of LL • Elsewhere in LL • Using indirection Prof S.P. Leblanc

  4. 1. Traversing/Searching through a LL • Recall from the Link List I lecture : typedefstructSTUDENT_NODE_TAG { char firstName[15]; char lastName[25]; unsigned long collegeNumber; float average; structSTUDENT_NODE_TAG* next; } STUDENT_NODE; //name of the type Prof S.P. Leblanc

  5. 1.Traversing/Searching through a LL • Declare a pointer to the type-defined structure in your declaration section (say pWalker) STUDENT_NODE* pWalker = NULL; • Set pWalkerto the head of the list pWalker = pLLHead; • Check if the pointer is NULL • Process the current node • Set the pointer to the next node pWalker = pWalker->next; • Go back to step 3 until done Prof S.P. Leblanc

  6. 1. Traversing/Searching through a LL intcount = 0; //1. pWalker could be declared before //2. Set pWalker to head of list STUDENT_NODE* pWalker= pLLHead; //3. Check for NULL while (pWalker != NULL) { //4. Process the current node count++; //5. Set pointer to next node pWalker = pWalker->next; } //6. Go back to step 3 … Prof S.P. Leblanc

  7. 1. Traversing/Searching through a LL Steps 1,2 – Set pointer to head of list count = 0 pWalker Allard 20001 Allen 22456 next next pLLHead Alpo 22460 Baar 20002 next next pWalker = pLLHead; Prof S.P. Leblanc

  8. 1. Traversing/Searching through a LL Steps 3 – 5 Process node and advance count = 1 pWalker Allard 20001 Allen 22456 next next pLLHead Alpo 22460 Baar 20002 next next pWalker = pWalker->next; Prof S.P. Leblanc

  9. 1. Traversing/Searching through a LL Steps 6 – Repeat until done pWalker count = 4 Allard 20001 Allen 22456 next next pLLHead Alpo 22460 Baar 20002 next next while (pWalker != NULL) {…} Prof S.P. Leblanc

  10. 2. Inserting a node in the LL • We already saw how to insert the first node in a LL that was empty in the previous lecture. • Before we insert any node in a LL we must first traverse it in search of our insertion point. • Of course, this is not required if we use an unordered list and always add nodes at the beginning of the list. • This is a simple general case that is easily handled by the more specific ordered list insertion steps that we will cover in 2a. Prof S.P. Leblanc

  11. 2a. Inserting a node at start of LL • Declare a temporary node (pTemp) and request memory through malloc • We verify if there is enough memory available • Initialize all the fields for the new node (to which pTemppoints)with the appropriate values • To insert • at the head of the list: • Point the new block to the first node by using the head pointer: pTemp->next = pLLHead; • Make the head point to the new node (using pTemp) pLLHead = pTemp; Prof S.P. Leblanc

  12. 2a. Inserting a node at start of LL //1. Create new node and check for available memory STUDENT_NODE* pTemp= NULL; pTemp = (STUDENT_NODE*)malloc(sizeof(STUDENT_NODE)); if (pTemp == Null) exit(1); //2. Initialize new node strcpy(pTemp->firstName,"Jack"); strcpy(pTemp->lastName,"Rabbit"); pTemp->collegeNumber = 22222; pTemp->average = 99.9; //3a. Insert node at head of list pTemp->next = pLLHead; pLLHead = pTemp; Prof S.P. Leblanc

  13. 2a. Inserting a node at start of LL Steps 1,2 - Declare and Initialize new node Rabbit 22222 pTemp ? next Allard 20001 next pLLHead Prof S.P. Leblanc

  14. 2a. Inserting a node at start of LL Steps 3a – Insert Node at start of LL Rabbit 22222 pTemp next 1 Allard 20001 2 pLLHead next pTemp->next = pLLHead; pLLHead = pTemp; Prof S.P. Leblanc

  15. 2b. Inserting a node elsewhere in LL • Create new node • Initialize new node • To insert • Elsewhere in LL: • Traverse the list using pWalker (here we assume that we want to insert after pWalker) • Make the new node point where pWalker points pTemp->next = pWalker->next; • Make pWalker point to the new node pWalker->next = pTemp; Prof S.P. Leblanc

  16. 2b. Inserting a node elsewhere in LL //1. Create new node STUDENT_NODE* pTemp= NULL; pTemp = (STUDENT_NODE*)malloc(sizeof(STUDENT_NODE)); if (pTemp== Null) exit(1); //2. Initialize new node strcpy(pTemp->firstName,"Jack"); strcpy(pTemp->lastName,"Rabbit"); pTemp->collegeNumber = 22222; pTemp->average = 99.9; //3bi Traversing the list to find insertion not shown //3biii. Insert node elsewhere in list pTemp->next = pWalker->next; pWalker->next = pTemp; Prof S.P. Leblanc

  17. 2b. Inserting a node elsewhere in LL Steps 1,2 - Declare and Initialize new node Rabbit 22222 pTemp ? next pWalker Allen 22456 Allard 20001 next next pLLHead Prof S.P. Leblanc

  18. 2b. Inserting a node elsewhere in LL Steps 3b – Insert Node elsewhere in LL Rabbit 22222 pTemp next 1 pWalker Allen 22456 Allard 20001 next next 2 pLLHead pTemp->next = pWalker->next; pWalker->next = pTemp; Prof S.P. Leblanc

  19. 3a. Deleting a node at start of LL • Declare a new pointer called pPred that will follow pWalker but one node behind • Check if the list is empty, if so print an error • See if first node must be deleted • Delete first node by setting the head pointer to the next node in the list. pLLHead = pLLHead->next; • Free the memory Prof S.P. Leblanc

  20. 3a. Deleting a node at start of LL unsigned long delNumber = 0; //get this value somehow //1. Declare pointers STUDENT_NODE* pWalker= pLLHead; STUDENT_NODE* pPred= pLLHead; //2. Check for empty list if (pLLHead == NULL){…} //print error and get out //3a. Delete first node if it matches delNumber if (pLLHead->collegeNumber == delNumber) { pLLHead = pLLHead->next; free(pWalker); //3b. Free memory } else pWalker = pWalker->next; …//pWalker is now one node ahead of pPred Prof S.P. Leblanc

  21. 3a. Deleting a node at start of LL Steps 1,2,3 – Find node to delete pPred delNumber = 20001 pWalker Allard 20001 Allen 22456 next next pLLHead Alpo 22460 Baar 20002 next next Prof S.P. Leblanc

  22. 3a. Deleting a node at start of LL Steps 3a-b – Delete node and free memory pPred delNumber = 20001 pWalker Allard 20001 Allen 22456 next next pLLHead Alpo 22460 Baar 20002 next next pLLHead = pLLHead->next; free(pWalker); Prof S.P. Leblanc

  23. 3b. Deleting a node elsewhere in LL • See if first node must be deleted • Find the node to delete by testing a condition match (collegeNumber) inside a loop • Advance both pointers (pWalker and pPred) • Delete the node by • setting the next node of pPred as follows: pPred->next = pWalker->next; • Free the memory where pWalker points Prof S.P. Leblanc

  24. 3b. Deleting a node elsewhere in LL …//Steps 1 – 3b of slide 20 //4. Find the node to delete while (pWalker->collegeNumber != delNumber) { pPred = pWalker; //4a Advance pointers pWalker = pWalker->next; if (pWalker == NULL) {…} //2 check for empty list } //5. Delete node and free memory pPred->next = pWalker->next; free(pWalker); … Prof S.P. Leblanc

  25. 3b. Deleting a node elsewhere in LL Steps 4 – Find Node to delete pPred delNumber = 20002 Allard 20001 Allen 22456 next next pLLHead Alpo 22460 Baar 20002 pWalker next next Prof S.P. Leblanc

  26. 3b. Deleting a node elsewhere in LL Steps 5 – Delete node and free memory pPred delNumber = 20002 pPred->next = pWalker->next; free(pWalker); Allard 20001 Allen 22456 pNextStdNode pNextStdNode pLLHead Alpo 22460 Baar 20002 pWalker pNextStdNode pNextStdNode Prof S.P. Leblanc

  27. 3c. Deleting a node elsewhere in LL using Indirection • You can also use a level of indirection to keep a hold of the predecessor to the node being deleted • pWalker->next->collegeNumber • The following code is equivalent to the one on slides 20 and 24 • We only keep one reference (pWalker) to walk through the list (pPred is not used) • When using this kind of indirection be careful what you free! Prof S.P. Leblanc

  28. 3c. Deleting a node elsewhere in LL using Indirection //1. Declare pointer STUDENT_NODE* pWalker = pHead; //2. Test for empty LL if (pWalker == NULL) return(pHead); //3a Test if head is the node to delete if (pHead->collegeNumber == delNumber) { pHead= pHead->next; free(pWalker); //3b free memory return pHead; } Prof S.P. Leblanc

  29. Deleting a node in the LL - general //4. Search the next node for the term to delete while (pWalker->next != NULL) { if pWalker->next->collegeNumber == delNumber) break; pWalker = pWalker->next; } if (pWalker->next != NULL) // delNumber not found return pHead; //5. Delete node and free memory pTemp = pWalker->next; pWalker->next = pWalker->next->next; free(pTemp); //This line is important Prof S.P. Leblanc

  30. Quiz Time • Describe the steps to traverse a LL • Describe the steps to insert elsewhere (not the start) of a LL Prof S.P. Leblanc

  31. Next lecture • Design Decision with Data Structures Prof S.P. Leblanc

More Related