(仅供参考)王道2020算法题代码2.3(P38-P41)

(仅供参考)王道2020算法题代码2.3(P38-P41)


部分题目有多种解法或次优解
#include<stdio.h>
#include<malloc.h>
#define Elem int 

 
 //单链表结构定义
 typedef struct LNode
 {
 	Elem data;
 	struct LNode* next;
  }LNode; 

//头插法建立链表 
LNode* HeadInsert(void)
{
	LNode* L = NULL;
	L = (LNode*)malloc(sizeof(LNode));	//头结点 
	L->next = NULL;
	
	int x;
	printf("Please enter the Value you want to creat in LNode: ");
	scanf("%d", &x);
	LNode* S;
	while(x != 9999)
	{
		S = (LNode*)malloc(sizeof(LNode));
		S->data = x;
		S->next = L->next;//带头结点 
		L->next = S;	  //带头结点
	//	S->next = L;	//不带头结点 
	//	L = S;			//不带头结点
		printf("Please enter the Value you want to creat in LNode: ");
		scanf("%d", &x);
	}
	
	return L;
 } 
 
//尾插法建立单链表
LNode* TailInsert(void)
{
	LNode* L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	LNode* r = L;
	LNode* S;
	
	int x;
	printf("Please enter the Value you want to creat in LNode: ");
	scanf("%d", &x);
	while(x != 9999)
	{
		S = (LNode*)malloc(sizeof(LNode));
		S->data = x;
		r->next = S;
		r = S;
		printf("Please enter the Value you want to creat in LNode: ");
		scanf("%d", &x);
	}
	
	r->next = NULL;
	return L;
} 

//按序号i查找第i个节点
LNode* GetLNode(LNode* L, int i)
{
	if(i < 0)
		return NULL;
	else if(i == 0)
		return L;
	else
	{
		LNode* S = L->next;
		for(int j = 2; j <= i && S; j++)
			S = S->next;
			
		return S;
	}
} 
//按给定值找差单链表
LNode* LocateElem(LNode* L, Elem e)
{
	LNode* S = L->next;
	while(S->data != e && S)
		S = S->next;
	
	return S;
 } 
 
//双链表结构定义
typedef struct DNode
{
	struct DNode* prior;
	Elem data;
	struct DNode* next;
 } DNode;
 
 //T1先操作再递归最后返回 
void deletex(LNode* &L, Elem x)
{
	if(L)
	{
		if(L->data == x)
		{
			LNode* p = L;
			L = L->next;
			free(p);
			deletex(L, x); //此时需要检查的下一个节点已经成为L所指向的节点 
		}
		else
			deletex(L->next, x);//递归下一个节点 
	}
} 
 //T1先递归到底再层层返回操作 
void deletenextx(LNode* L, Elem x) //递归删除下一个value为x的节点 
{
	if(L->next)
	{
		deletenextx(L->next, x);
		if(L->next->data == x)
		{
			LNode* p = L->next;
			L->next = p->next;
			free(p);
		}
	}
}
void deletex_1(LNode* &L, Elem x)
{
	deletenextx(L, x);
	if(L->data == x)//若第一个节点value为x,删除 
	{
		LNode* p = L;
		L = L->next;
		free(p);
	}
 } 
 /*T1错误解法
 void deletex(LNode* &L, Elem x)
{
	if(L)
	{
		LNode* n = L->next; //这里递归使用后有可能需要修改的是L本身只修改副本指针无用 
		if(L->data == x)
		{
			LNode* p = L;
			L = L->next;
			free(p);
		}
		
			deletex(n, x);//递归下一个节点 
	}
} 
 */ 
 //T2遍历法 
 void deletexhead(LNode* &L, Elem x)
 {
 	LNode* l, * r, * q;
 	l = L;
 	r = L->next;
 	while(r)
 	{
 		if(r->data == x)
 		{
 			q = r;
 			r = r->next;
 			l->next = r;
 			free(q);
		}
		else
		{
			r = r->next;
			l = l->next;
		}
	}
  } 
//T2重新建立合法单链表 
void deletexhead_2(LNode* &L, Elem x)
{
	LNode* l = L, * r = L->next, *q;
	while(r)
	{
		if(r->data != x)
		{
			l->next = r;
			l = r;
			r = r->next;
		}
		else
		{
			q = r;
			r = r->next;
			free(q);
		}
	}
}

//T3递归算法 
void R_printfvalue(LNode* L)
{
	while(L)
	{
		R_printfvalue(L->next);
		printf("%d\n", L->data);
	}
}

//T4
void deletemin(LNode* &L)
{
	LNode* r = L->next;
	LNode* l = L;
	LNode* pmin = r;
	LNode* pminp = l;
	
	while(r)
	{
		if(pmin->data > r->data)
		{
			pmin = r;
			pminp = l;
		}
		r = r->next;
		l = l->next;
	}
	
	pminp->next = pmin->next;
	free(pmin);
}

