timelist

#include
#include
 
typedef struct Node* NodePtr;
typedef void (*callback)(void *) ;
typedef struct Node
{
	int data;
	callback callfun;
	void * userdata;
	NodePtr prev;
	NodePtr next;
} Node;
 

 void addfun1(void *param)
 {
	 printf("%s:%d %s\n",__FUNCTION__,__LINE__,(char *)param);
 }
 void addfun2(void *param)
 {
	 printf("%s:%d %s\n",__FUNCTION__,__LINE__,(char *)param);
 }
 void addfun3(void *param)
 {
	 printf("%s:%d %s\n",__FUNCTION__,__LINE__,(char *)param);
 }
//插入并排序
void putBySort(NodePtr temnode, NodePtr head) {
 
	/*NodePtr node = (NodePtr)malloc(sizeof(Node));
	if (node == NULL)
	{
		printf("node==NUll");
		return;
	}
	int data=temnode->data;
	node->data = data;
	if(temnode->callfun!=NULL)
		node->callfun=temnode->callfun;
	if(temnode->userdata!=NULL)
		node->userdata=temnode->userdata;
	node->next = NULL;
	node->prev = NULL;*/
	int data=temnode->data;
	NodePtr node=temnode;
	NodePtr p = head;
	printf("input data:%d\n",data);
	if (data>p->data)
	{	
		printf("1.head data:%d\n",p->data);
		NodePtr pPrev = p;//保存P的前一个节点,在P=NULL的时候,P的前一个节点就代表是这个链表的最大值,如果data还大于目前最大值,则把data的节点设置为最大值
		while (p!=NULL&&data > p->data) 
		{
			printf("1,in while p->data:%d\n",p->data);
			pPrev = p;
			p = p->next;
		}
		
		if (p == NULL)
		{
			pPrev->next = node;
			node->prev = pPrev;
			printf("1.pPrev->data:%d\n",pPrev->data);
			printf("1,p==NULL,set tail input\n");
		}
		else 
		{
			printf("1,out while data:%d >p->data:%d\n",data,p->data);
			pPrev->next = node;
			node->prev = pPrev;
			node->next = p;
			p->prev = node;
			printf("1,p!=NULL,set aftet next input\n");
		}
		
	}
	else 
	{
		printf("2.head data:%d\n",p->data);
		NodePtr pNext = p;//保存P的后一个节点,在P=NULL的时候,P的后一个节点就代表是这个链表的最小值,如果data还小于目前最小值,则把data的节点设置为最大值
		while (p != NULL&&data < p->data) 
		{
			printf("2.in while p->data:%d\n",p->data);
			pNext = p;
			p = p->prev;
		}
		if (p == NULL)
		{
			pNext->prev = node;
			node->next = pNext;
			printf("2.p==NULL,set tail input\n");
		}
		else 
		{
			pNext->prev = node;
			node->next = pNext;
			node->prev = p;
			p->next = node;
			printf("2.p!=NULL,set aftet next input\n");
		}
	}
}
 
//冒泡排序
void sort(NodePtr head) {
 
	NodePtr p = head;
	NodePtr temp=NULL;
	NodePtr pNext = p->next;
 
	if (pNext == NULL)//只有一个参数
	{
		return;
	}
	if (pNext->next ==NULL && p->data > pNext->data)//只有两个参数
	{
		p->next = NULL;
		p->prev = pNext;
		pNext->next = p;
		pNext->prev = NULL;
		return;
	}
	NodePtr q;
	int tmp;
	for (; p->next!=NULL; p=p->next)
	{
		for (q=p->next; q!=NULL; q = q->next)
		{
			if (q->data < p->data)
			{
				tmp = q->data;
				q->data = p->data;
				p->data = tmp;				
			}
		}
	}
}
//查
NodePtr getNode(int position, NodePtr head) {
	NodePtr p = head;
	int i = 0;
	for (; p != NULL; p = p->next) {
		i++;
		if (i == position)
		{
			break;
		}
	}
	return p;
}
//删除节点
void deleteNode(int position, NodePtr head) {
	NodePtr p = getNode(position, head);
	NodePtr pNext = p->next;
	NodePtr pPrev = p->prev;
	pPrev->next = pNext;
	pNext->prev = pPrev;
	p->next = NULL;
	p->prev = NULL;
	p->data = 0;
}

//打印链表
void printNode(NodePtr list) {
	//找到最前面的节点
	NodePtr nodePrev = list;
	while (nodePrev->prev != NULL)
	{
		nodePrev = nodePrev->prev;
	}
	NodePtr node = nodePrev;
	while (node != NULL)
	{
		printf("%d ", node->data);
		//if(node->callfun!=NULL)
		//	node->callfun(node->userdata);
		node = node->next;
		if (node!=NULL)
		{
			printf(",");
		}
	}
	printf("\n");
}

NodePtr setlist(int data,callback fun,void *userdata)
{
	NodePtr node = (NodePtr)malloc(sizeof(Node));
	if (node == NULL)
	{
		printf("node==NUll");
		return NULL;
	}
	node->data = data;
	node->callfun=fun;
	node->userdata=userdata;
	node->next = NULL;
	node->prev = NULL;
	return node;
}
void main() {
 
	int i = 0;
	NodePtr list= (NodePtr)malloc(sizeof(Node));
	list->data = 0;
	list->next = NULL;
	list->prev = NULL;
 
	int intArr[] = {5,1,20,15,22,11,19,50,18,40,45,30,65};
	
	int length = sizeof(intArr) / sizeof(intArr[0]);
	callback test=addfun1;
	test("123456");
	//插入的时候排序
	//for (i = 0; i < length; i++)
	//{
	//	putBySort(intArr[i], list);
	//}
	NodePtr p1=setlist(10,addfun1,"addfun1");
	NodePtr p2=setlist(20,addfun2,"addfun2");
	NodePtr p3=setlist(15,addfun3,"addfun3");
	
	
	
	putBySort(p1,list);
	putBySort(p2,list);
	putBySort(p3,list);

	//首次插入数据后
	printf("首次插入数据后:\n");
	printNode(list);
	
	deleteNode(2,list);
	printNode(list);
}

你可能感兴趣的:(链表)