Download presentation
Presentation is loading. Please wait.
Published byDorothy Flexer Modified over 10 years ago
1
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists. Well create solutions in the next class, but you try them first! 1.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. 2.Write a function that will insert a node pointed to by newnode before the node pointed to by target. head target newnode
2
Page 22 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
3
Page 33 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; }
4
Page 44 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
5
Page 55 Basic Operations on a Doubly-Linked List 1.Add a node. 2.Delete a node. 3.Search for a node. 4.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.
6
Page 66 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)
7
Page 77 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: 39pNew pHead pCur 39pNew pHead pCur
8
Page 88 Adding a Node to the Middle of a Doubly-Linked List Before: After: 64 pNew pCur 55124 64 pNew pCur 55124
9
Page 99 Adding a Node to the Middle of a Doubly-Linked List The 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;
10
Page 1010 Adding a Node to the End of a Doubly-Linked List Before: After: 84 pNew pCur 5574 84 pNew pCur 5574
11
Page 1111 Adding a Node to the end of a Doubly-Linked List The Code pNew = (struct node *) malloc(sizeof(struct dllnode)); pNew -> data = 84; pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right = pNew;
12
Page 1212 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; }
13
Page 1313 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 predecessors and succesors link fields to point each other (see example). –Recycle the node using the free() function.
14
Page 1414 Deleting the First Node from a Doubly-Linked List Before:Code: pHead = pCur -> right; pCur ->right -> left = NULL; free(pCur); After: pHead 75124 pCur pHead Recycled 124 pCur
15
Page 1515 Deleting a Node from a Linked List – General Case Before: After: 7512446 pCur 75124 Recycled pCur 7723 77
16
Page 1616 Deleting a Node From a Doubly-Linked List The 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).
17
Page 1717 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;
18
Page 1818 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.