循环链表的实现
说明
- 参考资料
- 线性表基本知识
- 该实现的说明
实现
- circular_list.h
#ifndef _CIRCULAR_LIST_H_
#define _CIRCULAR_LIST_H_
#ifdef __cplusplus
extern "C"{
#endif
typedef void CIRCULAR_LIST;
typedef struct CIRCULAR_LIST_NODE
{
struct CIRCULAR_LIST_NODE* next;
}CIRCULAR_LIST_NODE;
CIRCULAR_LIST* CircularList_Create();
void CircularList_Destroy(CIRCULAR_LIST* list);
void CircularList_Clear(CIRCULAR_LIST* list);
int CircularList_Length(CIRCULAR_LIST* list);
CIRCULAR_LIST_NODE* CircularList_Get(CIRCULAR_LIST* list, int pos);
int CircularList_Insert(CIRCULAR_LIST* list, CIRCULAR_LIST_NODE* node, int pos);
CIRCULAR_LIST_NODE* CircularList_Delete(CIRCULAR_LIST* list, int pos);
CIRCULAR_LIST_NODE* CircularList_DeleteNode(CIRCULAR_LIST* list, CIRCULAR_LIST_NODE* node);
CIRCULAR_LIST_NODE* CircularList_ResetSlider(CIRCULAR_LIST* list);
CIRCULAR_LIST_NODE* CircularList_Current(CIRCULAR_LIST* list);
CIRCULAR_LIST_NODE* CircularList_SliderNext(CIRCULAR_LIST* list);
#ifdef __cplusplus
}
#endif
#endif
- circular_list.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "circular_list.h"
typedef struct TAG_CIRCULAR_LIST
{
CIRCULAR_LIST_NODE header;
CIRCULAR_LIST_NODE* slider;
int length;
}TAG_CIRCULAR_LIST;
CIRCULAR_LIST* CircularList_Create()
{
TAG_CIRCULAR_LIST* circular_list = NULL;
circular_list = (TAG_CIRCULAR_LIST*)malloc(sizeof(TAG_CIRCULAR_LIST));
if(circular_list == NULL)
{
printf("func CircularList_Create() err: circular_list == NULL\n");
return NULL;
}
memset(circular_list, 0, sizeof(TAG_CIRCULAR_LIST));
return circular_list;
}
void CircularList_Destroy(CIRCULAR_LIST* list)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
if (list == NULL)
{
printf("func CircularList_Destroy() err: list == NULL\n");
return ;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
if (circular_list != NULL)
{
free(circular_list);
circular_list = NULL;
}
return;
}
void CircularList_Clear(CIRCULAR_LIST* list)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
if (list == NULL)
{
printf("func CircularList_Clear() err: list == NULL\n");
return;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
memset(circular_list, 0, sizeof(TAG_CIRCULAR_LIST));
return;
}
int CircularList_Length(CIRCULAR_LIST* list)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
if (list == NULL)
{
printf("func CircularList_Length() err: list == NULL\n");
return -1;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
return circular_list->length;
}
CIRCULAR_LIST_NODE* CircularList_Get(CIRCULAR_LIST* list, int pos)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
CIRCULAR_LIST_NODE* current_ptr = NULL;
int i = 0;
if (list == NULL || pos < 0)
{
printf("func CircularList_Get() err: list == NULL || pos < 0\n");
return NULL;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
if (pos >= circular_list->length)
{
printf("func CircularList_Get() err: pos >= circular_list->length\n");
return NULL;
}
current_ptr = (CIRCULAR_LIST_NODE*)circular_list;
for (i = 0; i < pos; ++i)
{
current_ptr = current_ptr->next;
}
return current_ptr->next;
}
int CircularList_Insert(CIRCULAR_LIST* list, CIRCULAR_LIST_NODE* node, int pos)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
CIRCULAR_LIST_NODE* current_ptr = NULL;
CIRCULAR_LIST_NODE* last_ptr = NULL;
int i = 0;
if (list == NULL || node == NULL || pos < 0)
{
printf("func CircularList_Insert() err: list == NULL || node == NULL || pos < 0\n");
return -1;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
if (pos >= circular_list->length)
{
pos = circular_list->length;
}
current_ptr = (CIRCULAR_LIST_NODE*)circular_list;
for (i = 0; i < pos; ++i)
{
current_ptr = current_ptr->next;
}
node->next = current_ptr->next;
current_ptr->next = node;
circular_list->length++;
if (circular_list->length == 1)
{
node->next = node;
}
if (circular_list->length != 1 && current_ptr == (CIRCULAR_LIST_NODE*)circular_list)
{
last_ptr = (CIRCULAR_LIST_NODE*)circular_list;
for(i = 0; i < circular_list->length; ++i)
{
last_ptr = last_ptr->next;
}
last_ptr->next = current_ptr->next;
}
return pos;
}
CIRCULAR_LIST_NODE* CircularList_Delete(CIRCULAR_LIST* list, int pos)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
CIRCULAR_LIST_NODE* deleted_node = NULL;
CIRCULAR_LIST_NODE* current_ptr = NULL;
CIRCULAR_LIST_NODE* last_ptr = NULL;
int i = 0;
if (list == NULL || pos < 0)
{
printf("func CircularList_Delete() err: list == NULL || pos < 0\n");
return NULL;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
if (pos >= circular_list->length)
{
printf("func CircularList_Delete() err: pos >= circular_list->length\n");
return NULL;
}
current_ptr = (CIRCULAR_LIST_NODE*)circular_list;
for (i = 0; i < pos; ++i)
{
current_ptr = current_ptr->next;
}
deleted_node = current_ptr->next;
current_ptr->next = deleted_node->next;
circular_list->length--;
if (circular_list->length == 0)
{
current_ptr->next = NULL;
}
if (circular_list->length > 0 && current_ptr == (CIRCULAR_LIST_NODE*)circular_list)
{
last_ptr = (CIRCULAR_LIST_NODE*)circular_list;
for (i = 0; i < circular_list->length; ++i)
{
last_ptr = last_ptr->next;
}
last_ptr->next = current_ptr->next;
}
return deleted_node;
}
CIRCULAR_LIST_NODE* CircularList_DeleteNode(CIRCULAR_LIST* list, CIRCULAR_LIST_NODE* node)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
int i = 0;
CIRCULAR_LIST_NODE* current_ptr = NULL;
CIRCULAR_LIST_NODE* deleted_node = NULL;
if (list == NULL || node == NULL)
{
printf("func CircularList_DeleteNode() err: list == NULL || node == NULL\n");
return NULL;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
current_ptr = (CIRCULAR_LIST_NODE*)circular_list;
for(i = 0; i < circular_list->length; ++i)
{
current_ptr = current_ptr->next;
if (current_ptr == node)
{
deleted_node = current_ptr;
break;
}
}
if (i >= circular_list->length)
{
printf("func CircularList_DeleteNode() err: node is not in circular list\n");
return NULL;
}
else if(i < circular_list->length)
{
CircularList_Delete(circular_list, i);
}
return deleted_node;
}
CIRCULAR_LIST_NODE* CircularList_ResetSlider(CIRCULAR_LIST* list)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
if (list == NULL)
{
printf("func CircularList_ResetSlider() err: list == NULL\n");
return NULL;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
circular_list->slider = circular_list->header.next;
return circular_list->slider;
}
CIRCULAR_LIST_NODE* CircularList_Current(CIRCULAR_LIST* list)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
if (list == NULL)
{
printf("func CircularList_Current() err: list == NULL\n");
return NULL;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
return circular_list->slider;
}
CIRCULAR_LIST_NODE* CircularList_SliderNext(CIRCULAR_LIST* list)
{
TAG_CIRCULAR_LIST* circular_list = NULL;
CIRCULAR_LIST_NODE* current_ptr = NULL;
if (list == NULL)
{
printf("func CircularList_SliderNext() err: list == NULL\n");
return NULL;
}
circular_list = (TAG_CIRCULAR_LIST*)list;
current_ptr = circular_list->slider;
if (circular_list->length == 0)
{
printf("func CircularList_SliderNext() err: circular_list->length == 0\n");
return NULL;
}
else
{
circular_list->slider = circular_list->slider->next;
}
return current_ptr;
}
- test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "circular_list.h"
typedef struct VALUE
{
CIRCULAR_LIST_NODE* circular_node;
int value;
}VALUE;
int main(void)
{
int i = 0;
int number_of_people = 16;
int death_number = 3;
VALUE* v = NULL;
CIRCULAR_LIST* circular_list = NULL;
circular_list = CircularList_Create();
if (circular_list == NULL)
{
printf("func main(): circular_list == NULL\n");
return -1;
}
srand((unsigned int)time(NULL));
for (i = 0; i < number_of_people; ++i)
{
v = (VALUE*)malloc(sizeof(VALUE));
v->circular_node = NULL;
v->value = rand()%100;
CircularList_Insert(circular_list, (CIRCULAR_LIST_NODE*)v, 0);
}
for (i = 0; i < CircularList_Length(circular_list); ++i)
{
v = (VALUE*)CircularList_Get(circular_list, i);
printf("%d ",v->value);
}
printf("\n");
CircularList_ResetSlider(circular_list);
while (CircularList_Length(circular_list) > 0)
{
for (i = 0; i < (death_number - 1); ++i)
{
CircularList_SliderNext(circular_list);
}
v = (VALUE*)CircularList_Current(circular_list);
CircularList_SliderNext(circular_list);
CircularList_DeleteNode(circular_list, (CIRCULAR_LIST_NODE*)v);
printf("%d\n",v->value);
if(v != NULL)
{
free(v);
v = NULL;
}
}
CircularList_Destroy(circular_list);
printf("Hello world!\n");
return 0;
}
|