Realization of the cyclic chain list of data structure in C language

Source: Internet
Author: User
all source code address in Blog: GitHub managed Project Address Introduction to circular chain list

The cyclic list is another form of the linked list, it provides more flexibility to traverse the list elements of the ability, the circular chain can be one-way or reverse, the gall to distinguish a linked list is not a circular list only to see if it has tail elements can be. In a circular list, the next pointer to the last element points to the head pointer and is not set to null. In a two-way list, the Head element pointer prev The pointer to the last element, which allows each element in the loop list to change to the head element or tail element at will.

Definition header file: clist.h

#ifndef clist_h
#define CLIST_H 

#include <stdlib.h>

typedef struct CLISTELMT
{
	void *data;
	struct CLISTELMT *next
}clistelmt;

typedef struct CLIST
{
	int size;
	Int (*math) (const void *key1, const void *key2);
	void (*destory) (void *data);
	CLISTELMT *head;
} CList;

/* Public interface
/void Clist_init (CList *list, Void (*destory) (void *data))
; void Clist_destory (CList *list);
int Clist_ins_next (clist *list, clistelmt *element, const void *data);
int Clist_rem_next (clist *list, CLISTELMT *element, void **data);

#define CLIST_SIZE (list) (list)->size)
#define CLIST_HEAD (list) ((list)->head)
#define CLIST_DATA ( Element) ((Element)->data) #define CLIST_NEXT (Element) (element)
->next)

#endif

function Realization and analysis:

The 1.clist_list action is used to initialize a circular list so that you can perform other operations later, just like a non circular list.

2.clist_destory is used to destroy a circular list. In general, this operation means that all elements in the list will be removed.

3.clist_ins_next is used to insert a new element after the element specified by the parameter element in the loop list.

4.clist_rem_next removes the successor element of the element specified by the parameter element from the list.

5.clist_size, Clist_head,clist_data,clist_next is to implement some simple operations in the linked list.
Definition C File: clist.c

#include <stdlib.h> #include <string.h> #include "clist.h" void Clist_init (CList *list, Void (*destory) (void
	*data)) {list->size = 0;
	List->head = NULL;

	List->destory = Destory;
return;

	} void Clist_destory (CList *list) {void *data; while (Clist_size (list) > 0) {if (Clist_rem_next (list, List->head, (void**) &data) = = 0 && LIST-&G
		T;destory!= NULL) {list->destory (data);

	} meset (list, 0, sizeof (Clist));
return;
int Clist_ins_next (clist *list, clistelmt *element, const void *data);

	{CLISTELMT *new_element;

	if ((New_element = (CLISTELMT *) malloc (sizeof (CLISTELMT))) = = NULL) return-1;

	New_element->data = (void *) data;
		if (clist_size (list) = = 0) {new_element->next = new_element;
	List->head = new_element;
		else {New_element->next = element->next;
	Element->next = new_element;
	} list->size++;
return 0; int Clist_rem_next (clist *list, CLISTELMT *element, void **dATA);

	{CLISTELMT *old_element;

	if (clist_size (list) = = 0) return-1;
	*data = element->next->data;
		if (Element->next = = Element) {old_element = element->next;
	List->head = NULL;
		else {old_element = element->next;

		Element->next = element->next->next;
	if (old_element = = Clist_head (list)) List->head = old_element->next;

	Free (old_element);

	list->size--;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.