单链表递增排序

#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) {//删除所有值为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;
			

		}
	}
}
int Length(LinkList L) {//链表长度
	int j = 0;
	LNode* p = L->next;
	while (p) {
		j++;
		p = p->next;
	}
	return j;
}
LinkList Search_1st_Common(LinkList L1, LinkList L2) {//查找公共节点
	int len1 = Length(L1);
	int len2 = Length(L2);
	LinkList longList, shortList;
	int dist;
	if (len1 > len2) {
		longList = L1;
		shortList = L2;
		dist = len1 - len2;
	}
	else {
		longList = L2;
		shortList = L1;
		int dist = len2 - len1;
	}
	while (dist--) {
		longList = longList->next;
	}
	while (longList) {
		if (longList == shortList) {
			return longList;
		}
		else {
			longList = longList->next;
			shortList = shortList->next;
		}

	}
	return NULL;

}
void Min_Delete(LinkList& L) {//按递增顺序输出单链表中各节点数据元素,并释放存储空间
	
	while (L->next != NULL) {
		LNode* pre = L, * p = L->next;
		while (p->next)
		{
			if (pre->next->data >p->next->data) {
				pre = p;
			}
			p = p->next;
		}
		printf("%d ",pre->next->data );
		LNode *u=pre->next;
		pre->next = u->next;
		free(u);
	}
	free(L);
}
LinkList DisCreat(LinkList La) {
	LinkList Lb = (LinkList)malloc(sizeof(LNode)); 
	Lb->next = NULL;
	LinkList r = Lb,p=La->next,pre=La;
	while (p->next) {
		if (p->next->data % 2 == 0) {
			r->next = p;
			p->next = NULL;
			pre->next = p->next;
			p = pre->next;
		}
		pre = p;
		p = p->next;
	}
	return Lb;
}
void RangeDelete(LinkList &L,int min,int max) {
	LNode* pre = L,*p=L->next;
	while (p) {
		if (p->data<max && p->data>min) {
			pre->next = p->next;
			free(p);
			p = pre->next;
		}
		else {
			pre = p;
			p = p->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;
	}
}
void Delete_Reap(LinkList &L)//删除递增链表中的重复元素
{
	LNode * p = L->next,*u;
	if (p == NULL)
		return;
	while (p) {
		 u = p->next;
		if (p->data == p->next->data) {
			
			p->next = u->next;
			free(u);
		}
		else {
			p = p->next;
		}
	}
}
LinkList DisCreat_2(LinkList& La) {//将单链表分解为A表和B表,使A表序号为单数,B表序号为双数
	int i = 0;
	LinkList Lb = (LinkList)malloc(sizeof(LNode));
	Lb->next = NULL;
	LinkList ra = La, rb = Lb;
	LinkList p = La->next;
	La->next = NULL;	
	while (p) {
		i++;
		if (i % 2 == 0) {
			rb->next = p;
			rb = p;
		}
		else {
			ra->next = p;
			ra = p;
		}
		p = p->next;
	}
	ra->next = NULL;
	rb->next = NULL;
	return Lb;
}
int main() {
	LinkList L;
	List_HeadInsert(L);
	Print(L);
	printf("\n");
	/*LinkList p = GetElem(L, 3);
	printf("%d", p->data);
	LinkList q =LocateElem(L,5);
	printf("%d\n", q->data);*/
	LNode* q=L;
	/*L = Reverse_1(L);
	Print(L);
	Sort(L);//递增排序
	Print(L);*/
	LinkList Lb=DisCreat_2(L);
	Delete_Reap(L);//删除单链表中的重复值
	Print(L);
	printf("\n");
	//Print(Lb);
	return 0;
}

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