em có đoạn code này sắp xếp các phần tử trong dslk đơn nhưng nó báo lỗi không chạy ? mong mn sửa cho em chỗ sai với
cấu trúc của em:
Mã:
struct node
{
	int key;
	node* next;
};
struct list
{
	node* phead;
	node* ptail;
	//int count=0;
};
đây là hàm sắp xếp của em
Tăng dần
Mã:
void sapxep(list *l)
{
	node *p;
	node *q;
	int data = 0;
	p = l->phead;
	for (p = l->phead; p->next!= NULL;p=p->next)
	{
		for (q = p->next; q->next!= NULL; q = q->next)
		{
			if (p->key > q->key)
			{
				data = p->key;
				p->key = q->key;
				q->key = data;
			}
			//q = q->next;
		}
		//p = p->next;
	}
	//return l;
}
giảm dần,
Mã:
void sortList(list *l)
{
	node *p=NULL, *x=NULL;
	list *l1, *l2;
	x = l->phead;
	if (l->phead = l->ptail) return;
	l1->phead = l1->ptail = NULL;
	l2->phead = l2->ptail = NULL;
	l->phead = x->next;
	while (l->phead!=NULL)// cat l tanh l1, l2
	{
		p = l->phead;
		l->phead = p->next;
		p->next = NULL;
		if (p->key >= x->key)
		{
			addhead(l1, p->key);
		}
		else
		{
			addhead(l2, p->key);
		}
	}
	sortList(l1);//de quy de sort l1
	sortList(l2);//de quy de sort l2
	if (l1->phead != NULL)
	{
		l1->phead = l->phead;
		l1->phead->next = x;
	}
	else
	{
		l->phead = x;
	}
		x->next = l2;
	if (l2->phead != NULL)
	{
		l->ptail = l2->ptail;
	}
	else
	{
		l->ptail = x;
	}
}
đoạn tăng dần thì ko lỗi nhung ko sắp xếp được khi em in ra, còn sort giảm dần thì lỗi ah