C语言循环链表
作者:互联网
C语言循环链表
循环链表插入节点
void InsertCircularLinkList(CircularLinkList * clList,int pos,ElementType element){
//创建一个空节点
CircularNode * node = (CircularNode *)malloc(sizeof(CircularNode));
node->data = element;
node->next = NULL;
if (pos == 1){ //如果插入的是第一个节点
node->next = clList->next;
if (!node->next){ //如果插入的链表长度为0
node->next = node;
}else { //如果长度不为0,就找到链表的最后一个节点并改变其指针域
CircularNode * lastNode = clList->next;
for (int i=1;i < clList->length;i++){
lastNode = lastNode->next;
}
lastNode->next = node;
}
clList->next = node;
clList->length++;
return;
}
//插入的不是第一个节点
CircularNode * currNode = clList->next;
for (int i=1;currNode && i<pos-1;i++){
currNode = currNode->next;
}
if (currNode){
node->next = currNode->next;
currNode->next = node;
clList->length++;
if (pos == clList->length){
node->next = clList->next;
}
}
}
标签:node,CircularNode,clList,currNode,next,链表,循环,C语言 来源: https://blog.csdn.net/m0_51060495/article/details/120463471