链表的构建(数组&结构体)

//如有误求大佬指正!!

#include<bits/stdc++.h>
using namespace std;
struct node { //next即后面节点的编号,data就是需要维护的数据
    int next;
    int data;
    int pre;
} a[10000000];
node NO={-1,-1};//NULL通常用于表示指针的空值, 这里用-1代替
int head=0; //head即头节点的编号
int p,z,n,h,cnt=0;
bool f=1;
string c;
int findele(int ele){
    int hh=head;
    int ff;
    while(1){
        if(hh=a[hh].next==0){
            break;
        }        
        if(ele==hh){
            return ff;
        }
        ff=hh;
        hh=a[hh].next;
    }
    return 0;
}
int find(int mc){
    int hh=head;
    while(1){
        if(hh=a[hh].next==0){
            break;
        }        
        if(mc==hh){
            return a[hh].data;
        }
        hh=a[hh].next;    
    }
    return 0;
}
int main(){
    h=head;
    cout<<"----链表----" <<endl;
    cout<<"(请不要输入0)"<<endl;
    cout<<"请输入节点个数:"<<endl;
    cin>>n;
    for(int i=1;i<=n;i++){
        cout<<"请输入节点名称:"<<endl;        
        cin>>p;
        a[h].next=p;
        cout<<"请输入节点数据:"<<endl;
        cin>>a[p].data;
        h=p;
        cout<<"节点编号:"<<i<<endl;
    }
    h=head;
    fuck:
    cnt=0;
    cout<<"\n----------------------\n";
    cout<<"end----结束操作\n";
    cout<<"insert----插入\n";
    cout<<"delete----删除\n";
    cout<<"----------------------\n";
    while(f){    
        cout<<"请输入操作:"<<endl;
        cin>>c;
        if(c=="end"){
            f=0;
        }
        if(c=="insert"){
            cout<<"请输入插入节点名称:"<<endl;
            sh:
            int neww;
            cin>>neww;
            re:
            int pre=0;            
            if(a[neww].data==0){
                cout<<"名称正常可以输入,请输入插入的前节点名称:"<<endl;
                scanf("%d",&pre);
                if(a[pre].data==0){
                    cout<<"前节点名称不存在,请重新输入:"<<endl;                    
                    goto re;
                }
                a[neww].next=a[pre].next;
                a[pre].next=neww;
                cout<<"请输入插入节点数据:"<<endl;
                cin>>a[neww].data;    
                n++;            
                cout<<"操作完成"<<endl;
            }else if(a[pre].data!=0){
                char YN;
                cout<<"该名称已有数据,是否覆盖?(Y/N)"<<endl;
                ree:
                cin>>YN;
                if(YN=='Y'||YN=='y'){
                    cout<<"请输入插入数据:"<<endl;
                    cin>>a[neww].data;    
                    n++;
                    cout<<"操作完成"<<endl;        
                }else if(YN=='N'||YN=='n'){
                    cout<<"操作已取消"<<endl;
                }else {
                    cout<<"输入错误,请重新输入:"<<endl;
                    goto ree;
                }
            }else{
                cout<<"输入错误,请重新输入:"<<endl;
                goto sh;
            }
        }
        if(c=="delete"){
            cout<<"请输入删除节点名称:"<<endl;
            int le;
            cin>>le;
            re1:
            if(a[le].data==0){
                cout<<"该节点不存在,请重新输入:"<<endl;
                goto re1;
            }
            a[findele(le)].next=a[le].next;
            cout<<"删除成功"<<endl;
        }
        if(c=="find"){
            cout<<"查找节点data?(d)  查找节点名称(m)"<<endl;
            ree1:
            char pd;            
            cin>>pd;
            if(pd=='d'){
                cout<<"请输入查找节点的名称:"<<endl;
                int cha;
                cin>>cha;
                re2:
                if(a[cha].data==0){
                    cout<<"该节点不存在,请重新输入:"<<endl;
                    goto re2;
                }
                cout<<"查找名称为"<<cha<<"的节点的data为"<<a[cha].data<<endl;            
            }if(pd=='m'){
                cout<<"请输入查找节点的数据:"<<endl;                
                if(find(pd)){
                    cout<<"查找节点的数据为:"<<find(pd);
                }else{
                    cout<<"该节点不存在"<<endl;
                }
            }else{
                cout<<"输入错误,请重新输入:"<<endl;
                goto ree1;
            }        
        }
        else{
            cout<<"输入错误,请重新输入:"<<endl;
            goto fuck;
        }
    }
    for(int i=1;i<=n;i++){
        cout<<"节点名称:"<<a[h].next;
        cout<<"   节点数据:"<<a[a[h].next].data;
        h=a[h].next;
        cout<<"      节点编号:"<<i<<endl;
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值