
循环链表可以是首尾相连,也可以是在链表的某一位置有一个loop。



循环链表1.png

下面给出一个创建循环链表的变量creatCircleList

Node * creatCircleList(float *data, int totalL, int circleL){
/*
Input PARA:
float *data: the input data for list
int totalL: the total number of all nodes
int circleL: the node number of circle
*/
if(totalL<circleL || totalL < 3 || circleL<2){
printf("The total length is less than 3 or the circle length is less than 2!!!");
return(NULL);
}
//
Node *head, *end;
Node *node1;
Node *nodestc;//the position for circle start
int ii,k;
head = (Node *)malloc(sizeof(Node));
head->flag = 1;
end = head;
for(ii=0;ii<totalL-circleL;ii++){
node1 = (Node *)malloc(sizeof(Node));
node1->value = data[ii];
end->next = node1;
end = node1;
}
// the first node of circle
node1 = (Node *)malloc(sizeof(Node));
node1->value = data[ii];
end->next = node1;
end = node1;
nodestc = end;
for(k=1;k<circleL;k++){
node1 = (Node *)malloc(sizeof(Node));
node1->value = data[ii+k];
end->next = node1;
end = node1;
}
end->next = nodestc;
// return
return(head);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
上述这个写法显得有些笨拙,也是我第一次写下来的,后来看了网上大神留下的c++代码,将过程简化:
创建一个单项链表,记住circle的第一个节点位置c 循环链表c 循环链表,最后将单向链表的尾结点“接到”该节点即可。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-124771-1.html
王健林说得还是比较现实
2222222222222222
不怕事