循环链表删除结点
CircleListNode* CircleList_Delete(CircleList* list,int pos)
{
TCircleList* sList = (TCircleList*)list;
int i=0;
if(sList !=NULL)&&(pos>=0)&&(sList ->length >0))
{
CircleListNode* current =(CircleListNode*)sList;
CircleListNode* last = NULL:
for(int i=0;i<pos;i++)
{
current =current ->next;
}
//删除头结点
if( current ==(CircleListNode*)sLIst)
{
last = (CircleListNode*)CircleList_Get(sList,sList->length - 1);
}
//求要删除的元素
ret = current ->next;
current->next =ret->next;
sList->length--;
//判断链表是否为空
if(last !=NULL)
{
sList->header.next = ret->next;
last->next = ret->next;
}
//若删除元素后,链表长度为0
if(sList->Length == 0)
{
sList->header.next = NULL;
sList->slider = NULL;
}
}
return ret;
}
CircleListNode* CircleList_DeleteNode(CircleList* list,CircleListNode* node)
{
TCircleList* sList = (TCircleList*)list;
CircleListNode* ret = NULL;
int i=0;
if(sList != NULL)
{
CircleListNode* current = (CircleListNode*)sList;
//查找node在循环链表的位置
for(int i=0;i<sList->lengh;i++)
{
if(current->next==node)
{
ret = current->next;
break;
}
current = current->next;
}
//找到ret,根据i去删除
if(ret!=NULL)
{
CircleList_Delete(sList,i);
}
}
return ret;
}
CircleListNode* CircleList_Next(CircleList* list)
{
TCircleList* slist = (TCircleList*)list;
CircleListNode* ret = NULL;
if(sList != NULL)&&(sList->slider !=NULL))
(
ret = sList ->slider;
sList->slider = ret->next;
}
return ret;
}
双向链表
单链表数据元素无法直接访问其前驱元素
在单链表的结点中增加一个指向其前驱的pre指针
双向链表拥有单链表的所有操作
创建链表
销毁链表
获取链表长度
清空链表
获取第pos个元素操作
插入元素到位置pos
删除位置pos处的元素
一般实现
current->next = node;
node->next = next;
next->pre = node;
node->pre = current;
删除操作
current->next = next;
next->pre = current;