数据结构---配置网络步骤、单向链表额外应用
一、配置网络
valgrind:GNU提供的内存探测工具,用来监测内存错误和内存泄漏
内存泄漏:申请的空间使用完时,没有及时释放,造成内存泄漏。
配置网络步骤:
1、菜单栏虚拟机
点击菜单栏的“虚拟机”--->“ 设置 ”--->“ 网络适配器 ”--->“ 桥接模式 ”--->“ 确定 ”;
2、菜单栏编辑
点击菜单栏的“ 编辑 ”--->“ 虚拟网络编辑器 ”--->“ 更改设置 ”--->" VMnet0 "--->“ 桥接 ”--->桥接到PC正在上网的网卡上--->“ 应用 ”--->“ 确定 ”;
3、配置网络文件
在终端输入“ sudo vim /etc/network/interfaces ”,输入密码“ 1 ”,进入配置文件,将文件内容改为(白色内容为必须内容,完成后保存退出):
4、重启网络服务
在终端输入“ sudo /etc/init.d/network restart ”。
5、接入网络
在终端输入“ ping www.baidu.com ”。
6、安装内存泄漏检测器
在终端输入“ sudo apt-get install valgrind ”,可以查看有无内存泄漏。
二、单向链表的额外应用
1、快慢指针的应用
1)单向链表---查找中间结点函数的封装
//查找中间结点,并返回地址
Node_t *find_middle_link(Link_t *plink)
{if(is_empty_link(plink) == 0){return NULL;}Node_t *pfast = plink->phead;Node_t *pslow = pfast;while(pfast != NULL){pfast = pfast->pnext;if(pfast == NULL){break;}pfast = pfast->pnext;pslow = pslow->pnext;plink->clen++;}return pslow;
}
2)单向链表---查找倒数第k个值的函数的封装
//查找倒数第k个值
Node_t *find_last_k_node(Link_t *plink, int k)
{Node_t *pfast = plink->phead;Node_t *pslow = pfast;for(int i = 0; i < k; ++i){if(NULL == pfast){return NULL;}pfast = pfast->pnext;}while(pfast != NULL){pfast = pfast->pnext;pslow = pslow->pnext;}return pslow;
}
2、单向链表---倒置函数的封装
//实现链表的倒置
void reverse_link(Link_t *plink)
{Node_t *pinsert = NULL;Node_t *ptmp = plink->phead;plink->phead = NULL;while(ptmp != NULL){pinsert = ptmp;ptmp = ptmp->pnext;pinsert->pnext = plink->phead;plink->phead = pinsert;}
}
3、单向链表---排序函数的封装
//排序
void sort_link(Link_t *plink)
{if(is_empty_link(plink) == 0 || 1 == plink->clen){return ;}Node_t *ptmp = plink->phead->pnext;//第二个结点Node_t *pinsert = NULL;Node_t *p = NULL;plink->phead->pnext = NULL;while(ptmp != NULL){pinsert = ptmp;ptmp = ptmp->pnext;if(plink->phead->data >= pinsert->data){pinsert->pnext = plink->phead;plink->phead = pinsert;}else{p = plink->phead;while(p->pnext != NULL && pinsert->data > p->pnext->data){p = p->pnext; }pinsert->pnext = p->pnext;p->pnext = pinsert; }}
}
4、单向链表---链表是否成环
//成环
int cir_link(Link_t *plink)
{if(is_empty_link(plink) == 0){return 0;}Node_t *pfast = plink->phead;Node_t *pslow = plink->phead;while(pfast != NULL){pfast = pfast->pnext-> pnext;pslow = pslow -> pnext;if(pfast == pslow){break;}}if(pfast != NULL){return 1;}else{return -1;}
}
5、单向链表---约瑟夫环
//约瑟夫环,剩最后一个
Node_t *find_last_live_link(Link_t *plink)
{Node_t *ptmp = plink->phead;Node_t *pinsert = ptmp;Node_t *p = NULL;while(ptmp->pnext != ptmp){ptmp = ptmp->pnext -> pnext; pinsert = pinsert -> pnext;p = ptmp -> pnext;free(ptmp);ptmp = p;pinsert->pnext = p; pinsert = pinsert -> pnext;}return ptmp;
}
【END】