int listDel(int delData) {
NodeType *ptr;
ptr = listHead;
while (ptr) {
if(ptr->data == delData) {
printf("data = %d key found\n", ptr->data);
if(ptr == listHead) {
printf("This is a head\n");
if(ptr->next) {
listHead = ptr->next;
(ptr->next)- >prev = 0;
}
else {
listHead = 0;
}
}
else if(ptr == listTail) {
printf("This is a tail\n");
listTail = ptr->prev;
(ptr->prev)-> next = 0;
}
else {
printf("This is a middle\n");
// connect previous to next
(ptr->prev)-> next = ptr->next;
// connect next to previous
ptr->next = (ptr->prev)->next;
}
// disconnect
ptr->prev = 0;
ptr->next = 0;
free(ptr); // delete
break;
}
ptr = ptr->next;
}
return 0;
}
int main() {
listAdd(1);
listAdd(2);
listAdd(3);
listAdd(4);
listAdd(5);
listAdd(6);
listShow();
printf("\n");
listDel(1);
listDel(6);
listDel(4);
listDel(7);
listDel(2);
printf("\n");
listShow();
return 0;
}
1st node added (1)
data = 1
data = 2
data = 3
data = 4
data = 5
data = 6
data = 1 key found
This is a head
data = 6 key found
This is a tail
data = 4 key found
This is a middle
data = 2 key found
This is a head
data = 3
data = 5
No comments:
Post a Comment