#include <iostream>#include<stdlib.h>using namespacestd;structNode {structNode *Next; intvalue;}; Node*createlistnode (intvalue) { if(value==NULL)returnNULL; Node*pnode= (node*) malloc (sizeof(node)); Pnode->value=value; Pnode->next=NULL; returnPnode;}voidConnectnodes (node*pcurrent,node*Pnext) { if(pcurrent==NULL) {cout<<"Error to connect the nodes."<<Endl; Exit (1); } pcurrent->next=Pnext;}voidPrintlist (node*phead) {Node*pnode=Phead; while(pnode!=NULL) {cout<<pNode->value<<" "; Pnode=pnode->Next; } cout<<Endl;}structNode *reverse (structNode *list) { if(list==NULL)returnNULL; if(list->next==NULL)returnlist; Node* tpre=list; Node* tmp=list->Next; while(TMP) {node* tnext=tmp->Next; TMP->next=Tpre; Tpre=tmp; TMP=Tnext; } list->next=NULL; List=Tpre; returnlist;}structNode *swap (structNode *list) { if(list==NULL)returnNULL; if(list->next==NULL)returnlist; Node*p=list; Node*oddhead= (node*) malloc (sizeof(node)); Node*evenhead= (node*) malloc (sizeof(node)); Node*odd=Oddhead; Node*even=Evenhead; while(p) {if(p->value%2==0) {even->next=p; Even=p; } Else{ODD->next=p; ODD=p; } P=p->Next; } even->next=NULL; ODD->next=NULL; Even=reverse (evenhead->next); ODD=reverse (oddhead->next); Oddhead=Odd; while(odd->next) ODD=odd->Next; ODD->next=even; returnOddhead; //return oddhead;}intMain () {node*pnode1=createlistnode (4); Node*pnode2=createlistnode (5); Node*pnode3=createlistnode (7); Node*pnode4=createlistnode (1); Node*pnode5=createlistnode (6); Connectnodes (PNODE1,PNODE2); Connectnodes (PNODE2,PNODE3); Connectnodes (PNODE3,PNODE4); Connectnodes (PNODE4,PNODE5); Node* p=swap (PNODE1); Printlist (P); return 0;}
The odd number is in the front even after. Connected after each reversal