循環(huán)鏈表
單鏈表終端結(jié)點的指針指向頭節(jié)點
循環(huán)鏈表的創(chuàng)建
參考 https://blog.csdn.net/qq_29542611/article/details/79029540
typedef struct CLinkNode
{
Elemtype data;
struct CLinkNode *next;
}CLinkNode, *CLinkList;
void IniteCLinkList(CLinkList *L) {
int data = 0;
CLinkNode *target = NULL;
CLinkNode *head_node = NULL;
cout << "輸入數(shù)據(jù),0代表結(jié)束" << endl;
while (1) {
cin >> data;
if (data == 0) {
break;
}
if (*L == NULL) {
CLinkNode *head = (CLinkNode*)malloc(sizeof(CLinkNode));
*L = head; //鏈表指向頭結(jié)點
CLinkNode *node = (CLinkNode*)malloc(sizeof(CLinkNode));
node->data = data;
node->next = head;
head->next = node;
}
else
{
for (target = (*L)->next; target->next != *L; target = head_node = target->next) {
head_node = target->next;
CLinkNode* node = (CLinkNode*)malloc(sizeof(CLinkNode));
node->data = data;
node->next = head_node;
target->next = node;//將新結(jié)點插入尾部
}
}
}
}