对单链表排序

#include
#include
typedef struct LNode {
	int data;
	struct LNode* next;
}LNode,*LinkList;
LinkList List_HeadInsert(LinkList &L)
{
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	int n;
	scanf_s("%d", &n);
	for(int i=0;i<n;i++){
		LNode* p= (LNode*)malloc(sizeof(LNode));
		int e;
		scanf_s("%d", &e);
		p->data = e;		
		p->next = L->next;
		L->next = p;
	}
	return L;

}
void Delete_x(LinkList& L, LNode* p, int x) {
	if (L->data == x) {
		LNode* q = L;
		L = q->next;
		p = L->next;
		free(q);
	}else if (p->data==x&&p->next) {
		p = p->next;
		Delete_x(L, p->next, x);
	}
	
}
void Delete_x1(LinkList& L, int x) {
	LNode* p=L, * q = L->next;
	while (q) {
		if (q->data == x) {
			p->next = q->next;
			free(q);
			q = p->next;
		}
		else {
			p = q;
			q = q->next;
			

		}
	}
}
LNode* GetElem(LinkList& L,int i) {
	int j = 1;
	LNode* p = L->next;
	if (i == 0)
		return L;
	while (j< i && p) {
		p = p->next;
		j++;
	}
	return p;
}
LNode* LocateElem(LinkList& L,int e) {
	LinkList p = L->next;
	int i=0;
	while (p->data != e && p) {
		p = p->next;
		i++;
	}
	return p;
}
void DeleteElem(LinkList &L,int i) {
	LinkList q=GetElem(L, i-1);
	q->next = q->next->next;

}
LinkList Reverse_1(LinkList& L) {//将单链表原地逆置
	LNode* pre = L->next, * p = pre->next, * pnext = p->next;
	pre->next = NULL;
	while (pnext != NULL) {	
		p->next = pre;
		pre = p;
		p = pnext;
		pnext = p->next;
	}
	p->next = pre;
	pre = p;
	L->next = pre;
	return  L;
}
void Sort(LinkList& L) {//对单链表排序
	LNode* pre, * p = L->next, * r = p->next;
	p->next = NULL;
	p = r;
	while (p) {
		r = r->next;
		pre = L;
		while (pre->next != NULL && pre->next->data < p->data)
			pre = pre->next;
		p->next = pre->next;
		pre->next = p;
		p = r;
	}
}
void Delete_min(LinkList& L) {
	LNode* minp,*minpre,*p,*pre;
	minpre=pre = L;
	minp=p = L->next;
	while (p) {
		if (p->data < minp->data) {
			minp = p;
			minpre = pre;
			pre = p;
			p = p->next;		
		}
		else {
			pre = p;
			p = p->next;
		}
	}
	minpre->next = minp->next;
	free(minp);
}
void Print(LinkList &L) {
	LinkList p = L->next;
	while (p) {
		printf("%d ", p->data);
		p = p->next;
	}
}
int main() {
	LinkList L;
	List_HeadInsert(L);
	Print(L);
	printf("\n");
	LNode* q=L;
	Sort(L);
	Print(L);
	return 0;
}

你可能感兴趣的:(王道数据结构大题,c语言,算法)