Doubly linked list (b)

Insert a new node doubly-linked list and delete nodes

About new node is inserted, the same method and the single doubly-linked list used, the only difference is required doubly-linked list pointer will refer to forward a data node is stored inside the exchange logical addresses

About delete nodes, a single list uses two pointers, a pointer to the need to remove one node, the other in front of a pointer to delete nodes, it can be done the entire list will be out, because there is a doubly linked list pointer is pointing to the front a node, and therefore you do not need to re-declare a pointer.

Insert node:

void node_insert (Node * head, int n-)   // n-used to indicate which data created after the node 
{ 
    Node * P, Q *;   // P to point to the node, q is used to create a new node 
    int L = 0 ; 
    P head- => Next;
     the while (P =! NULL) 
    { 
        L ++ ;
         IF (n-L ==)   // find the positions, achieve the creation of insertion 
        { 
            Q = new new Node; 
            CIN >> Q-> A; 
            Q - > = p-next> next;   // point to the next node address to exchange data, the not yet finished
            p-> Next = Q;
             IF (Q-> Next =! NULL) 
                Q -> next-> Q = pre;   // needs to exchange data points just before a node address 
            Q-> pre = P;
             BREAK ; 
        } 
        P p-=> Next; 
    } 
}

 

Delete node:

void node_delete (Node * head, int n-) 
{ 
    Node * P;
     int L = 0 ; 
    P = head-> Next;
     the while (P =! NULL) 
    { 
        L ++ ;
         IF (L == n-) 
        { 
            P -> pre-> = p-next> next;   // find the next pointer before deleting a node of the 
            p-> next-> p-pre => pre; // find the delete a node pointer pre 
            delete P;
             BREAK ; 
        } 
        P = p-> Next; 
    }
}

 

Guess you like

Origin www.cnblogs.com/hzb1224/p/11407460.html