节点类型================================================================
typedef stuuct node
{
struct node *ppre;
datatype data;
struct node *pnext;
}linknode;
一.创建双向链表=============================================================
1.malloc创建空间
2.初始化
ptmpnode->ppre = NULL;
ptmpnode->pnext = NULL;
二.头插法插入===============================================================
1.malloc创建新空间
2.赋值
pnewnode->data = tmpdata;
pnewnode->pnext = phead->pnext;
pnewnode->ppre = head;
3.连接
phead->pnext = pnewnode;
if(pnewnode->pnext != NULL)
{
pnewnode->pnext->ppre = pnewnode;
}
三.删除链表节点=============================================================
ptmpnode = phead->pnext; //从第一个节点开始访问
if(ptmpnode->data == tmpdata) //满足条件
{
pnextnode = ptmpnode->pnext; //记录下一个节点 不然删除后不能找不到后面的
ptmpnode->ppre->pnext = ptmpnode->pnext; //将前面节点连接到后面节点
if(ptmpnode->pnext != NULL) //如果后面有节点
{
ptmpnode->pnext->ppre = ptmpnode->ppre; //将后面节点连接到前面节点
}
free(ptmpnode); //删除节点
ptmpnode = pnextnode; //将节点向后移动
cnt++; //记录删除的数量
}
else
{
ptmpnode = ptmpnode->pnext; //不满足上面条件 就往后走
}
四.遍历=====================================================
ptmpnode只要不是NULL 就一直往下走
五.反向遍历===========================================================
先让ptmpnode走到最末尾(条件:ptmpnode->pnext不是NULL)
只要ptmpnode不是phead 就一直往前走遍历
六.销毁========================================================
传入二级指针
ptmpnode = pfreenode = *pphead;
while(ptmpnode != NULL)
{
ptmpnode = ptmpnode->pnext; //先向后走,否走free后找不到后面的节点
free(pfreenode); //销毁节点
pfreenode = ptmpnode; //向后走,准备销毁下一个节点
}
*pphead = NULL; //防止野指针