2021-09-21

void funa(int x)  // 返回类型 + 形参列表
{
	printf("%d ", x);
}
void funb(int a)
{
	printf("%d ", a);
}
int main()
{
	void (*pfun)(int) = nullptr;
	pfun = funa;

	pfun = funb;
    return 0;
}
void* PrintInt(const void* p)
{
	const int* ip = (int*)p;
	printf("%d ", *ip);
	return (void*)(ip + 1);
}
void* PrintDouble(const void* p)
{
	const double* dp = (double*)p;
	printf("%f ", *dp);
	return (void*)(dp + 1);
}
void* PrintChar(const void* p)
{
	const char* cp = (char*)p;
	printf("%c ", *cp);
	return (void*)(cp + 1);
}
void* PrintString(const void* p)
{
	const char* sp = *(char**)p;
	printf("%s ", sp);
	return (void*)((char**)p + 1);
}
void Print_Ar(void* ar, int n, void* (*pfun)(const void*))
{
	for (int i = 0; i < n; ++i)
	{
		ar = pfun(ar);
	}
	printf("\n");
}
int main()
{
	int ar[] = { 12,23,34,45,56,67,78,89 };
	int nar = sizeof(ar) / sizeof(ar[0]);

	double dr[] = { 1.2,2.3,3.4,4.5,5.6,6.7,7.8,8.9,9.0,10.0 };
	int ndr = sizeof(dr) / sizeof(dr[0]);

	char cr[] = { 'a','k','a','s','h','i' };
	int ncr = sizeof(cr) / sizeof(cr[0]);

	Print_Ar(ar, nar,PrintInt);
	Print_Ar(dr, ndr,PrintDouble);
	Print_Ar(cr, ncr,PrintChar);
}
void* PrintInt(const void* p)
{
	const int* ip = (int*)p;
	printf("%d ", *ip);
	return (void*)(ip + 1);
}
void* PrintDouble(const void* p)
{
	const double* dp = (double*)p;
	printf("%f ", *dp);
	return (void*)(dp + 1);
}
void* PrintChar(const void* p)
{
	const char* cp = (char*)p;
	printf("%c ", *cp);
	return (void*)(cp + 1);
}
void* PrintString(const void* p)
{
	const char* sp = *(char**)p;
	printf("%s ", sp);
	return (void*)((char**)p + 1);
}
void Print_Ar(void* ar, int n, void* (*pfun)(const void*))
{
	for (int i = 0; i < n; ++i)
	{
		ar = pfun(ar);
	}
	printf("\n");
}
int main()
{
	const char* str[] = { "akashi","newdata","hello" };
	int n = sizeof(str) / sizeof(str[0]);

	Print_Ar(str, n, PrintString);

	return 0;
}

my_adlist.h

#ifndef MY_ADLIST_H
#define MY_ADLIST_H

#define AL_START_HEAD 0
#define AL_START_TAIL 1

typedef struct listNode
{
	struct listNode* prev;
	struct listNode* next;
	void* value;  // ElemType value;
}listNode;

typedef struct listIter
{
	listNode* next;
	int direction; // 0 // 1 
}listIter;

typedef struct list
{
	listNode* head;
	listNode* tail;
	void* (*dup)(void* ptr); //复制 
	void (*free)(void* ptr); //
	int (*match)(void* ptr, void* key);
	unsigned int len;
}list;

list* listCreate();
void listRelease(list* list);
list* listAddNodeHead(list* list, void* value);
list* listAddNodeTail(list* list, void* value);
list* listInsertNode(list* list, listNode* old_node, void* value, int after);
void listDelNode(list* list, listNode* node);

listIter* listGetIterator(list* list, int direction);
listNode* listNext(listIter* iter);


#endif

adlist.cpp

#include<stdlib.h>
#include"my_adlist.h"

list* listCreate()
{
	struct list* list = nullptr;
	list = (struct list*)malloc(sizeof(*list));
	if (list == nullptr)
	{
		return nullptr;
	}
	list->head = list->tail = nullptr;
	list->len = 0;
	list->dup = nullptr;
	list->free = nullptr;
	list->match = nullptr;
	return list;
}
//void listRelease(list* list);
list* listAddNodeHead(list* list, void* value)
{
	listNode* node = (listNode*)malloc(sizeof(*node));
	if (node == nullptr) return nullptr;
	node->value = value;
	if (list->len == 0)
	{
		list->head = list->tail = node;
		node->next = node->prev = nullptr;
	}
	else
	{
		node->prev = nullptr;
		node->next = list->head;
		list->head->prev = node;
		list->head = node;
	}
	list->len += 1;
	return list;
}
//list* listAddNodeTail(list* list, void* value);
//list* listInsertNode(list* list, listNode* old_node, void* value, int after);
//void listDelNode(list* list, listNode* node);

void listDelNode(list* list, listNode* node)
{
	if (node->prev != nullptr)
		node->prev->next = node->next;
	else
		list->head = node->next;
	if (node->next != nullptr)
		node->next->prev = node->prev;
	else
		list->tail = node->prev;

	if (list->free) list->free(node->value);
	free(node);
	list->len--;
}

listIter* listGetIterator(list* list, int direction)
{
	listIter* iter = (listIter*)malloc(sizeof(*iter));
	if (iter == nullptr) return nullptr;
	if (direction == AL_START_HEAD)
	{
		iter->next = list->head;
	}
	else
	{
		iter->next = list->tail;
	}
	iter->direction = direction;
	return iter;
}
listNode* listNext(listIter* iter)
{
	listNode* current = iter->next;
	if (current != nullptr)
	{
		if (iter->direction == AL_START_HEAD)
		{
			iter->next = current->next;
		} 
		else
		{
			iter->next = current->prev;
		}
	}
	return current;
}

RadisList.cpp

#include<stdio.h>
#include"my_adlist.h"

void PrintInt(void* p)
{
	int x = *(int*)p;
	printf("%d ", x);
}
void PrintList(list* plist, void (*pfun)(void*))
{
	listIter* iter = listGetIterator(plist,AL_START_TAIL);
	listNode* p = nullptr;
	while ((p = listNext(iter)) != nullptr)
	{
		pfun(p->value);
	}

	printf("\n");
}

void* mydup(void* ptr)
{
	int x = (int)ptr;
	return new int(x);
}

void  myfree(void* ptr)
{
	delete ptr;
}
int main()
{
	list* plist = listCreate();

	plist->dup = mydup;
	plist->free = myfree;

	for (int i = 0; i < 10; ++i)
	{
		plist = listAddNodeHead(plist, plist->dup((void*)(i + 10)));
	}

	PrintList(plist,PrintInt);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值