Doubly linked list (a)

Create a doubly linked list head node, data node

After you create a single list will be, and doubly linked list is also very easy to get, just in front of more than a pointer to a node.

Create the head node

#include <the iostream>
 the using  namespace STD;
 struct Node 
{ 
    int A;
     struct Node * Next;   // to point to the next node pointer 
    struct Node * pre;   // a pointer to the node 
};
 int main () 
{ 
    Node * head;   // points to the head node pointer 
    node * P; 
    P = new node;   // here replaced by new malloc, can be used to allocate memory space 
    p-> Next = NULL; // no data node, and thus are all NULL 
    p-> pre = NULL; 
    head = P;
     return(0);
}

There are two ways to create a data node, each time one is created behind the head node, another is created each time data following a previous node

The first look

void node_creat (Node * head, int n-) 
{ 
    Node * P;
     int I;
     for (I = . 1 ; I <= n-; I ++ ) 
    { 
        P = new new Node; 
        CIN >> p-> A; 
        P -> Next = head-> Next; 
        head -> Next = P;
         IF (p-> Next = NULL!)     // It should be determined whether the data following the header node of the node 
            p-> next-> = P pre; // if let its pre pointer to the newly inserted node 
        p-> = pre head; // pre newly inserted node to the head node
    } 
} 

// Since the new node is inserted is determined whether the time is behind the head node has a new data node
 // So why p-> next! = NULL instead head-> Next! = NULL
 // because the front head-> next it has been assigned to p-> next

The second

void node_creat (Node * head, int n-) 
{ 
    Node * P; 
    Node * Q;   // Q for each time point to the created data node 
    Q = head;
     int I;
     for (I = . 1 ; I <= n-; ++ I ) 
    { 
        P = new new Node; 
        CIN >> p-> A; 
        P -> = Q- Next> Next; 
        Q -> Next = P; 
        P -> pre = Q; 
        Q = Q-> Next; 
    } 
}

 

Guess you like

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