首页 > IT职业 > > 关于双链表删除任意数据!
今天归纳链表的时候,写了一个删除double linklist的任意数据,我觉得自己写的好臃肿,虽然g++过了,但是总觉得是不是哪里欠妥当。。。。。强迫症,总是感觉代码是不是不够精炼。。。。请大神给我提意见。。。自己有python和linux的一点点基础。。。C才学一个月,轻喷。。。一下是delete部分的代码!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | struct Node *delete_givendata(int data){ struct Node *current = head; struct Node *temp = NULL; struct Node *temp1 = NULL; if (head == NULL) return NULL;//empty! while (current -> data != data)//find the data we want to delete. { current = current -> next; } if (current == head && current -> next != NULL)//the first data is that data we want to delete { head = current -> next; current -> next -> prev = NULL; free(current); return head; } else if (current -> prev != NULL && current -> next == NULL)//the last data is that data we want to delete { current -> prev -> next = NULL; free(current); return head; } else if (head -> next == NULL)//just one data in list { head = NULL; free(current); return head; } //the data in (first+1,last-1) temp = current -> prev; temp1 = current -> next; temp -> next = temp1; temp1 -> prev = temp; free(current); return head;} |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1470/viewspace-3159315/ ,如需转载,请注明出处,否则将追究法律责任。