今天归纳链表的时候,写了一个删除double linklist的任意数据,我觉得自己写的好臃肿,虽然g++过了,但是总觉得是不是哪里欠妥当。。。。。强迫症,总是感觉代码是不是不够精炼。。。。请大神给我提意见。。。自己有python和linux的一点点基础。。。C才学一个月,轻喷。。。一下是delete部分的代码!
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; }
onemoo
为梦想努力_冬