链表排序


第一种排序方法只交换数据域值,不交换各节点顺序 以数据域为整型数据且链表带有头结点为例

ElemSN *fun(ElemSN*h)
{
	int t;
	ElemSN*p, *q;
	for (p = h->next; p->next; p = p->next) 
	     for (q = p->next; q; q = q->next)
		 {
			 if (p->datadata)
			 { 
				 t = p->data;
				 p->data = q->data; 
				 q->data = t; 
			 } 
		 }
		 return h;
}


第二种排序方法不交换数据域值,直接交换节点顺序,即指针域值 

    每次循环取出值最大的节点,建链


void fun(ElemSN*h)
{
	ElemSN *p, *q, *mp, *mq, *head = NULL; 
	h = h->next;
	while (h)
	{ 
		for (p = mp = h; p; q = p, p = p->next) 
		{ 
			if (mp->data>p->data)
			{
				mp = p;
				mq = q;
			} 
		} 
		if (mp - h) 
			mq->next = mp->next; 
		else 
			h = h->next; 
		mp->next = head; 
		head = mp; 
	} 
	h->next = head; 
}


贴上来。。。不信我还能忘掉T^T

你可能感兴趣的:(c,数据,链表,指针)