那个建设网站好,怀来县建设局网站,网站网站开发需要多少钱,推广网站的方法有搜索引擎营销、邮件营销双向链表#xff1a;节点数据 NEXT PREV
手撕代码(增加删除)
增加#xff0c;删除的操作#xff0c; 需要tmp 停止待操作节点的前一节点上。
查找操作进行了扩展#xff0c;回调函数(函数指针)。解耦合#xff0c;扩展功能。
相关的操作代码#xff1a;#include …双向链表节点数据 NEXT PREV
手撕代码(增加删除)
增加删除的操作 需要tmp 停止待操作节点的前一节点上。
查找操作进行了扩展回调函数(函数指针)。解耦合扩展功能。
相关的操作代码
#include DouLink.h
#include stdio.h
#include stdlib.h
#include string.hDouLinkList *CreateDouLinkList()
{DouLinkList *dl malloc(sizeof(DouLinkList));if (NULL dl){perror(CreateDouLinkList malloc);return NULL;}dl-head NULL;dl-clen 0;return dl;
}
int InsertHeadDouLinkList(DouLinkList *dl, DATATYPE *data)
{DouLinkNode *newnode malloc(sizeof(DouLinkNode));if (NULL newnode){perror(InsertHeadDouLinkList malloc);return 1;}memcpy(newnode-data, data, sizeof(DATATYPE));newnode-next NULL;newnode-prev NULL;if (IsEmptyDouLinkList(dl)){dl-head newnode;}else{newnode-next dl-head;dl-head-prev newnode;dl-head newnode;}dl-clen;return 0;
}int ShowDouLinkList(DouLinkList *dl, DIRECT direct)
{DouLinkNode *tmp dl-head;if (DIR_FORWARD direct){while (tmp){printf(name:%s sex:%c age:%d score:%d\n, tmp-data.name, tmp-data.age,tmp-data.age, tmp-data.score);tmp tmp-next;}}else //逆向显示{// tmp 会停在最后一个有效元素上while (tmp-next){tmp tmp-next;}while (tmp){printf(name:%s sex:%c age:%d score:%d\n, tmp-data.name, tmp-data.age,tmp-data.age, tmp-data.score);tmp tmp-prev;}}return 0;
}int IsEmptyDouLinkList(DouLinkList *dl)
{return 0 dl-clen;
}int InsertTailDouLinkList(DouLinkList *dl, DATATYPE *data)
{if (IsEmptyDouLinkList(dl)){return InsertHeadDouLinkList(dl, data);}else{DouLinkNode *newnode malloc(sizeof(DouLinkNode));if (NULL newnode){perror(InsertTailDouLinkList malloc);return 1;}memcpy(newnode-data, data, sizeof(DATATYPE));newnode-next NULL;newnode-prev NULL;DouLinkNode *tmp dl-head;while (tmp-next){tmp tmp-next;}newnode-prev tmp;tmp-next newnode;}dl-clen;return 0;
}int InsertPosDouLinkList(DouLinkList *dl, DATATYPE *data, int pos)
{int size GetSizeDouLinkList(dl);if (pos 0 || pos size){printf(InsertPosDouLinkList pos error\n);return 1;}if (0 pos){return InsertHeadDouLinkList(dl, data);}else if (size pos){return InsertTailDouLinkList(dl, data);}else{DouLinkNode *newnode malloc(sizeof(DouLinkNode));if (NULL newnode){perror(InsertPosDouLinkList malloc);return 1;}memcpy(newnode-data, data, sizeof(DATATYPE));newnode-next NULL;newnode-prev NULL;DouLinkNode *tmp dl-head;while (pos--){tmp tmp-next;}newnode-next tmp;newnode-prev tmp-prev;tmp-prev newnode;newnode-prev-next newnode;dl-clen;}return 0;
}//DouLinkNode *FindDouLinkList(DouLinkList *dl, char *name)
DouLinkNode *FindDouLinkList(DouLinkList *dl,PFUN fun, void*arg)
{DouLinkNode* tmp dl-head;while(tmp){//if(0strcmp(tmp-data.name,name))if(fun(tmp-data,arg)){return tmp;}tmptmp-next;}return NULL;
}
int ModifyDouLinkList(DouLinkList *dl, char *name, DATATYPE *newdata);
int DeleteDouLinkList(DouLinkList *dl, char *name);
int GetSizeDouLinkList(DouLinkList *dl)
{return dl-clen;
}int DestroyDouLinkList(DouLinkList *dl);双向链表的逆序三个指针分别代表前一个当前后一个
int ReverDouLinkList(DouLinkList *dl)
{if (NULL dl-head || NULL dl-head-next){printf(Rever error\n);return -1;}DouLinkNode *prev NULL;DouLinkNode *tmp dl-head;DouLinkNode *next NULL;while (tmp){next tmp-next;tmp-next prev;tmp-prev next;prev tmp;tmp next;}dl-head prev;return 0;
}