Data structure 1: linked list (circular linked list)

1: Realization mechanism

The implementation idea of Linux linked list is to create only one next pointer in the node, and use the pointer to connect each node for printing and searching, and then perform type conversion.
The circular list is changed on the basis of the Linux list. The next of the last node points to the start node, while the next of the last node of the Linux list points to NULL. When the circular list is initialized, the head node points to itself (Clist - > head. Next = & (Clist - > head);)

Two: Code

1 CircleLinkList.h

/*
CircleLinkList.h
 The implementation mechanism is linux linked list (enterprise linked list) 
*/
#ifndef  _CIRCLELINKLIST_H_
#define _CIRCLELINKLIST_H_
#include <stdio.h>
#include <stdlib.h>

#define CIRCLELINKLIST_TRUE 0
#define CIRCLELINKLIST_FALSE -1

//Create a small node
typedef struct CIRCLELINKNODE{
	struct CIRCLELINKNODE *next;
}CircleLinkNode; 

//Linked list 
typedef struct CIRCLELINKLIST{
	CircleLinkNode head;//Head head node is a structure 
	int size;//Number of linked list data 
}CircleLinkList; 

//Value delete callback function 
typedef int (*COMPARE)(CircleLinkNode *data1,CircleLinkNode *data2); 

//Print callback function
typedef void (*PRINT)(CircleLinkNode *data);

//Initialization
CircleLinkList *_Init_List();
//insert
void _Insert_List(CircleLinkList *clist,int pos,CircleLinkNode *data);
//Delete by location
void _DeleteByPos_List(CircleLinkList *clist,int pos);
//Delete by value
void _DeleteByValue_List(CircleLinkList *clist,CircleLinkNode *data,COMPARE my_Compare);
//Return the first data
CircleLinkNode *_Front_List(CircleLinkList *clist);
//Get chain length
int _Size_List(CircleLinkList *clist);
//Printing
void _Print_List(CircleLinkList *clist,PRINT my_Print);
//lookup
int _Find_List(CircleLinkList *clist,CircleLinkNode *data,COMPARE my_Compare);
//Free memory
void _Free_List(CircleLinkList *clist);
#endif
 

2 CircleLinkList.c

/*
CircleLinkList.c
*/
#include "CircleLinkList.h"


//Initialization
CircleLinkList *_Init_List(){
	CircleLinkList *clist=(CircleLinkList *)malloc(sizeof(CircleLinkList));
	clist->head.next=&(clist->head);
	clist->size=0;
	return clist;
}
//insert
void _Insert_List(CircleLinkList *clist,int pos,CircleLinkNode *data){
	if(clist==NULL){
		return ;
	}
	if(pos<0||pos>clist->size){
		pos=clist->size;//Default to tail 
	}
	CircleLinkNode *pCurrent=&(clist->head);
	for(int i=0;i<pos;i++){//Find the previous node 
		pCurrent=pCurrent->next; 
		
	}
	//insert data
	data->next=pCurrent->next;
	pCurrent->next=data;
	clist->size++;
}

