zoukankan      html  css  js  c++  java
  • 奇数在前偶数在后。各自反转后相连

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    struct node {
        struct node *next;
        int value;
    };
    node *CreateListNode(int value)
    {
        if(value==NULL)
            return NULL;
        node *pNode=(node*)malloc(sizeof(node));
        pNode->value=value;
        pNode->next=NULL;
        return pNode;
    }
    void ConnectNodes(node*pCurrent,node* pNext)
    {
        if (pCurrent==NULL)
        {
            cout<<"Error to connect two nodes."<<endl;
            exit(1);
        }
        pCurrent->next=pNext;
    }
    void PrintList(node* pHead)
    {
        node *pNode=pHead;
        while (pNode!=NULL)
        {
            cout<<pNode->value<<" ";
            pNode=pNode->next;
        }
        cout<<endl;
    }
    struct node *reverse(struct node *list)
    {
        if (list==NULL)
            return NULL;
        if(list->next==NULL)
            return list;
        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;
        return list;
    }
    struct node *swap(struct node *list)
    {
        if (list==NULL)
            return NULL;
        if (list->next==NULL)
            return list;
        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;
        return OddHead;
        //return OddHead;
    }
    
    int main()
    {
        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;
    }
  • 相关阅读:
    45、linux shell命令,ldconfig
    47、linux shell,ln链接
    43、linux shell命令,chmod
    39、linux 进程管理
    46、linux shell命令,chkconfig
    40、linux shell常用函数mkdir,rmdir,mount
    26、linux 几个C函数,nanosleep,lstat,unlink
    38、linux shell常用函数,nice
    44、linux shell命令,ldd
    41、linux shell常用函数,lsof
  • 原文地址:https://www.cnblogs.com/xiaodi914/p/5853253.html
Copyright © 2011-2022 走看看