将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并使奇数之间顺序反转,偶数之间顺序反转。

将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并使奇数之间顺序反转,偶数之间顺序反转。

示例:

交换前链表的顺序           交换后链表的顺序

4→5→7→1→6   ==>  1→7→5→6→4

1                ==>  1                   (链表仅含一个元素)

2→1            ==>  1→2 

                   ==>                        (链表为空)

 

C/C++:

链表节点定义为:

struct node {

struct node *next;

int value;

};

struct node*swap(struct node *list);

#include<iostream>

using namespace std;

struct node
{
	struct node *next;
	int value;
};


node * CreateListNode(int value)//链表中创建节点
{
	if(value == NULL)
		return NULL;
	node *pNode = new node();
	pNode->value = value;
	pNode->next =NULL;
	return pNode;
}

void ConnectListNodes(node *pCurrent, node *pNext)	// 连接两个节点
{
	if(pCurrent == NULL)
	{
		cout<<"Error to connect two nodes."<<endl;
		exit(1);
	}
	pCurrent->next = pNext;
}

void PrintList(node *pHead)
{
	cout<<"PrintList start."<<endl;
	node *pNode = pHead;
	while(pNode != NULL)
	{
		cout<<pNode->value;
		pNode = pNode->next;
	}
	cout<<endl;
	cout<<"PrintList"<<endl;
}

void Destroy(node *pHead)
{
	node *pNode = pHead;
	while(pNode != NULL)
	{
		pHead = pHead->next;
		delete pNode;
		pNode = pHead;
	}
}

//返回,p所对应位置的下一个位置对应的值
 int dqExchage(struct node *list1,struct node *list2,struct node *list3,int p)
 {
 	if(p %2 ==0){//偶数
		while(list1->next && list1->value % 2 !=0){
 			list1=list1->next;
 		}
 		list2=list1;		
 	}
 	
	int tmp=list1->value;
	int q = p;
 	while( list1->next && list1->value !=q){
		list1->value = p;
		p = tmp;
		list1 = list1->next;
		tmp = list1->value;
		
 	}
	list1->value = p;
	
	if(list1->next){
		return list1->next->value;
 	}else{
 		return NULL;
 	}
 	
 }
 //思路分析
 // 特殊的插入排序
 //针对奇数,插入到最前面,针对偶数,插入到前面奇数的后面,使其成为第一个偶数
struct node *swap(struct node *list)
{	
	node *pHead = list;
	node *pHead1 = list;

	//0
	if(list->next){
		int p=list->next->value;
		while(p != NULL){
			p=dqExchage(list, list, list,p);
		
		}
		return list;
	}else{
		return NULL;
	}

}



void Test1()
{
	node *pNode1 = CreateListNode(4);
	node *pNode2 = CreateListNode(5);
	node *pNode3 = CreateListNode(7);
	node *pNode4 = CreateListNode(1);
	node *pNode5 = CreateListNode(6);

	ConnectListNodes(pNode1, pNode2);
	ConnectListNodes(pNode2, pNode3);
	ConnectListNodes(pNode3, pNode4);
	ConnectListNodes(pNode4, pNode5);
	PrintList(pNode1);
	PrintList(swap(pNode1));
}

void Test2()
{
	node *pNode1 = CreateListNode(4);
	node *pNode2 = CreateListNode(2);
	node *pNode3 = CreateListNode(7);
	node *pNode4 = CreateListNode(8);
	node *pNode5 = CreateListNode(6);

	ConnectListNodes(pNode1, pNode2);
	ConnectListNodes(pNode2, pNode3);
	ConnectListNodes(pNode3, pNode4);
	ConnectListNodes(pNode4, pNode5);
	PrintList(pNode1);
	
	PrintList(swap(pNode1));
}


int main()
{
	cout<<"Test1"<<endl;

	Test1();
	cout<<"Test2"<<endl;
	Test2();
	//
	system("pause");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值