Data structure - doubly linked list (analog array)

 

 

#include<iostream>
using namespace std;

const int N = 100010;

int Val [N], Le [N], RI [N];
 int IDX;
 int n-, m;
 // initialize ri [0] and le [1] are doubly-linked list about the endpoint 
void the init () {
    RI [ 0 ] = . 1 , Le [ . 1 ] = 0 ;
    idx = 2;
}
// insert a number on the right side of the node k 
void the Add ( int k, int X) {
    val[idx] = x;
    Ri [Idx] = Ri [K];
    the [idx] = k;
    le[ri[k]] = idx;
    ri[k] = idx;
    idx++;
}
//删除节点k
void del(int k){
    le[ ri[k] ] = le[k];
    ri[ le[k] ] = ri[k];
}


int main(){
    cin>>n;
    string ch;
    init();
    int k,x;
    while(n--){
        cin>>ch;
        if(ch == "L"){
            cin>>x;
            add(0,x);
        }
        if(ch == "R"){
            cin>>x;
            add(le[1],x);
        }
        if(ch == "D"){
            cin>>x;
            del(x+1);
        }
        if(ch == "IL"){
            cin>>k>>x;
            add(le[k+1],x);
        }
        if(ch == "IR"){
            cin>>k>>x;
            add(k+1,x);
        }
    }
    
    for(int i = ri[0] ; i != 1 ; i = ri[i]){
        cout<<val[i]<<" ";
    }
    cout<<endl;
    
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Flydoggie/p/12244510.html