Page 129 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 129
Fundamentals of Data Structures
Notes The implementation is shown as below:
void DLLDelete(struct DLLNode **head, int position) { stnict DLLNode
*temp, *temp2, temp = *head;
int k = 1;
if(*head == NULL) {
printf(“List is empty”);
return;
}
if(position 1){
*head = *head—>next;
if(*head ! = NULL)
*head—>prev = NULL;
free(temp);
return;
}
while((k<position-1) && temp—>next1=NULL) {
temp = temp—>next;
k++;
}
if( temp—>next == NULL) { //Deletion from end
temp2 = temp—>prev;
temp2—>next = NULL;
free(temp);
}
else {
temp2 = temp->Prev;
temp2—>next = temp—>next;
temp—>next—>prev = temp2;
free (temp);
}
return;
}
Self Assessment
Fill in the blanks:
6. Insertion into a doubly-linked list has three cases, that is, Inserting a new node before the
head, Inserting a new node after the tail (at the end of the list), and Inserting a new node at
the ......................... of the list.
7. When Inserting a Node in Doubly Linked List at the Ending, ......................... the list till the
end and insert the new node.
8. When Deleting an Intermediate Node in Doubly Linked List, ......................... and tail links
are not updated.
9. Once we found the node to be deleted in ......................... linked list, change the previous
nodes next pointer to the next node of the node to be deleted.
10. When Deleting the Last Node in Doubly Linked List, Update tail nodes previous nodes
next pointer with ..........................
122 LOVELY PROFESSIONAL UNIVERSITY