万金流
初次使用博客园,目前感觉还不错。 不知不觉用了4年零4个月了,越来越喜欢博客园。

代码:

#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); 
}

 

posted on 2019-08-23 15:07  万金流  阅读(279)  评论(0编辑  收藏  举报