#include <stdio.h>
#include <malloc.h>
//循环链表
typedef struct node {
int num;
node* next;
} NODE;
NODE* fool;//尾结点
int lenght = -1;//链表的长度
NODE* create_node(int num) {
NODE* n = (NODE*) malloc(sizeof(NODE));
n->num = num;
n->next = NULL;
lenght++;
return n;
}
void print(NODE* head, int index) {
NODE* p = head->next;
int j = 0;
while (p != NULL && j <= index) {
p = p->next;
j++;
}
j = 0;
while (p != NULL) {
printf("%d ", p->num);
p = p->next;
j++;
if (j >= lenght) {
return;
}
}
printf("\n");
}
int main() {
NODE* head = create_node(-1);//头结点
NODE* p = head;
for (int i = 0; i < 10; i++) {
p->next = create_node(i);
p = p->next;
fool = p;
}
fool->next = head->next;// 使链表的首位相连
print(head, 3);//指定从链表的第几个位置开始遍历
return 0;
}