Data Structures and Algorithms -------- list

Single list

Linked list node

typedef struct Node{
    int data;       //数据域
    Node* next;     //指针域,指向下一个结点
};

List of print

Notes:

  1. data is not stored in the list head.
  2. next the last element of the list points to NULL.
void PrintLinkList(Node* head){
    Node* cur = head->next;
    while(cur != NULL){
        cout<<cur->data<<"  ";
        cur = cur->next;
    }
    cout<<endl;     
}

Interpolation head of the list

Notes:

  1. Head of the list becomes the end of the list, and the tail of the list is NULL point, and thus put up on a head of the list to null.
  2. First list generated interpolation method head-> data is data, it is possible to generate a new_head, such new_head-> head. So that will not head node data field data and embarrassed.
Node* HeadInsert(Node* head,int arr[],int arrSize){
    head = NULL;                    //Notes:1
    for(int i = 0;i < arrSize;i++){
        Node* new_node = new Node;
        new_node->data = arr[i];
        new_node->next = head;      //Notes:1
        head = new_node;
    }
    Node* new_head = new Node;      //Notes:2
    new_head->next = head;
    return new_head;
}

List tail interpolation

Notes:

  1. Position requires a team of tail pointer rear end of the recording team, each time a new element is added must be updated rear.

  2. Each new_node-> next point to be NULL.

Node* RearInsert(Node head,int arr[],int arrSize){
    Node* rear = head;
    for(int i = 0;i < arrSize;i++){
        Node* new_node = new Node;
        new_node->data = arr[i];
        new_node->next = NULL;
        rear->next = new_node;
        rear = rear->next;
    }
    return head;
}

List in reverse order (non-recursive)

Notes:

  1. Before the first reverse list of elements is determined, no operation (head == NULL or head-> next == NULL) return it directly.
  2. Just generated p1, p2, after p3, p1 would be the end node after the reverse, it is necessary p1-> next = NULL.
  3. p1, p2, p3 are the head after the first, second, third node, after the entire list completely reverse the head node can not have the data, it is necessary head-> next = p2, so unified.
Node* ReverseLinkList(Node* head){
    if(head == NULL || head->next == NULL){
        return head;
    }
    Node* p1 = head->next;
    Node* p2 = p1->next;
    Node* p3 = p2->next;
    p1->next = NULL;        //头变尾
    while(p3 != NULL){
        p2->next - p1;
        p1 = p2;
        p2 = p3;
        p3 = p3->next;
    }
    p2->next = p1;
    head->next = p2;        //尾变头
    return head;
}

List in reverse order (recursive)

Notes:

  1. Is returned by the recursive method pn -> ......-> p2-> p1-> head-> NULL. So the way it should be careful when using
Node* ReverseLinkListRecursive(Node* head){
    if(head == NULL || head->next == NULL){
        return head;
    }
    Node* second = head->next;
    Node* new_head = ReverseLinkListRecursive(second);
    second->next = head;
    head->next = NULL;
    return new_head;
}

Merging sorted linked list

Notes:

  1. It must be the same in the two sorted lists direction
  2. If a list is empty, return directly to another list
  3. Normally, the first node is no data at this time to the head need not be determined in advance which of the nodes in the linked list. If the first node contains the data will need to identify a new head of the list in which of the linked list.
  4. To some examples of the new head of the list, or the list when determining which node is the first element to be wrong (new_head-> next = p1).
  5. The node is empty by determining whether there are still two elements of the list, the last element pointer if the next node is determined to be missing cycle through the piece of list
  6. If the loop has a first linked list, the linked list pointer to other remaining node to the first.
Node* MergeLinkList(Node* head1,Node* head2){
    Node *p1,*p2,*pcur;
    Node *new_head = new Node;  //一定要开辟空间
    if(head1->next == NULL){
        return head2;
    }
    if(head2->next == NULL){
        return head1;
    }
    if(head1->next == NULL && head2->next == NULL){
        new_head = NULL;
        return new_head;
    }
    //确定新链表的头结点
    p1 = head1->next;
    p2 = head2->next;
    /*
    if(p1->data <= p2->data){       //这一步是可以省的
        new_head->next = p1;
        p1 = p1->next;
    }
    else{
        new_head->next = p2;
        p2 = p2->next;
    }
    pcur = new_head->next;
    */
    pcur = new_head;
    while(p1 != NULL && p2 !=NULL){
        if(p1->data <= p2->data){
            pcur->next = p1;
            pcur = pcur->next;
            p1 = p1->next;
        }
        else{
            pcur->next = p2;
            pcur = pcur->next;
            p2 = p2->next;
        }
    }
    if(p1){
        pcur->next = p1;
    }
    else{
        pcur->next = p2;
    }
    return new_head;
}

Guess you like

Origin www.cnblogs.com/ziyuemeng/p/12370249.html