node

//实现在输入时自动排序(由小到大)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


typedef void(*backcall)(void *);
typedef struct node
{
	backcall fun;
	void* userdata;
	int data;
	struct node *next;
	
}PNode;
PNode head={0};
void list_printf(PNode *head)
{
	PNode *p=head->next;
	while(p!=NULL)
	{
		printf("data:%d\n",p->data);
		p=p->next;
	}
	printf("\n\n");
}
void list_delate(PNode *head)
{
	PNode *p=head->next;
	PNode *temp=head;
	if(p!=NULL)
	{
		if(p->next!=NULL)
			temp->next=p->next;
		free(p);
		p=NULL;
	}
}
PNode* get_node(PNode *head)
{
	if(head==NULL||head->next==NULL)
		return NULL;
	return head->next;
}
int  add_node(PNode *head,PNode *new)
{
	int data=new->data;
	PNode *p=head;
	PNode *temp=p;
	PNode *node=(PNode*)malloc(sizeof(PNode));
	if(node==NULL)
	{
		printf("malloc node fail\n");
		return -1;
	}else{
		printf("malloc success\n");
		node->next=NULL;
		node->data=data;
		node->fun=new->fun;
		node->userdata=new->userdata;
		
	}
	while(p!=NULL&&data>p->data)
	{
		printf("temp->data;%d\n",temp->data);
		temp=p;
		p=p->next;
	}
	if(p==NULL)
	{
		temp->next=node;
		node->next=NULL;
	}
	else
	{
		temp->next=node;
		node->next=p;
	}
	return 0;
} 
void callp(void *param)
{
	printf("%s\n",(char *)param);
}
void nodecall(void *param)
{
	PNode* node=(PNode*)param;
	printf("this is node test\n");
	printf("node data:%d\n",node->data);
	node->fun(node->userdata);
	list_delate(&head);
	list_printf(&head);
	node->data=12;
	add_node(&head,node);
	list_printf(&head);
}

void funtest()
{
	PNode* node= get_node(&head);
	node->fun(node->userdata);
}
int main()
{
	
	PNode test1={0};
	PNode test2={0};
	PNode test3={0};
	test1.data=10;
	test1.fun=callp;
	test1.userdata="1234test1";
	
	test2.data=30;
	test2.fun=callp;
	test2.userdata="5678test2";
	
	
	test3.data=20;
	test3.fun=nodecall;
	test3.userdata=&test2;
	add_node(&head,&test1);
	add_node(&head,&test2);
	add_node(&head,&test3);
	
	list_printf(&head);
	funtest();
	list_delate(&head);
	list_printf(&head);
	funtest();
	list_printf(&head);
		funtest();
	list_printf(&head);
	printf("this is test\n");
	return 0;
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值