本文主要是介绍双向链表--,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
内存泄漏:当while(1)一直运行时,操作系统一直被申请空间,最终无空间可申请,造成内存泄漏。所以可以在堆区手动申请空间,在用完后手动释放
测试是否存在内存泄漏:
安装命令:sudo apt-get install valgrind执行命令:先编译,然后 valgrind ./a.out
双向链表的节点与链表对象
typedef struct stu
{int id;char name[32];int score;
}Datatype;typedef struct dnode
{Datatype data;struct dnode *ppre;struct dnode *pnext;}DLink_Node_t;typedef struct dlink
{DLink_Node_t *phead;int clen;}DLink_t;
创建一个链表
DLink_t * create_dlink()
{DLink_t *pdlink = malloc(sizeof(DLink_t));if(pdlink == NULL){perror("fail malloc");return NULL;}pdlink->phead = NULL;pdlink->clen = 0;return pdlink;}
判断链表是否为空
int is_empty_dlink(DLink_t *pdlink)
{return NULL == pdlink->phead;}
遍历
void printlist(DLink_t *pdlink,int dir)
{if(is_empty_dlink(pdlink)){return;}DLink_Node_t *p = pdlink->phead;if(dir){while(p != NULL){printf("%d\n%s\n%d\n",p->data.id,p->data.name,p->data.score);p=p->pnext;}}else{while(p->pnext != NULL){p = p->pnext;}while(p != NULL){printf("%d\n%s\n%d\n",p->data.id,p->data.name,p->data.score);p = p->ppre;}}printf("\n");
}
头插
//头插
int push_dlink_head(DLink_t *pdlink,Datatype data)
{DLink_Node_t *pnode = malloc(sizeof(DLink_Node_t));if(NULL == pnode){perror("fail malloc");return -1;}pnode->data = data;pnode->ppre = NULL;pnode->pnext = NULL;if(is_empty_dlink(pdlink)){pdlink->phead = pnode;}else{pnode->pnext = pdlink->phead;pdlink->phead->ppre = pnode;pdlink->phead = pnode;}pdlink->clen++;return 0;
}
尾插
//尾插
int push_dlink_back(DLink_t *pdlink,Datatype data)
{DLink_Node_t *p = pdlink->phead;DLink_Node_t *pnode = malloc(sizeof(DLink_Node_t));if(NULL == pnode){perror("fail malloc");return -1;}pnode->data = data;pnode->ppre = NULL;pnode->pnext = NULL;if(is_empty_dlink(pdlink)){pdlink->phead = pnode;}else{while(p->pnext != NULL){p=p->pnext; }p->pnext = pnode;pnode->ppre = p;pnode->pnext = NULL; }pdlink->clen++;return 0;
}
头删
//头删
void pop_front_dlink(DLink_t *pdlink)
{DLink_Node_t *p = pdlink->phead;if(!is_empty_dlink(pdlink)){if(p->pnext== NULL){pdlink->phead =NULL;free(p);}else{pdlink->phead = p->pnext;p->pnext->ppre=NULL;free(p);}pdlink->clen--;}}
尾删
//尾删
void pop_back_dlink(DLink_t *pdlink)
{DLink_Node_t *p = pdlink->phead;if(p->pnext == NULL){pop_front_dlink(pdlink);}else{while(p->pnext != NULL){p=p->pnext;}p->ppre->pnext=NULL;free(p);pdlink->clen--;}
}
查找
//查找
DLink_Node_t *select_dlink(DLink_t *pdlink,char *s)
{DLink_Node_t *p = pdlink->phead;while(p != NULL){if(strcmp(p->data.name,s)==0){return p;}p=p->pnext;}}
修改
//修改int change_dlink_data(DLink_t *pdlink,char *s,int m)
{DLink_Node_t *p = select_dlink(pdlink,s);p->data.score = m;return 0;
}
销毁
//销毁
void destory_dlink(DLink_t *pdlink)
{while(!is_empty_dlink(pdlink)){pop_front_dlink(pdlink);}free(pdlink);}
主函数调用
#include"head.h"
#include"doublelink.h"int main(int argc,const char *argv[])
{DLink_t *pdlink = create_dlink();if(pdlink == NULL){return -1;}Datatype stus[] = {{1, "zhangsan", 99},{2, "lisi", 85},{3, "wanger", 88},{4, "zhaowu", 65},{5, "lijiu", 78}};push_dlink_head(pdlink, stus[0]);push_dlink_head(pdlink, stus[1]);push_dlink_head(pdlink, stus[2]);push_dlink_back(pdlink,stus[3]);push_dlink_back(pdlink,stus[4]);printlist(pdlink,1);printlist(pdlink,0);pop_front_dlink(pdlink);pop_back_dlink(pdlink);// pop_back_dlink(pdlink);// DLink_Node_t *p = select_dlink(pdlink,"lisi");// printf("%d\n",p->data.score);// change_dlink_data(pdlink,"lisi",75);// printf("----------%d\n",pdlink->clen);// push_dlink_back(pdlink,stus[4]);// printlist(pdlink,1);// printlist(pdlink,0);destory_dlink(pdlink);return 0;
}
这篇关于双向链表--的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!