zoukankan      html  css  js  c++  java
  • 链表-基础

    #include<iostream>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    struct Node
    {
        Node* next;
        int val;
        Node() {};
        Node(int v)
        {
            val=v;
            next=nullptr;
        }
    };
    
    void CreateList(Node* head,int n)
    {
        Node* L=head;
        for(int i=0; i<n; i++)
        {
            Node* tmp=new Node(i);
            L->next=tmp;
            L=L->next;
        }
        L->next=nullptr;
    }
    void PrintList(Node* L)
    {
        Node* q;
        q=L->next;//去掉头结点
        while(q)
        {
            cout<<q->val<<" ";
            q=q->next;
        }
        cout<<endl;
    }
    Merge_List(Node* Head1,Node* Head2)
    {
        Node* L3=new Node();
        Node* Head3=L3;
        Node* L1=Head1->next;//去除头结点
        Node* L2=Head2->next;
        while(L1&&L2)
        {
            if(L1->val<=L2->val)
            {
                L3->next=L1;
                L1=L1->next;
            }
            else
            {
                L3->next=L2;
                L2=L2->next;
            }
            L3=L3->next;
        }
        if(L1)
            L3->next=L1;
        else if(L2)
            L3->next=L2;
        PrintList(Head3);
    }
    int main()
    {
        Node* Head1=new Node();
        Node* Head2=new Node();
        CreateList(Head1,5);
        CreateList(Head2,5);
        PrintList(Head1);
        PrintList(Head2);
        Merge_List(Head1,Head2);
        return 0;
    }
    /**
    
    */
  • 相关阅读:
    常用的正则表达式
    Nginx反向代理
    docker-day1-安装和基本使用
    Nginx + Keepalived
    Nginx源码安装
    apache-实战(二)
    apache-实战(一)
    apache--配置文件属性介绍
    软件目录结构规范
    python常用模块(二)
  • 原文地址:https://www.cnblogs.com/caijiaming/p/13207300.html
Copyright © 2011-2022 走看看