#include <stdio.h> #include <malloc.h> typedef int elemtype; struct node { elemtype data; node * pnext; }; typedef struct { node * head; int length; }circular_list; /* FUNCTION circular_list_create DESCRIPTION initialize the circular list PARAMETERS void RETURNS p_cir_list: the pointer of circular list */ circular_list * circular_list_create() { circular_list * p_cir_list = (circular_list *)malloc(sizeof(circular_list)); p_cir_list->head = (node *)malloc(sizeof(node)); p_cir_list->head->data = -1; p_cir_list->head->pnext = p_cir_list->head; p_cir_list->length = 0; return p_cir_list; } /* FUNCTION circular_list_length DESCRIPTION get the circular list's length PARAMETERS p_cir_list: the pointer of circular list RETURNS p_cir_list->length: the length of circular list */ int circular_list_length(circular_list * p_cir_list) { return p_cir_list->length; } /* FUNCTION circular_list_get DESCRIPTION get the appoint position element's pointer PARAMETERS p_cir_list: the pointer of circular list position: appoint position RETURNS pnode: the pointer of appoint position element */ node * circular_list_get(circular_list * p_cir_list, int position) { if (position < 0 || position > circular_list_length(p_cir_list)) { return NULL; } else { node * pnode = p_cir_list->head; int i; for (i = 1; i <= position; ++i) { pnode = pnode->pnext; } return pnode; } } /* FUNCTION circular_list_insert DESCRIPTION insert the element to the appoint position in the circular list PARAMETERS p_cir_list: the pointer of circular list value: the element prepare to insert position: the appoint position RETURNS flag: the flag whether the insert operation successful or not */ int circular_list_insert(circular_list * p_cir_list, elemtype value, int position) { int flag; if (position < 0 || position > circular_list_length(p_cir_list) + 1) { flag = 0; } else { node * pnode = circular_list_get(p_cir_list, position - 1); node * pnew = (node *)malloc(sizeof(node)); pnew->data = value; pnew->pnext = NULL; pnew->pnext = pnode->pnext; pnode->pnext = pnew; ++p_cir_list->length; flag = 1; } return flag; } /* FUNCTION circular_list_delete DESCRIPTION delete the appoint position's element in the circular list PARAMETERS p_cir_list: the pointer of circular list position: the appoint position RETURNS flag: the flag whether the delete operation is successful */ int circular_list_delete(circular_list * p_cir_list, int position) { int flag; int length = circular_list_length(p_cir_list); if (position < 1 || position > length) { flag = 0; } else { node * pnode = circular_list_get(p_cir_list, position - 1); node * ptemp = pnode->pnext; pnode->pnext = pnode->pnext->pnext; free(ptemp); --p_cir_list->length; flag = 1; } return flag; } /* FUNCTION circular_list_reverse DESCRIPTION reverse all elements in the circular list PARAMETERS p_cir_list: the pointer of circular_list RETURNS void */ void circular_list_reverse(circular_list * p_cir_list) { node * pnode = p_cir_list->head->pnext; node * pend = p_cir_list->head; p_cir_list->head->pnext = p_cir_list->head; while (pnode != pend) { node * ptemp = pnode; pnode = pnode->pnext; ptemp->pnext = p_cir_list->head->pnext; p_cir_list->head->pnext = ptemp; } } /* FUNCTION circular_list_print DESCRIPTION print the all element in the circular list PARAMETERS p_cir_list: the pointer of circular list RETURNS void */ void circular_list_print(circular_list * p_cir_list) { node * pend = p_cir_list->head; node * pnode = p_cir_list->head->pnext; while (pnode != pend) { printf("%d ", pnode->data); pnode = pnode->pnext; } printf("/n"); } int main() { circular_list * p_cir_list = circular_list_create(); printf("%d/n", circular_list_length(p_cir_list)); node * pnode = circular_list_get(p_cir_list, 1); if (pnode != NULL) { printf("data = %d/n", pnode->data); } else { printf("NULL pointer!/n"); } for (int i = 1; i <= 10; i++) { circular_list_insert(p_cir_list, i, i); } circular_list_print(p_cir_list); printf("The length of circular lis is %d/n", circular_list_length(p_cir_list)); circular_list_delete(p_cir_list, 7); circular_list_print(p_cir_list); printf("The length of circular lis is %d/n", circular_list_length(p_cir_list)); circular_list_reverse(p_cir_list); circular_list_print(p_cir_list); printf("The length of circular lis is %d/n", circular_list_length(p_cir_list)); return 0; }