zoukankan      html  css  js  c++  java
  • 链表-插入

    代码:

    #include <iostream>
    #include <string>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    struct Node
    {
        int v;
        Node *next;
    };
    Node* create_lian(int n)
    {
        Node *a=new Node,*b;
        b=a;
        cin>>a->v;
        a->next=NULL;
        for(int i=2;i<=n;i++)
        {
            a->next=new Node;
            a=a->next;
            cin>>a->v;
            a->next=NULL;
        }    
        cout<<"Node created."<<endl;
        return b;
    }
    void out_lian(Node *p)
    {
        do
        {
            cout<<p->v<<endl;
            p=p->next;
        }
        while(p!=NULL);
    }
    main()
    {
        int n,x,y;
        Node *a,*b=new Node,*pre,*head;
        cin>>n;
        head=create_lian(n);
        cin>>x>>y;//在x前面插入y
        b->v=y;
        b->next=NULL;
        a=head;
        pre=b;
        while(a!=NULL)
        {
            if(a->v==x)
            {
                pre->next=b;
                b->next=a;
            }
            pre=a;
            a=a->next;
        }
        out_lian(head); 
    }
  • 相关阅读:
    四十八.监控概述 、 Zabbix基础 、 Zabbix监控服务
    123D
    bzoj3879
    bzoj1699
    LA6878
    uoj#149
    687C
    codeforces round #424 div2
    803E
    713C
  • 原文地址:https://www.cnblogs.com/wanjinliu/p/11400125.html
Copyright © 2011-2022 走看看