//Printing
void _Print_List(CircleLinkList *clist,PRINT my_Print){
	if(clist==NULL){
		return ;
	}
	CircleLinkNode *pCurrent=(clist->head.next);
	for(int i=0;i<clist->size;i++){
		if(pCurrent==&(clist->head)){//Skip pointer if printing multiple times 
			pCurrent=pCurrent->next; 
		}
		//Printing
		my_Print(pCurrent);
		pCurrent=pCurrent->next; 
	}
}
//Delete by location
void _DeleteByPos_List(CircleLinkList *clist,int pos){
	if(clist==NULL){
		return ;
	}
	if(pos<0||pos>=clist->size){
		return ;
	}
	CircleLinkNode *pCurrent=&(clist->head);
	for(int i=0;i<pos;i++){//Find the previous node 
		pCurrent=pCurrent->next; 
	}
	//Delete this node cache
	CircleLinkNode *DelNode= pCurrent->next;
	pCurrent->next=DelNode->next;
	//Release delete node space,
	/*
	if(DelNode!=NULL){
		free(DelNode);
		DelNode=NULL;
	} 
	*/
	
	clist->size--;
}
//Delete by value
void _DeleteByValue_List(CircleLinkList *clist,CircleLinkNode *data,COMPARE my_Compare){
	if(clist==NULL){
		return ;
	}
	if(data==NULL){
		return ;
	}
	CircleLinkNode *PreCurrent= (clist->head.next);//Previous data 
	CircleLinkNode *pCurrent=PreCurrent->next;//Corresponding data found 
	for(int i=0;i<clist->size;i++){
		if(my_Compare(pCurrent,data)==CIRCLELINKLIST_TRUE){//Matching success 
			PreCurrent->next=pCurrent->next;
			clist->size--;
			break;
		}
		//No match to
		PreCurrent->next= pCurrent;
		pCurrent->next=PreCurrent->next; 
	}
	
}
//Return the first data
CircleLinkNode *_Front_List(CircleLinkList *clist){
	return clist->head.next;
}
//Get chain length
int _Size_List(CircleLinkList *clist){
	return clist->size;
}
//lookup
int _Find_List(CircleLinkList *clist,CircleLinkNode *data,COMPARE my_Compare){
	if(clist==NULL){
		return -1;
	}
	if(data==NULL){
		return -1;
	}
	CircleLinkNode *pCurrent=(clist->head.next);
	int pos=-1;
	for(int i=0;i<clist->size;i++){
		if(my_Compare(pCurrent,data)==CIRCLELINKLIST_TRUE){//Eureka 
			pos=i;
			break;
		}
		pCurrent=pCurrent->next;
	}
	return pos;
}
//Free memory
void _Free_List(CircleLinkList *clist){
	if(clist!=NULL){
		free(clist);
		clist=NULL;
	}
}

3 main.c

/*
main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CircleLinkList.h"

typedef struct PERSON{
	CircleLinkNode node;
	char name[64];
	int age;
}Person;

//Print callback
void my_Print(CircleLinkNode *data){
	Person *p=(Person*)data;
	printf("name:%s  age:%d\n",p->name,p->age);
} 

//Comparison callbacks
int my_Compare(CircleLinkNode *data1,CircleLinkNode *data2){
	Person *p1=(Person*)data1;
	Person *p2=(Person*)data2;
	//judge
	if(strcmp(p1->name,p2->name)==CIRCLELINKLIST_TRUE&&p1->age==p2->age) {
		return CIRCLELINKLIST_TRUE;
	}
	else{
		return CIRCLELINKLIST_FALSE;
	}
} 
int main(void){	
	//Initialization
	CircleLinkList *clist=_Init_List();
	//Create data
	Person p1,p2,p3,p4;
	strcpy(p1.name,"AAA"); 
	strcpy(p2.name,"BBB"); 
	strcpy(p3.name,"CCC"); 
	strcpy(p4.name,"DDD"); 
	p1.age=20; 
	p2.age=30;
	p3.age=40;
	p4.age=50;
	
	//insert
	_Insert_List(clist,0,(CircleLinkNode*)&p1);
	_Insert_List(clist,1,(CircleLinkNode*)&p2);
	_Insert_List(clist,2,(CircleLinkNode*)&p3);
	_Insert_List(clist,3,(CircleLinkNode*)&p4);
	//Printing
	_Print_List(clist,my_Print);
	
	//lookup
	Person p6;
	strcpy(p6.name,"BBB"); 
	p6.age=60;
	int pos=_Find_List(clist,(CircleLinkNode*)&p6,my_Compare);//You can find it by changing p6 to p2 
	if(pos==-1){
		printf("p2 not found\n");
	}
	else{
		printf("p2 found pos=%d\n",pos);
	}
	//Delete by value
	printf("*********value delete***********\n");
	_DeleteByValue_List(clist,(CircleLinkNode*)&p2,my_Compare);
	_Print_List(clist,my_Print);
	
	//Delete by location
	printf("*********pos delete***********\n");
	_DeleteByPos_List(clist,1);
	_Print_List(clist,my_Print);
	printf("List length:%d\n",_Size_List(clist));
	
	//Free memory
	 _Free_List(clist);
	
	return 0;
}

3: Results show that

1. Compile environment

centos 3.10.0-862.el7.x86_64 
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

2. Compile command

gcc main.c CircleLinkList.c -std=c99
./a.out

3. Result display

84 original articles published, 23 praised, 10000 visitors+
Private letter follow

Tags: Linux P4 CentOS Red Hat

Posted on Wed, 12 Feb 2020 11:03:36 -0500 by bellaso