链表插入排序 [英] Linked list insertion sort

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

问题描述

我不编程的分拣部分非常先进的,所以我一直在寻找一些帮助,我的算法。

I'm not very advanced in the sorting part of programming yet, so I was looking for some help with my algorithm.

void sortList()
{
    Item_PTR tmpNxt = current->nextItem;
    Item_PTR tmpPTR = current;
    int a, tmp;

    while(tmpNxt != NULL)
    {   
        a = tmpPTR->value;
        while(tmpNxt != tmpPTR && tmpNxt->value < a)
        {
            tmp = a;
            tmpPTR->value = tmpNxt->value;
            tmpNxt->value = tmp;
            tmpPTR = tmpPTR->nextItem;
        }
        tmpPTR = current;   
        tmpNxt = tmpNxt->nextItem;
    }

}

该列表状态之前排序:9 8 7 6 5 4 3 2 1
排序后:1 9 8 7 6 5 4 3 2

The list state before sorting: 9 8 7 6 5 4 3 2 1 after sorting: 1 9 8 7 6 5 4 3 2

我不知道为什么...我玩电脑了很多文件,我觉得它应该工作...但也许其他的眼睛会发现这个问题。

I'm not sure why...I've played computer a lot on paper and I feel like it should work...but maybe other eyes will spot the problem.

电流是一个全球性的指针将始终具有列表中的第一/顶部元件的位置。

Current is a global pointer that will always have the location of the first/ top element in the list.

推荐答案

这是因为函数 sortlist中()不可以更改电流,全局
变量表示链表头。

This is because the function sortList() is not changing current, the "global" variable denoting the list head.

请不要使用全局变量,当然不是一个链表头。 (你会做什么,当你需要的两个的名单?)

Please don't use a global variable, and certainly not for a linked list head. (What will you do when you need two lists?)

我要重新设计 sortlist中()函数要么执行下列操作之一:

I would redesign the sortList() function to either one of the following:

/* sort the list pointed to by phead and return the new head after sorting */
Item_PTR sortList( Item_PTR phead );

/* sort the list pointed to by *pphead */
void sortList( Item_PTR * pphead );

此外,让自己熟悉的(即使你不能在眼前的项目中使用它们)到C ++标准库对列表界面,的std ::列表 < A HREF =htt​​p://www.cplusplus.com/reference/stl/list/相对=nofollow>链接

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