• 链表A和B的有序合并,合并过程不产生新的节点,合并后B消失


    /*有序链表的合并,合并过程不产生新的节点,如A和B合并,合并和B在A中,B消失*/
    #include <iostream>
    using namespace std;
    typedef struct Node 
    {
        int element;
        Node *pNext;
    }Node,*LinkList;
    void InitList(LinkList& head)
    {
        head=(LinkList)malloc(sizeof(Node));
        head->pNext=NULL;
    }
    int ListLength(LinkList head)
    {
        int length=-1;
        LinkList p=head;
        while (p!=NULL)
        {
            length++;
            p=p->pNext;
        }
        return length;
        
    }
    void InsertList(LinkList& head,int element){
        int length=ListLength(head);
        if(length==-1)
            return;
        LinkList q=head;
        LinkList p=head->pNext;
        while (p)
        {
            if (element<p->element)
            {
                break;
            }
            q=p;
            p=p->pNext;
        }
        LinkList temp=(LinkList)malloc(sizeof(Node));
        temp->element=element;
        q->pNext=temp;
        temp->pNext=p;    
    }
    
    void PrintList(LinkList head)
    {
        if (head==NULL)
        {
            return;
        }
        LinkList p=head->pNext;
        while (p)
        {
            cout<<p->element<<endl;
            p=p->pNext;
        }
    }
    void DestroyList(LinkList& head)
    {
        if(head==NULL)
            return;
        LinkList q=head;
        LinkList p=head->pNext;
        while (p)
        {
            free(q);
            q=p;
            p=p->pNext;
        }
        free(q);
        head=NULL;
    }
    void InsertList(LinkList& headA,LinkList linklist)
    {
        if (headA==NULL)
            return;
        LinkList q=headA;
        LinkList p=q->pNext;
        while (p)
        {
            if (linklist->element<p->element)
            {
                break;
            }
            q=p;
            p=p->pNext;
        }
        q->pNext=linklist;
        linklist->pNext=p;
    }
    void UnionAB(LinkList &headA,LinkList &headB)
    {
        if (ListLength(headB)<1)
            return;
        LinkList q=headB;
        LinkList p=headB->pNext;
        free(headB);
        headB=NULL;
        while (p)
        {
            q=p->pNext;
            InsertList(headA,p);
            p=q;
        }
    }
    void main()
    {
        LinkList headA;
        InitList(headA);
        InsertList(headA,1);
        InsertList(headA,8);
        InsertList(headA,5);
        InsertList(headA,10);
        PrintList(headA);
    
        LinkList headB;
        InitList(headB);
        InsertList(headB,15);
        InsertList(headB,2);
        InsertList(headB,7);
        InsertList(headB,12);
        PrintList(headB);
        UnionAB(headA,headB);
        PrintList(headA);
        DestroyList(headA);
        DestroyList(headB);
    
    }
  • 相关阅读:
    thinkphp中ajax使用实例(thinkphp内置支持ajax)
    Uncaught SyntaxError: Invalid regular expression flags(看页面源代码)
    php函数实现显示几秒前,几分钟前,几天前等方法(网络上什么都有)
    php实现二叉树遍历
    php实现数组中的逆序对(归并排序实现:排序 辅助数组)
    基于HTML5气3D仿真培训系统
    CSDN Androidclient生产 导航帖
    MongoDB日常保养
    Machine Learning—Linear Regression
    OpenCVR 有新成员 OpenCVV OpenCVC
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2654045.html
Copyright © 2020-2023  润新知