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

问题描述

我试图通过修改不使用交换选择sort.i的data.i'm的链接进行排序链表不知道我要去的地方wrong.i'm初学者请帮助我。

 结构节点
{
    int数据;
    节点*链接;
};节点* P;无效的sort()
{
    节点*临时= P;
    节点*保存;
    节点* preV;
    节点* T preV;
    节点*推=新节点;    ŧpreV = NULL;
    为(;!TEMP = NULL; TEMP = TEMP-GT&;链接)
    {
        推= TEMP-GT&;联系;
        对于(;推= NULL;!=推推 - >链接)
        {
            如果(推挽>数据< TEMP-GT&;数据)
            {
                save->链接= TEMP-GT&;联系;
                TEMP-GT&;链接=推挽>链接;
                推挽>链接= save->链接;
                preV-GT&;链接=温度;
                ŧpreV-GT&;链接=推;
            }            preV =推;
        }        ŧpreV =温度;
    }
}


解决方案

 无效的sort(){
    节点*温度,*推;
    节点* T preV,* P preV;
    节点*保存;    ŧpreV = NULL;
    对于(TEMP = P;!TEMP = NULL; TEMP = TEMP-GT&;链接){
        p preV =温度;
        对于(推= TEMP-GT&;联系;!推= NULL;推=推挽>链接){
            如果(推挽>数据< TEMP-GT&;数据){
                节省= TEMP-GT&;联系;
                TEMP-GT&;链接=推挽>链接;
                如果(救==推)
                    推挽>链接=温度;
                其他
                    推挽>链接=保存;
                如果(P preV!= TEMP)
                    p preV-GT&;链接=温度;
                如果(T preV)
                    ŧpreV-GT&;链接=推;
                其他
                    P =推;
                节省=气温;
                TEMP =推;
                推=保存;
            }
            p preV =推;
        }
        ŧpreV =温度;
    }
}

i'm trying to sort the linked list by modifying the links not swapping the data.i'm using selection sort.i don't know where i am going wrong.i'm beginner please help me.

struct node
{
    int   data;
    node* link;
};

node* p;    

void sort()
{
    node* temp = p;
    node* save;
    node* prev;
    node* tprev;
    node* push = new node;

    tprev = NULL;
    for (; temp != NULL; temp = temp->link)
    {
        push = temp->link;
        for (; push != NULL; push = push->link)
        {
            if (push->data<temp->data)
            {
                save->link  = temp->link;
                temp->link  = push->link;
                push->link  = save->link;
                prev->link  = temp;
                tprev->link = push;
            }

            prev = push;
        }

        tprev = temp;
    }
}
解决方案
void sort(){
    node *temp, *push;
    node *tprev, *pprev;
    node *save;

    tprev = NULL;
    for (temp = p; temp != NULL; temp = temp->link){
        pprev = temp;
        for (push=temp->link; push != NULL; push = push->link){
            if (push->data < temp->data){
                save = temp->link;
                temp->link = push->link;
                if(save == push)
                    push->link = temp;
                else
                    push->link = save;
                if(pprev != temp)
                    pprev->link = temp;
                if(tprev)
                    tprev->link = push;
                else
                    p = push;
                save = temp;
                temp = push;
                push = save;
            }
            pprev = push;
        }
        tprev = temp;
    }
}

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

10-27 03:54