交换单链表任意两个元素(完整程序)

#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>

typedef struct node
{
	int data;
	struct node* next;
}linklist;
//typedef struct node linklist;

linklist *L=NULL;

linklist* createList(int *a,int len)
{
 // int data;
  linklist *pCurrent,*rear;
  L=(linklist*)malloc(sizeof(linklist));
  rear=L;

  int count=0;
  while(count<len)
  {
	  pCurrent=(linklist*)malloc(sizeof(linklist));
	  pCurrent->data=a[count];
	  rear->next=pCurrent;
	  rear=pCurrent;
	  count++;
  }
  rear->next=NULL;
  return L;
}
//返回前驱节点
node* FindPre(node* head,node* p)
{
  node *q=head;
  while(q->next!=p&&q!=NULL)
  {
	  q=q->next;
  }
  return q;

}
//返回前驱节点
/*node* FindPre(node* head,node* p)
{
  node *q=head;
  while(q)
  {
	  if(q->next==p)
		  return q;
	  else
		  q=q->next;
  }
  return NULL;
}*/
linklist* Swap(linklist *head,node *p,node *q)
{
	if(head==NULL||p==NULL||q==NULL)
	{
		cout<<"invalid parameter:NULL."<<endl;
		return head;
	}
	if(p->data==q->data)
		return head;
	if(p->next==q)
	{
     node* pre_p=FindPre(head,p);
	 pre_p->next=q;
	 p->next=q->next;
	 q->next=p;
	}
	else if(q->next==p)
	{
      node* pre_q=FindPre(head,q);
	  pre_q->next=p;
	  q->next=p->next;
	  p->next=q;
	}
	else if(p!=q)
	{
     node* pre_q2=FindPre(head,q);
	  node* pre_p2=FindPre(head,p);
	  node* after_p=p->next;
      p->next=q->next;
      q->next=after_p;

	 pre_q2->next=p;
	 pre_p2->next=q;
	 
	 
	}
	return head;
}

int main()
{
	int i,j;
	int a[]={4,5,1,6,12,9,23,15,19};
	int lens=sizeof(a)/sizeof(a[0]);

	createList(a,lens);
   linklist* p;
   p=L;
	while(p->next!=NULL)
	{
		printf("%d ",p->next->data);
		p=p->next;
	}
   node *temp1,*temp2;
   temp1=(node*)malloc(sizeof(node));
   temp2=(node*)malloc(sizeof(node));
    temp1=L->next;
	temp2=L->next;
   for(i=0;i<3;i++)
   {
	   temp1=temp1->next;
   }
   for(j=0;j<6;j++)
   {
     temp2=temp2->next;
   }
  
   printf("\n");
   Swap(L,temp1,temp2);
   p=L;
	while(p->next!=NULL)
	{
		printf("%d ",p->next->data);
		p=p->next;
	}
		
	return 0;
}







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值