1 / 18

Solutions to Practice Problems Using a Linked List From Previous Day’s Notes

Solutions to Practice Problems Using a Linked List From Previous Day’s Notes. Create C functions to solve the following problems with linked lists. We’ll create solutions in the next class, but you try them first!

taipa
Download Presentation

Solutions to Practice Problems Using a Linked List From Previous Day’s Notes

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. Solutions to Practice Problems Using a Linked ListFrom Previous Day’s Notes • Create C functions to solve the following problems with linked lists. We’ll create solutions in the next class, but you try them first! • Write a recursive function that will search a list for a specific item, returning NULL if it is not found and a pointer to the node containing the value if it is found. • Write a function that will insert a node pointed to by newnode before the node pointed to by target. head target newnode Page 1

  2. Solution to Problem #1 struct node * lookup (int item, struct node *head) { if (head == NULL) return NULL; else if (item == head -> data) return head; else return(lookup(item, head -> next)); } ptr 23 Page 2

  3. Solution to Problem #2 void insert_before (struct node ** head, //pointer to a pointer struct node *target, struct node *newnode) { struct node *p, *pre; //search target pointer and keep track of the previous pointer p = *head; //must de-reference while (p != NULL && p != target) { pre = p; p = p -> next; } //do the insert newnode -> next = p; if (*head == p) *head = newnode; else pre -> next = newnode; } Page 3

  4. Doubly-Linked List Implementation Issues in C • A node in a doubly-linked list is a structure that has at least three fields. One of the fields is a data field; the two are pointers that contains the address of the logical predecessor node and logical successor node in the sequence. • An example doubly-linked list node with one data field: struct dllnode{ int data; struct dllnode *left; struct dllnode *right; }; left data right Page 4

  5. Basic Operations on a Doubly-Linked List • Add a node. • Delete a node. • Search for a node. • Traverse (walk) the list. Useful for counting operations or aggregate operations. • Note: the operations on a doubly-linked list are exactly the same that are required for a singly-linked list. As we discussed before, the reasons for using a doubly-linked list are application dependent for the most part. Page 5

  6. Adding Nodes to a Doubly-Linked List Adding a Node There are four steps to add a node to a doubly-linked list: • Allocate memory for the new node. • Determine the insertion point to be after (pCur). • Point the new node to its successor and predecessor. • Point the predecessor and successor to the new node. Current node pointer (pCur) can be in one of two states: • it can contain the address of a node (i.e. you are adding somewhere after the first node – in the middle or at the end) • it can be NULL (i.e. you are adding either to an empty list or at the beginning of the list) Page 6

  7. Adding Nodes to an Empty Doubly-Linked List Initial: Code: pNew = (struct node *) /*create node*/ malloc(sizeof(struct dllnode)); pNew -> data = 39; pNew -> right = pHead; pNew -> left = pHead; pHead = pNew; After: pNew 39 pHead pCur pNew 39 pHead pCur Page 7

  8. 55 124 pCur Adding a Node to the Middle of a Doubly-Linked List Before: After: pNew 64 64 pNew 55 124 pCur Page 8

  9. Adding a Node to the Middle of a Doubly-Linked ListThe Code pNew = (struct node *)malloc(sizeof(struct dllnode)); pNew -> data = 64; pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right -> left = pNew; pCur -> right = pNew; Page 9

  10. Adding a Node to the End of a Doubly-Linked List Before: After: pNew 84 55 74 pCur 84 pNew 55 74 pCur Page 10

  11. Adding a Node to the end of a Doubly-Linked ListThe Code pNew = (struct node *)malloc(sizeof(struct dllnode)); pNew -> data = 84; pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right = pNew; Page 11

  12. Inserting a Node Into a Doubly-Linked List //insert a node into a linked list struct node *pNew; pNew = (struct node *) malloc(sizeof(struct node)); pNew -> data = item; if (pCur == NULL){ //add before first logical node or to an empty list pNew -> left = pHead; pNew -> right = pHead; pHead = pNew; } else { if (pCur -> right == NULL) { //add at the end pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right = pNew; } else { //add in the middle pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right -> left = pNew; pCur -> right = pNew; } } } Page 12

  13. Deleting a Node from a Doubly-Linked List • Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap). • Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL. • To logically delete a node: • First locate the node itself (pCur). • Change the predecessor’s and succesor’s link fields to point each other (see example). • Recycle the node using the free() function. Page 13

  14. Deleting the First Node from a Doubly-Linked List Before: Code: pHead = pCur -> right; pCur ->right -> left = NULL; free(pCur); After: pHead 75 124 pCur pHead Recycled 124 pCur Page 14

  15. Deleting a Node from a Linked List – General Case Before: After: 23 77 75 46 124 pCur 23 Recycled 77 75 124 pCur Page 15

  16. Deleting a Node From a Doubly-Linked ListThe Code //delete a node from a linked list if (pCur -> left == NULL){ //deletion is on the first node of the list pHead = pCur -> right; pCur -> right -> left = NULL; { else { //deleting a node other than the first node of the list pCur -> left -> right = pCur -> right; pCur -> right -> left = pCur -> left; } free(pCur). Page 16

  17. Searching a Doubly-Linked List • Notice that both the insert and delete operations on a linked list must search the list for either the proper insertion point or to locate the node corresponding to the logical data value that is to be deleted. //search the nodes in a linked list pCur = pHead; //search until the target value is found or the end of the list is reached while (pCur != NULL && pCur -> data != target) { pCur = pCur -> right; } //determine if the target is found or ran off the end of the list if (pCur != NULL) found = 1; else found = 0; Page 17

  18. Traversing a Doubly-Linked List • List traversal requires that all of the data in the list be processed. Thus each node must be visited and the data value examined. • Notice that this is identical code to the singly-linked list except for the name of the pointer link. //traverse a linked list Struct dllnode *pWalker; pWalker = pHead; printf(“List contains:\n”); while (pWalker != NULL){ printf(“%d ”, pWalker -> data); pWalker = pWalker -> right; } Page 18

More Related