#include <stdio.h>
#include <stdlib.h>
#define m 8
#define n 3
//结点结构;
typedef struct CIRCLENODE
{
struct CIRCLENODE* next;
int data;
}CircleNode;
//链表结构;
typedef struct CIRCLELIST
{
CircleNode* head;
int size;
}CircleList;
CircleList* init_circleList()
{
CircleList* list = (CircleList*) malloc(sizeof(CircleList));
list->head = (CircleNode*)malloc(sizeof(CircleNode));
list->size = 0;
return list;
}
void insert_circleList(CircleList* list, int pos, int data)
{
if (pos == 0)
{
list->head->data = data;
list->head->next = list->head;
list->size++;
}
else
{
CircleNode* finalNode = list->head;
while (finalNode->next != list->head)
{
finalNode = finalNode->next;
}
//为新结点新开空间;
CircleNode* newNode = (CircleNode*)malloc(sizeof(CircleNode));
newNode->data = data;
newNode->next = list->head;
finalNode->next = newNode;
list->size++;
}
}
int front_circleList(CircleList* list)
{
return list->head->data;
}
void print_circleList(CircleList* list)
{
CircleNode* node = list->head;
for (int i = 0; i < list->size; ++i)
{
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int judge_same_num(int* arr,int a)
{
for (int i = 0; i<8; ++i)
{
if (arr[i] == a)
return 0;
}
return 1;
}
int main()
{
CircleList* list = init_circleList();
int arr[8];
int a = 3, b = 5, c = 10;
printf("--------测试循环链表指定位置输入数据--------\n");
for (int i = 1; i < 9; ++i)
{
insert_circleList(list, i-1, i);
}
print_circleList(list);
CircleNode* node = list->head;
int flag = 1;
for (int i = 0; i < 8; ++i)
{
if (flag == 1)
{
for (int j = 0; j < 2; ++j)
{
node = node->next;
}
flag = 0;
}
else
{
for (int j = 0; j < 3;)
{
node = node->next;
if (judge_same_num(arr, node->data)) //不等于才递加;
++j;
}
}
arr[i] = node->data;
printf("%d ", node->data);
}
printf("\n");
//for (int i = 0; i < 8; ++i)
//{
// printf("%d ", arr[i]);
//}
//printf("\n");
system("pause");
return 0;
}