首页 > 解决方案 > C++ 程序,它以这样一种方式分隔链表中的节点,使得所有偶数节点都出现在链表的开头

问题描述

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

可以请解释无效的隔离功能。

#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;
}

标签: c++data-structureslinked-list

解决方案


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

必须说,代码可以大大简化。


推荐阅读