| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 501 人关注过本帖
标题:这是我写的关于链表排序的问题,我想用的是冒泡排序法,但我不会求节点个数 ...
只看楼主 加入收藏
钟SX
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-28
结帖率:100%
收藏
已结贴  问题点数:18 回复次数:2 
这是我写的关于链表排序的问题,我想用的是冒泡排序法,但我不会求节点个数,就默认五个吧,但系统还是会停止工作 = =
#include<iostream.h>

struct student
{
    long int number;
    char *name;   
     int sex;
    int age;
    student* next;
};

student* head = NULL;
student* newhead = NULL;

void Create();
void sort();

void main()
{
    student* pS;
    Create( );
    cout<<"您输入的链表是:"<<endl;
    pS = head;
    for( ; pS; pS=pS->next )
    cout<<pS->number<<" "<<pS->name<<" "<<pS->sex<<" "<<pS->age<<endl;
    cout<<endl;
    sort();
    pS = newhead;
    for( ; pS; pS=pS->next )
    cout<<pS->number<<" "<<pS->name<<" "<<pS->sex<<" "<<pS->age<<endl;
    cout<<endl;

}

void Create()
{
    student* pS = NULL;
    student* pEnd = NULL;
    cout<<"请顺次输入学生的学号,姓名,性别和年龄:"<<endl;
    pEnd = pS = new student;
    pS->name = new char [10];  
    cin>>pS->number>>pS->name>>pS->sex>>pS->age;

    while( pS->number != 0 )
    {
        if( head == NULL )
        {
            head = pS;
        }
        else
            pEnd->next = pS;
        
        pEnd = pS;
        pS = new student;
         pS->name = new char [10];  
        cin>>pS->number>>pS->name>>pS->sex>>pS->age;
    }
    pEnd->next = NULL;
    delete pS;
}

void sort( )
{
    int i = 0 , pass , l = 0;
    student* pS = head;
    student* ptr = NULL;
    student* temp = NULL;
    for( pass=1; pass<5; pass++ )
    {
        pS = head + l;
        for( ; pS; pS = pS->next )
        {
            if( pS->number > pS->next->number )
            {
                temp = pS->next;
                pS->next = pS->next->next;
                if( ptr==NULL )
                {
                    temp->next = pS;
                    pS = temp;
                }
                else
                {
                    temp->next = pS;
                    ptr->next = temp;
                    pS = temp;
                }
                ptr = pS;
            }
        }
        l++;
    }
}
搜索更多相关主题的帖子: next include Create number 
2013-03-31 22:31
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:6 
建议你先写一个交换结点的函数,

思路就会清晰很多了


[fly]存在即是合理[/fly]
2013-03-31 22:50
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
收藏
得分:6 
太长了,懒得看

总有那身价贱的人给作业贴回复完整的代码
2013-03-31 22:51
快速回复:这是我写的关于链表排序的问题,我想用的是冒泡排序法,但我不会求节点 ...
数据加载中...
 
   



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

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