C 언어 데이터 구조 알고리즘 - 동적 배열

4069 단어
잔말 말고 코드로 바로 올라가. 이건 C 언어야.
#define  _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

//1.                、
struct DynamicArray 
{
	//             
	void **addr;
	//         
	int capacity;  //  
	//                
	int size;//  
};

//        
//                 
struct DynamicArray * INit_DynamicArray(int capacity)
{
	//    
	if (capacity <= 0)
	{
		return NULL;
	}
	struct DynamicArray * arr = malloc(sizeof(struct DynamicArray));
	arr->capacity = capacity;
	arr->addr = malloc(sizeof(void *) *arr->capacity);
	arr->size = 0;  //      0
	return arr;
	
}
//    
void Insert_DynamicArray(struct DynamicArray * arr, int pos, void *data)  //                  
{
	if (NULL == arr || NULL == data)
	{
		return;
	}
	if (pos < 0 || pos > arr->size)
	{
		pos = arr->size;
	}
	//        
	if (arr->size >= arr->capacity)
	{
		//1.           
		int newcapacity = arr->capacity * 2;
		void **newspace = malloc(sizeof(void *)* newcapacity);
		//2.              
		memcpy(newspace, arr->addr, sizeof(void *)*arr->capacity);
		//3.       
		free(arr->addr);
		//4.Updata addr   
		arr->addr = newspace;
		arr->capacity = newcapacity;
		//     
	}
	//    , pos       
	for (int i = arr->size - 1; i >= pos;--i)
	{
		//   i        
		arr->addr[i + 1] = arr->addr[i];
	}
	//        pos  
	arr->addr[pos] = data;
	arr->size++;

}
//                 ,    
void Foreach_DynamicArray(struct DynamicArray * arr, void(*_callback)(void*))
{
	if (NULL == arr || NULL == _callback)
	{
		return;
	}
	for (int i = 0; i < arr->size;i++)
	{
		_callback(arr->addr[i]);
	}
}

//    
void Destroy_DynamicArray(struct DynamicArray * arr)
{
	if (arr==NULL)
	{
		return;
	}
	if (arr->addr!=NULL)
	{
		free(arr->addr);
		arr->addr = NULL;
	}
	free(arr);
	arr = NULL;
}

struct  Person
{
	char name[64];
	int age;
};

void myPrint(void *data)
{
	struct Person * person = (struct Person*)data;
	printf("Name:%s Age:%d
",person->name,person->age); } // void RemoveByPos_DynamicArray(struct DynamicArray * arr,int pos) { if (NULL == arr) { return; } if (pos<0 || pos > arr->size-1) { return; } // for (int i = pos; i < arr->size - 1;++i) { arr->addr[i] = arr->addr[i + 1]; } arr->size--; } // void RemoveByValue(struct DynamicArray * arr, void *data,int(*compare)(void *,void *)) { if (NULL == arr || NULL == data || NULL == compare) { return; } for (int i = 0; i < arr->size;++i) { if (compare(arr->addr[i],data)) { // RemoveByPos_DynamicArray(arr, i); break; } } } int MyCompare(void *d1, void *d2) { struct Person *p1 = (struct Person *)d1; struct Person *p2 = (struct Person *)d2; return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age; } void test() { // struct DynamicArray *da = INit_DynamicArray(10); // struct Person p1 = { "aaa", 10 }; struct Person p2 = { "bbb", 20 }; struct Person p3 = { "ccc", 30 }; struct Person p4 = { "ddd", 40 }; struct Person p5 = { "eee", 50 }; struct Person p6 = { "fff", 60 }; Insert_DynamicArray(da, 0, &p1); Insert_DynamicArray(da, 0, &p2); Insert_DynamicArray(da, 0, &p3); Insert_DynamicArray(da, 1, &p4); Insert_DynamicArray(da, 1, &p5); printf("capacity1:%d
", da->capacity); Insert_DynamicArray(da, 100, &p6);//3 5 4 2 1 6 printf("capacity2:%d
", da->capacity); Foreach_DynamicArray(da, myPrint); printf("----------
"); RemoveByPos_DynamicArray(da, 2);// 3 5 2 1 6 Foreach_DynamicArray(da, myPrint); printf("----------
"); struct Person pDal = { "aaa", 10 }; RemoveByValue(da, &pDal,MyCompare); Foreach_DynamicArray(da, myPrint); // Destroy_DynamicArray(da); } int main() { test(); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기