//实现在输入时自动排序(由小到大)
#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;
}
node
最新推荐文章于 2023-03-06 16:40:58 发布