链表练习题

在这里插入图片描述
一.

//逆置函数1
void Reserve1(PNode mylist)//链表逆置
{
	assert(mylist != NULL);
	Node* p = mylist->next;
	Node* s = NULL;
	mylist->next = NULL;//逆置后最后一个节点的next为空
	while (p != NULL)
	{
		s = p;
		p = p->next;
		s->next = mylist->next;
		mylist->next = s;
	}
}
//逆置函数2一直头插
void Reserve2(PNode plist)
{
	assert(mylist != NULL);
	Node* p = mylist->next;
	Node* s = NULL;
	mylist->next = NULL;//逆置后最后一个节点的next为空
	while (p != NULL)
	{
		p = p->next;
		Insert_head(plist,p->data);
		free(p);
		p=q;
	}
}
//逆置函数3
void Reserve1(PNode plist)//链表逆置
{
	assert(plist != NULL&&plist->next!=NULL&&plist->next->next!=NULL);
	if(plist==NULL||plist->next==NULL&&plist->next->next==NULL)
	return;
	Node* p = plist->next;
	Node* q=p->next;
	Node* s;
	p->next = NULL;//逆置后最后一个节点的next为空
	while (q != NULL)
	{
		s = q->next;
		q->next=p;
		p=q;
		q=r;
	}
	plist->next=p;
}

二.

bool DclPoint(PNode plist, PNode point)
{
	assert(plist != NULL && point != NULL);
	if (plist == NULL || point == NULL)return false;
	if (point->next == NULL)//删除节点是尾节点
	{
		return Del_pos(plist, Get_length(plist) - 1);
	}
	Node* p = point->next;
	point->data = p->data;
	point->next = p->next;
	free(p);
	p = NULL;
	return true;
}

三.

int IsIntersect(PNode pla, PNode plb)
{
	assert(pla != NULL && plb != NULL);
	Node* pa = pla;
	Node* pb = plb;
	int lena = 0;
	int lenb = 0;
	while (pla->next != NULL || plb->next != NULL)
	{
		pla = pla->next;
		plb = plb->next;
		lena++;
		lenb++;
		if (pla->data==plb->data)
		{
			return plb->data;
		}
	}
}
int Get_length(PNode pla)
{
	assert(pla != NULL);
	int count = 0;
	Node* p = pla->next;
	while (p != NULL)
	{
		count++;
		p = p->next;
	}
	return count;
}
Node* Intersect(PNode pla, PNode plb)
{
	assert(pla != NULL && plb != NULL);
	int len1=Get_length(pla);
	int len2=Get_length(plb);

	Node*p=len1>len2?pla:plb;
	Node*q=len1<len2?pla:plb;

	for(int i=0;i<abs(len1-len2);i++)//abs求绝对值
	{
	    p=p->next;
	}
	while(p!=NULL)
	{
	    if(p=q)
		{
		   return p;
		}
		p=p->next;
		q=q->next;
     }
	 return NULL;
}

四.

//返回倒数第k个节点
int Back_k(PNode plist, int k)//k不能为零
{
	assert(plist != NULL);
	Node* p = plist->next;
	Node* q = p;
	int i = 1;
	while (i <= k)
	{
		q = q->next;
		i++;
	}
	while (q!=NULL)
	{
		q = q->next;
		p = p->next;
	}
	return p->data;
}

五.

void IsLoop(Pnode plist)
{
   Node*slow=plist;
   Node*fast=slow->next;
   while(1)
   {
      if(fast==NULL||fast->next==NULL)
	  {
	      return NULL;//没有环
	  }
      slow=slow->next;
	  fast=fast->next;
	  if(fast==slow )
	  {
	     break;
	  }
	//为了找入环点
	   Node*p=plist;
       Node*q=fast;

	   while(p!=q)
	   {
	     p=p->next;
		 q=q->next;
	   }
   }
   return p;
   return true;
}

附第五题图解:
六.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值