//T5重新头插法 
void Reverse_link(LNode* &L)
{
	LNode* p = L->next, *temp;
	L->next = NULL;
	while(p)
	{
		temp = p->next;
		p->next = L->next;
		L->next = p;
		p = temp;
	}
}
//T5指针逆置法
void Reverse_link1(LNode* &L)
{
	LNode* l = L->next;
	LNode* m = l->next;
	LNode* r = m->next;
	l->next = NULL;
	while(r)
	{
		m->next = l;
		l = m;
		m = r;
		r = r->next;
	}
	m->next = l;
	L->next = m;
 } 
 
//T6
void sort_link(LNode* &L)
{
	LNode* l;
	LNode* p = L->next->next;
	LNode* r = p->next;
	L->next->next = NULL;
	while(p)
	{
		l = L;
		while(l->next && l->next->data < p->data)
			l = l->next;
		r = p->next;
		p->next = l->next;
		l->next = p;
		
		p = r;
	}
}
//T7同T2,只写一种 
void deleterange_link(LNode* &L, Elem min, Elem max)
{
	LNode* l = L;
	LNode* p = l->next;
	LNode* r = p->next;
	
	while(p)
	{
		if(p->data > min && p->data < max)
		{
			l->next = p->next;
			free(p);
			p = l->next;
		}
		else
		{
			l = l->next;
			p = p->next;
		}
	}
}

//T8
int Length(LNode* L)
{
	int count = 0;
	L = L->next;
	while(L)
	{
		count++;
		L = L->next;
	}
	return count;
}
LNode* Search_common(LNode* L1, LNode* L2)
{
	int len1 = Length(L1);
	int len2 = Length(L2);
	L1 = L1->next;
	L2 = L2->next;
	int t;
	LNode* temp;
	
	if(len1 > len2)
	{
		temp = L1;
		L1 = L2;
		L2 = temp;
		t = len2;
		len2 = len1;
		len1 = t;
	}
	t = len2 - len1;
	
	while(t--)
	{
		L2 = L2->next;
	}
	while(L1 && L1 != L2)
	{
		L1 = L1->next;
		L2 = L2->next;
	}
	if(L1)
		return NULL;
	else
		return L1;

}

//T9
void printfgrow(LNode* &L)
{
	while(L->next)
	{
		LNode* l = L;
		LNode* p = L->next;
		LNode* minl = l;
		LNode* min = p;
		while(p)
		{
			if(min->data > p->data)
			{
				min = p;
				minl = l;
			}
			p = p->next;
			l = l->next;
		}
		printf("%d\n", min->data);
		minl->next = min->next;
		free(min);
	}
	free(L);
}

//T10
void demerge_link(LNode* &L, LNode* &L1, LNode* &L2)
{
	int count = 1;
	L1 = L2 = (LNode*)malloc(sizeof(LNode)); 
	L1->next = L2->next =NULL;
	LNode* r1 = L1;
	LNode* r2 = L2;
	LNode* p = L->next;
	while(p)
	{
		if(count%2 != 0)
		{
			r1->next = p;
			r1 = p;
		}
		else
		{
			r2->next = p;
			r2 = p;
		}
		p = p->next;	
	} 
	r1->next = r2->next = NULL;
	 
	free(L);
}

//T11
void demerge_link1(LNode* L, LNode* L1, LNode* L2)
{
	L1 = L2 = (LNode*)malloc(sizeof(LNode));
	L1->next = L2->next = NULL;
	LNode* r1 = L1;
	LNode* q2;
	LNode* p = L->next;
	while(p)
	{
		r1->next = p;
		r1 = p;
		p = p->next;
		
		q2 = p->next;
		p->next = L1->next;
		L1->next = p;
		p = p->next;
	}
	r1->next = NULL;
	
	free(L);
}

//T12
void deletesame_link(LNode* &L)
{
	if(L->next == NULL)
		return;
	LNode* l = L->next;
	LNode* p = l->next;
	while(p)
	{
		if(l->data == p->data)
		{
			l->next = p->next;
			free(p);
			p = l->next;
		}
		else
		{
			l = p;
			p = p->next;
		}	
	}
}

//T13
LNode* Merge_link(LNode* L1, LNode* L2)
{
	LNode* p1, * p2, * q;
	p1 = L1->next;
	p2 = L2->next;
	L1->next = NULL;
	
	while(p1 && p2)
	{
		if(p1->data > p2->data)
		{
			q = p2->next;
			p2->next = L1->next;
			L1->next = p2;
			p2 = q; 
		}
		else
		{
			q = p1->next;
			p1->next = L1->next;
			L1->next = p1;
			p1 = q;
		}
	}
		while(p2)
		{
			q = p2->next;
			p2->next = L1->next;
			L1->next = p2;
			p2 = q;
		}
		while(p1)
		{
			q = p1->next;
			p1->next = L1->next;
			L1->next = p1;
			p1 = q;
		}

	free(L2);
	return L1;
}

