#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
// 在循环链表指定位置插入新节点的函数
struct Node* insertAtPosition(struct Node *head, int data, int position) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (position == 1) {
struct Node *current = head;
while (current->next != head) {
current = current->next;
}
newNode->next = head;
current->next = newNode;
return newNode;
}
struct Node *current = head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
free(newNode);
return head;
}
newNode->next = current->next;
current->next = newNode;
return head;
}