순환 링크 의 실현

1. 표준 순환 링크 와 일반 링크 에 커서 가 하나 더 있 고 커서 조작 함수 가 3 개 더 있 습 니 다.
2. 순환 링크 API 의 작성 방법 은 일반 링크 와 대체적으로 같 고 0 번 노드 의 증가 와 삭제 만 주의해 야 합 니 다. 그리고 한 번 노드 의 삽입 과 마지막 노드 의 삭 제 는 주의해 야 합 니 다. 삭제 할 때 커서 의 이동 에 주의해 야 합 니 다.
3. 순환 링크 는 일반 링크 에 비해 성능 이 크게 향상 되 지 않 고 링크 로 조세 프 문 제 를 해결 하 는 데 매우 편리 할 뿐이다. 
 
circle list. h 파일
#pragma once
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef void CircleList;

typedef struct _tag_CircleListNode
{
	struct _tag_CircleListNode* next;
}CircleListNode;

typedef struct _tag_CircleList
{
	int length;
	CircleListNode header;
	CircleListNode* slider;
}TCircleList;

CircleList* CircleList_Create();

int CircleList_Destroy(CircleList* list);

int CircleList_Length(CircleList* list);

void CircleList_Clear(CircleList* list);

CircleListNode* CircleList_Get(CircleList* list,int pos);

CircleListNode* CircleList_Insert(CircleList* list, CircleListNode* node,int pos);

int CircleList_Delete(CircleList* list,int pos);

CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);

CircleListNode* CircleList_Reset(CircleList* list);

CircleListNode* CircleList_Current(CircleList* list);

CircleListNode* CircleList_Next(CircleList* list);


circlemist. c 파일
#include "circlelist.h"


CircleList* CircleList_Create()
{
	TCircleList* list = NULL;
	list = (TCircleList*)malloc(sizeof(TCircleList));
	memset(list, 0, sizeof(TCircleList));

	return list;
}

int CircleList_Destroy(CircleList* list)
{
	if (list != NULL)
	{
		free(list);
		list = NULL;
	}
	return 0;
}

int CircleList_Length(CircleList* list)
{
	TCircleList* rList = NULL;
	if (list == NULL)
	{
		printf("Length:list is NULL
"); return -1; } rList = (TCircleList*)list; return rList->length; } void CircleList_Clear(CircleList* list) { TCircleList* rList = NULL; if (list == NULL) { printf("Clear:list is NULL
"); return ; } rList = (TCircleList*)list; rList->length = 0; rList->header.next = NULL; rList->slider = NULL; return ; } CircleListNode* CircleList_Get(CircleList* list,int pos) { TCircleList* rList = NULL; CircleListNode* current = NULL; int i; if (list == NULL || pos < 0) { printf("Get:list is NULL or pos<0
"); return NULL; } rList = (TCircleList*)list; current = &(rList->header); for (i = 0;i < pos;i++) { current = current->next; } return current->next; } CircleListNode* CircleList_Insert(CircleList* list, CircleListNode* node, int pos) { TCircleList* rList = NULL; CircleListNode* current = NULL; int i; if (list == NULL || node == NULL || pos < 0) { printf("Insert:list is NULL or pos<0
"); return NULL; } rList = (TCircleList*)list; current = &(rList->header); for (i = 0; (i < pos) && (current->next != NULL); i++) { current = current->next; } node->next = current->next; current->next = node; // if (rList->length == 0) { rList->slider = node; } rList->length++; // if (current == &(rList->header)) { // CircleListNode* last = CircleList_Get(list, rList->length - 1); last->next = current->next; } return node; } int CircleList_Delete(CircleList* list, int pos) { TCircleList* rList = NULL; CircleListNode* current = NULL; CircleListNode* last = NULL; CircleListNode* node = NULL; int i = 0; if (list == NULL || pos < 0) { printf("Delete:list is NULL or pos<0
"); return -1; } rList = (TCircleList*)list; current = &(rList->header); if (pos >= 0 && rList->length > 0) { for (i = 0; i < pos; i++) { current = current->next; } // if (current == &(rList->header)) { last = (CircleListNode*)CircleList_Get(rList, rList->length - 1); } // node = current->next; current->next = node->next; rList->length--; // if (last != NULL) { rList->header.next = node->next; last->next = node->next; } // if (rList->slider == node) { rList->slider = node->next; } // 0 if (rList->length == 0) { rList->header.next = NULL; rList->slider = NULL; } } return 0; } CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; int i = 0; if (sList != NULL) { CircleListNode* current = &(sList->header); // node for (i=0;ilength;i++) { if (current->next == node) { ret = current->next; break; } current = current->next; } // ret , i if (ret != NULL) { CircleList_Delete(list, i); } } return ret; } CircleListNode* CircleList_Reset(CircleList* list) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; if (sList!=NULL) { sList->slider = sList->header.next; ret = sList->slider; } return ret; } CircleListNode* CircleList_Current(CircleList* list) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; if (sList != NULL) { ret = sList->slider; } return ret; } // , CircleListNode* CircleList_Next(CircleList* list) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = NULL; if (sList != NULL && sList->slider != NULL) { ret = sList->slider; sList->slider = ret->next; } return ret; }

main. c 파일
#include "circlelist.h"

struct Value
{
	CircleListNode circlenode;
	int v;
};

int main()
{
	//       
	int i = 0;
	CircleList* list = CircleList_Create();

	struct Value v1, v2, v3, v4, v5, v6, v7, v8;
	v1.v = 1;
	v2.v = 2;
	v3.v = 3;
	v4.v = 4;
	v5.v = 5;
	v6.v = 6;
	v7.v = 7;
	v8.v = 8;

	CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list));
	CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list));

	//         
	for (i = 0; i < CircleList_Length(list); i++)
	{
		struct Value* pv = (struct Value*)CircleList_Get(list,i);
		printf("%d
", pv->v); } printf("
"); for (i=0;iv); } printf("
"); // CircleList_Reset(list); while (CircleList_Length(list)>0) { struct Value* pv= NULL; for (i=1;i<3;i++) { CircleList_Next(list); } pv = (struct Value*)CircleList_Current(list); printf("%d
", pv->v); CircleList_DeleteNode(list, (CircleListNode*)pv); } CircleList_Destroy(list); system("pause"); return 0; }

순환 링크 개념 에 대해 보 세 요:https://www.cnblogs.com/zhaobinyouth/p/9826312.html

좋은 웹페이지 즐겨찾기