//T14
LNode* samecreat(LNode* L1, LNode* L2)
{
	LNode* L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	L1 = L1->next;
	L2 = L2->next;
	LNode* r = L;
	while(L1 && L2)
	{
		if(L1->data = L2->data)
		{
			r->next = (LNode*)malloc(sizeof(LNode));
			r->next->data = L1->data;
			r = r->next;
			L1 = L1->next;
			L2 = L2->next;
		}
		else if(L1->data < L2->data)
			L1 = L1->next;
		else
			L2 = L2->next;
	}
	r->next = NULL;
	
	return L;
}

//T15
void Union(LNode* &la, LNode* &lb)
{
	LNode* pa, * pb, * prep, * temp;
	pa = la->next;
	pb = lb->next;
	prep = la;
	while(pa && pb)
	{
		if(pa->data == pb->data)
		{
			temp = pb;
			pb = pb->next;
			free(temp);
			prep = pa;
			pa = pa->next;
			
		}
		else if(pa->data < pb->data) 
		{
			prep->next = pa->next;//保持A不断 
			temp = pa;
			pa = pa->next;
			free(temp);
		}
		else
		{
			temp = pb;
			pb = pb->next;
			free(temp);
		 } 
	}
	if(pb)	//确保pa指向未空表 
		pa = pb; 
	while(pa)
	{
		temp = pa;
		pa = pa->next;
		free(temp); 
	}
	prep->next = NULL;
	free(lb);
}
//T16字符串子串(略KMP类型算法) 
bool pattern(LNode* LA, LNode* LB)
{
	LNode* pa = LA->next;
	LNode* pb = LB->next;
	LA = LA->next;
	LB = LB->next;
	while(LA)
	{
		if(LA->data = LB->data)
		{
			pa = LA->next;
			pb = LB->next;
			while(pa && pb && pa->data == pb->data)
			{
				pa = pa->next;
				pb = pb->next;
			}
			if(!pb)
				return true;
			if(!pa)
				return false;
		}
	
		LA = LA->next;
	}
}

//T17
bool symmetry(DNode* L)
{
	DNode* l = L->next, * r = L->prior;
	while(l != r && l->next != r)
	{
		if(l->data != r->data)
			return false;
		else
			{
				l = l->next;
				r = r->prior;
			}
	}
	
	return true;
} 

//T18
void Link(LNode* &h1, LNode* &h2)
{
	LNode* p1 = h1;
	LNode* p2 = h2;
	while(p1->next != h1)
		p1 = p1->next;
	while(p2->next != h2)
		p2 = p2->next;
	p1->next = h2->next;		//h1尾节点连接h2第一个节点 
	free(h2);					//h2去冗余头操作 
	p2->next = p1; 
}

//T19
void printflinkmin(LNode* &L)
{
	Elem min;
	LNode* minpre, * p;
	while(L->next != L)
	{
		min = L->next->data;
		minpre = L;
		p = L->next;
		while(p->next != L)
		{
			if(p->next->data < min)
			{
				minpre = p;
				min = p->next->data;
			}	
			p = p->next;
		}
		printf("%d\n", min);
		p = minpre->next;
		minpre->next = p->next;
		free(p);
	}
	
	free(L);
}
//T20
typedef struct freqDNode
{
	int freq;
	struct freqDNode* pred;
	Elem data;
	struct freqDNode* next;
}freqDNode;
freqDNode* Locate1x(freqDNode* &L, Elem x)
{
	freqDNode* p = L->next;
	freqDNode* q = L->next;
	while(p && p->data != x)
		p = p->next;
	if(!p)
		return NULL;
	p->freq ++;
	p->pred->next = p->next;
	p->next->pred = p->pred;
	
	while(q && q->freq > p->freq)
		q = q->next;
	p->next = q;
	p->pred = q->pred;
	q->pred->next = p;
	q->pred = p;
	
	return p;
}

//T21
int SearchR_k(LNode* list,int k)
{
	LNode* p = list->next;
	LNode* q = p;
	k = k-1;
	while(k-- && p)
		p = p->next;
	if(!p)
		return 0;
	while(p->next)
	{
		p = p->next;
		q = q->next;
	}
	
	printf("%d", q->data);
	return 1;
}

//T22同T8

//T23
void deleteabssame_link(LNode* &head)
{
	int n = 10;
	int a[n+1] = {0};
	LNode* p = head->next;
	LNode* l = head;
	int m;
	while(p)
	{
		if(p->data < 0)
			m = -p->data;
		else
			m = p->data;
		if(a[m] == 0)
		{
			a[m]++;
			l = l->next;
			p = p->next;
		}
		else
		{
			l->next = p->next;
			free(p);
			p = l->next;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值