| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:请教:单链表的排序
只看楼主 加入收藏
Alen0128
Rank: 4
等 级:业余侠客
帖 子:171
专家分:222
注 册:2009-12-26
结帖率:76.19%
收藏
已结贴  问题点数:10 回复次数:2 
请教:单链表的排序
编了一个类似于插入法的排序,我的思路是:一边建立链表,一边进行插入排序;但总出错,调试了几次,发现可能是for循环出错了(红字标出),请教各位高手帮忙看看!

#include <stdio.h>
#include <stdlib.h>

void Printflink(struct Node* head);     //输出链表

#define TRUE 1

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

int main()
{
    int iData = 0;
    struct Node* head = NULL;
    struct Node* pCurrent = NULL;
    struct Node* pNewNode = NULL;
    struct Node* pForward = NULL;

    while (TRUE)
    {
        scanf("%d",&iData);

        if (iData == 0)
        {
            break;
        }

        pNewNode = malloc(sizeof(struct Node));
        pNewNode->data = iData;
        pNewNode->next = NULL;

        if (head == NULL)                     //链表为空
        {
            head = pNewNode;
            continue;
        }

        if (pNewNode->data <= head->data)      //表头交换
        {
            pNewNode->next = head;
            head = pNewNode;
            continue;
        }

        for (pCurrent=head->next,pForward=head; pCurrent!=NULL; pCurrent=pCurrent->next,pForward=pForward->next)
        {
            if (pNewNode->data < pCurrent->data)
            {
                pForward->next = pNewNode;
                pNewNode->next = pCurrent;
                break;
            }
        }

        if (pCurrent == NULL)
        {
            pCurrent = pNewNode;
        }   
    }

    Printflink(head);
        
    return 0;
}

void Printflink(struct Node* head)   
{
    struct Node* pTemp = NULL;
   
    if (head == NULL)
    {
        printf("Link is empty!\n");

        return;
    }
    else
    {
        for (pTemp=head; pTemp!=NULL; pTemp=pTemp->next)
        {
            printf("%d\n",pTemp->data);
        }
    }
}
搜索更多相关主题的帖子: 单链 
2010-02-10 09:02
heartnheart
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:335
专家分:1096
注 册:2009-7-10
收藏
得分:10 
for循环没错,for后面的if错了
程序代码:
#include <stdio.h>
#include <stdlib.h>

void Printflink(struct Node* head);     //输出链表       这一句最好放在结构声明之后

#define TRUE 1

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

int main()
{
    int iData = 0;
    struct Node* head = NULL;
    struct Node* pCurrent = NULL;
    struct Node* pNewNode = NULL;
    struct Node* pForward = NULL;

    while (TRUE)
    {
        scanf("%d",&iData);

        if (iData == 0)
        {
            break;
        }

        pNewNode = malloc(sizeof(struct Node));
        pNewNode->data = iData;
        pNewNode->next = NULL;

        if (head == NULL)                     //链表为空
        {
            head = pNewNode;
            continue;
        }

        if (pNewNode->data <= head->data)      //表头交换
        {
            pNewNode->next = head;
            head = pNewNode;
            continue;
        }

        for (pCurrent=head->next,pForward=head; pCurrent!=NULL; pCurrent=pCurrent->next,pForward=pForward->next)
        {
            if (pNewNode->data < pCurrent->data)
            {
                pForward->next = pNewNode;
                pNewNode->next = pCurrent;
                break;
            }
        }

        if (pCurrent == NULL)
        {
           pForward -> next= pNewNode;//把这改了就行了
        }    
    }

    Printflink(head);
        
    return 0;
}

void Printflink(struct Node* head)   
{
    struct Node* pTemp = NULL;
    
    if (head == NULL)
    {
        printf("Link is empty!\n");

        return;
    }
    else
    {
        for (pTemp=head; pTemp!=NULL; pTemp=pTemp->next)
        {
            printf("%d\n",pTemp->data);
        }
    }
}


[ 本帖最后由 heartnheart 于 2010-2-10 10:29 编辑 ]
2010-02-10 10:27
Alen0128
Rank: 4
等 级:业余侠客
帖 子:171
专家分:222
注 册:2009-12-26
收藏
得分:0 
o了

-不想让你发现我 凌乱的脚步 ,我努力 跟上你的速度
2010-02-10 11:18
快速回复:请教:单链表的排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026750 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved