C++ 程序,将链表中的节点分开,使所有偶数节点都出现在链表的开头

问题描述

谁能给我解释一下这段代码。这运行得很好,但我不明白如何:(。 我通过为链表的节点创建结构来解决这个问题。然后是两个函数一个用于在链表的开头插入,一个用于显示链表。

能否解释一下void seggregate函数

#include <bits/stdc++.h>

using namespace std;

struct LLNode
{
    int data;
    struct LLNode* next;
};


void insertAtBeginning(struct LLNode** head,int dataToBeInserted)
{
    struct LLNode* current = new LLNode;
    current->data = dataToBeInserted;
    current->next = NULL;    
    if(*head == NULL)
            *head=current; 
        
    else
        {
            current->next=*head; 
            *head=current;
        }
        
}



void display(struct LLNode**node)
{
    struct LLNode *temp= *node;
    while(temp!=NULL)
        {
            if(temp->next!=NULL)
            cout<<temp->data<<" --> ";
            else
            cout<<temp->data;
            
            temp=temp->next; 
        }

    cout<<endl;
}



void Segregate(struct LLNode **head)
{
    struct LLNode *end = *head;
    struct LLNode *prevIoUs = NULL;
    struct LLNode *current = *head;
    while (end->next != NULL)
    {
        end = end->next;
    }
    struct LLNode *new_end = end;
    while(current->data % 2 != 0 && current != end)
    {
        new_end->next = current;
        current = current->next;
        new_end->next->next = NULL;
        new_end = new_end->next;
    }
 
    if(current->data%2 == 0)
    {
        *head = current;
        while(current!= end)
        {
            if((current->data)%2 == 0)
            {
                prevIoUs = current;
                current = current->next;
            }
            else
            {
                prevIoUs->next = current->next;
                current->next = NULL;
                new_end->next = current;
                new_end = current;
                current = prevIoUs->next;
            }
        }
    }
    else prevIoUs = current;
    if (new_end!=end && (end->data)%2 != 0)
    {
        prevIoUs->next = end->next;
        end->next = NULL;
        new_end->next = end;
    }
    return;
}

int main()
{
    struct LLNode* head = NULL;
    insertAtBeginning(&head,341);
    insertAtBeginning(&head,362);
    insertAtBeginning(&head,921);
    insertAtBeginning(&head,748);
    insertAtBeginning(&head,210);
    insertAtBeginning(&head,701);
    insertAtBeginning(&head,300);
    insertAtBeginning(&head,899);
    insertAtBeginning(&head,407);
    insertAtBeginning(&head,666);
    insertAtBeginning(&head,369);
    insertAtBeginning(&head,235);
    insertAtBeginning(&head,236);

   
    cout<<endl;
    cout<<endl;
    cout<<"The Input linked list is:    "<<endl;
    
    display(&head);
 
    Segregate(&head);
    
    cout<<endl;
    cout<<endl;
    cout<<"The Final Modified linked list in such a way that all the even nodes appear at the begining of the Linked List is:   "<<endl;
    
    display(&head);
    
    cout<<endl;
    cout<<endl;
 
    return 0;
}

解决方法

  1. while(current->data % 2 != 0 && current != end) 在这个 while 循环中,从第一个节点开始,所有奇数节点在末尾移动,直到到达末尾或找到偶数节点(偶数节点)。
  2. next if(current->data%2 == 0) else previous = current;处理直到最后一个节点的所有元素。在 while(current!= end) 中,所有奇数元素都移到了结尾。
  3. if (new_end!=end && (end->data)%2 != 0) 处理最后一个节点,如果是奇数,则移动到链表的末尾。

不得不说,代码可以大大简化。