2 - list

Single list

Only to have a linked list pointing direction

Common operations to insert and delete nodes node

 

Template entitled acwing 826 title

Analog with an array of linked list structure, cnt this last point number inserted

H represents the list head is inserted into a number x

D represents a k-th after deleting the number entered, if k is 0 to delete the list head

I represents the k-th input to insert a number x

#include <bits / STDC ++ H.>
 the using  namespace STD;
 struct List {
     int Val, Next; // Next, if the tail of the list shows that the arrival is 0 
} Ar [ 100050, China ];
 int main () {
    ios::sync_with_stdio(0);
    cin.tie ( 0 ); cout.tie ( 0 );
     int M, I, X, K, head = 0 , cnt = 0 ; // head indicates the current position of the head of the list, cnt indicates the number of an input 
    char S [ 2 ];
    cin>>M;
    for(i=1;i<=M;i++){
        cin>>s;
        if(s[0]=='H'){
            cin>>x;
            cnt++;
            ar[cnt].val=x;
            ar[cnt].next=head;
            head = CNT; // let the head of the list and then point to the old update list head 
        }
         the else  IF (S [ 0 ] == ' D ' ) {
            cin>>k;
            if(k)
                Ar [K] .next = Ar [Ar [K] .next] .next; // directly to the next to the next number a number of 
            the else 
                head = Ar [head] .next; // change the head of the list to the second list The number of 
        }
         the else  IF (S [ 0 ] == ' the I ' ) {
            cin>>k>>x;
            cnt++;
            ar[cnt].val=x;
            Ar [CNT] .next = Ar [k] .next; // to point to the copy number of the k- 
            Ar [k] = CNT .next; // let the k number of point to themselves 
        }
    }
    if(head)
        cout<<ar[head].val;
    while(ar[head].next){
        head=ar[head].next;
        cout<<' '<<ar[head].val;
    }
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/stelayuri/p/12339865.html