王道数据结构第二章18-25


//18
Linklist linkmix(Linklist &A,Linklist &B)
{
	LNode *h1=A,*h2=B;
	while(h1->next!=A)
		h1=h1->next;
	while(h2->next!=B)
		h2=h2->next;
	h1->next=B;
	h2->next=A;
	return A;
}

//19
void del_cycle(Linklist &l)
{
	LNode *pre,*p,*temp;
	while(l!=NULL)
	{
		pre=l,p=l->next
		while(p!=l)
		{
			if(p->next<pre->next)
			{
				pre=p;
				p=p->next;
			}
			else
				p=p->next;
		}
		temp=pre->next;
		pre->next=pre->next->next;
		free(temp);
	}
	free(l);
}

//20
Linklist locate(Linklist &l,Elemtype x)
{
	LNode *p=l->next,*q;
	while(p!=NULL&&p->data!=x)
		p=p->next;
	if(p==NULL)
		print("x不存在");
		return 0;
	else
	{
		p->freq++;
		if(p->next!=NULL)
			{
				p->next->pre=p->pre;
			}
		p->pre->next=p->next;
		q=p->pre;
		while(q!=l&&q->freq<=p->freq)
			q=q->pre;
		p->next=q->next;
		q->next->pre=p;
		p->pre=q;
		q->next=p;
	}
	return p;
}

//21
typedef int Elemtype;
typedef struct LNode
{
	Elemtype data;
	struct LNode *link;
}LNode,*Linklist;
int research(Linklist &L,int k)
{
	LNode *p,*q;
	p=l->link;
	q=q->link;
	int count=0;
	while(p->next!=NULL)
	{
		if(count<k) count++;
		else p=p->link;
		q=q->link;
	}
	if(count<k) return 0;
	else 
	{
		print("q->data");
		return 1;
	} 
}

//22
typedef struct LNode
{
	Elemtype data;
	struct LNode *next;
}LNode,*Linklist;
int listlen(Linklist &l)
{
	int len=0;
	LNode *p;
	while(p!=NULL)
	{
		p=p->next;
		len++;
	}
	return len;
}
LNode find_addr(LNode *a,*b)
{
	LNode *p,*q;
	int m,n;
	m=listlen(a);
	n=listlen(b);
	for(p=a;m>n;m--)
		p=p->next;
	for(p=b;m<n;n--)
		q=q->next;
	while(p->next&&q->next!=p->next)	
	{
		p=p->next;
		q=q->next;
	}
	return q->next;
}

//23
typedef struct node
{
	int data;
	struct LNode *link;
}Node;
void function(Node *h,int n)
{
	Node *p=h,*r;
	int m=0;
	int q[n+1]={0};
	while(p->link!=NULL)
	{
		m=p->next->data;
		if(m<0) m=-m;
		if(a[m]==0) 
		{
			a[m]=1;
			p=p->next;
		}
		else
		{
			temp=p->next;
			p->next=temp->next;
			free(temp);
		}
	}
	free(q);
}

//24
LNode* FindLoopStart(LNode *head)
{
	LNode *fast=head,*slow=head;
	while(slow->next!=NULL&&fast->next!=NULL)
	{
		slow=slow->next;
		fast=fast->next->next;
		if(fast==slow)
			break;
	}
	if(slow==NULL&&fast==NULL)
	{
		return 0;	
	}
	LNode *p=head;*q=slow;
	if(p!=q)
	{
		p=p->next;
		q=q->next;
	}
	return p;
}

//25
void changelink(Linklist *l)
{
	LNode *p=l,*q=l;
	while(p->next!=NULL&&q->next!=NULL)
	{
		p=p->next;
		q=q->next;
		if(q->next!=NULL) q=q->next;
	}
	LNode *h;
	h->next=p;
	p->next=NULL
	while(p!=NULL)
	{
		r=p;
		p->next=h->next;
		h->next=p;
		p=r->next;
	}
	LNode *s=l->next;
	LNode *w=h->next;
	l->next=NULL;
	while(w!=NULL)
	{
		l->next=s;
		s->next=w;
		l=w;
		s=s->next;
		w=w->next;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值