本文介绍了冒泡排序双向链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为双向链表我冒泡排序功能的问题。
它工作时,我整理了单链接的方式在节点(仅带 - >下一个),但我不能使它与合作 - > $ P $光伏指针。
这里是code我使用的是:

 无效排序(诠释计数)
{
    结构数据* TMP,*目前,* nextone;
    INT I,J;
    对于(i = 0; I<计数;我++)
    {
        电流=第一;
        为(J = 0; J<计数-1-I; J ++)
        {
            如果(电流 - >若干>&CURRENT- GT;下一步 - >号)
            {
                nextone =电流 - >接下来,
                电流 - >接下来= nextone->接下来,
                nextone->接下来=电流;
                如果(当前==第一)
                {
                    第一= nextone;
                    电流= nextone;
                }
                其他
                {
                    电流= nextone;
                    tmp->接下来= nextone;
                }
            }
            TMP =电流;
            电流=电流 - >接着,
        }
    }
}

和这是我使用的结构(与用于列表的第一个和最后一个元件的全局变量):

 结构数据
{
    INT ID;
    焦炭名[20];
    INT编号;
    结构数据*接下来的;
    结构数据* preV;
};结构数据*第一= NULL;
结构数据*最后= NULL;


解决方案

下面的逻辑会的工作。

我将遵循类似的算法...如果你想移动整个节点...

 结构数据*前,后的*;
如果(电流 - >若干>&CURRENT- GT;下一步 - >号)
{
    之前=电流 - > preV;
    后=电流 - >接下来,
    如果(前!= NULL){
        直至─>接下来经过=;
    }
    电流 - >接下来=离职后>接下来,
    电流 - > preV =后;
    离职后>接下来=电流;
    离职后> previous =前;
}

另外,你可以简单地在交换节点的数字而没有移动整个节点,如果数据的排序是目的。您可以展开下面的逻辑来包括字符数组和id的交换也是如此。

 如果(电流 - >若干>&CURRENT- GT;下一步 - >号)
{
    INT tempNum =电流 - >数;
    电流 - >数=电流 - >下一步 - >数;
    电流 - >下一步 - >数= tempNum;
}

I have a problem with my bubble-sorting function for the doubly linked list.It is working when I'm sorting the nodes in the singly linked way (only with ->next), but I can't make it work with ->prev pointers.Here is the code I'm using:

void sort(int count)
{
    struct data *tmp,*current,*nextone;
    int i,j;
    for(i=0;i<count;i++)
    {
        current = first;
        for(j=0;j<count-1-i;j++ )
        {
            if(current->number > current->next->number)
            {
                nextone = current->next;
                current->next = nextone->next;
                nextone->next = current;
                if(current == first)
                {
                    first = nextone;
                    current = nextone;
                }
                else
                {
                    current = nextone;
                    tmp->next = nextone;
                }
            }
            tmp = current;
            current = current->next;
        }
    }
}

And this is the structure I'm using (with the global variables for the first and last element of the list):

struct data
{
    int id;
    char name[20];
    int number;
    struct data *next;
    struct data *prev;
};

struct data *first = NULL;
struct data *last = NULL;
解决方案

Below logic would work.

I would follow similar algorithm... If you want to move the entire nodes...

struct data *before, *after;
if(current->number > current->next->number)
{
    before = current->prev;
    after = current->next;
    if(before != NULL){
        before->next = after;
    }
    current->next = after->next;
    current->prev = after;
    after->next = current;
    after->previous = before;
}

Alternatively, you can simply swap the numbers in the nodes without bothering to move entire nodes, if sorting of the data is the purpose. You can expand the below logic to include swapping of both char array and id as well.

if(current->number > current->next->number)
{
    int tempNum = current->number;
    current->number = current->next->number;
    current->next->number = tempNum;
}

这篇关于冒泡排序双向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:17