数据结构之顺序表的合并

写一个程序完成以下功能:

(1)  一条记录有学号和成绩两个数据项,依次输入数据(学号,成绩): (5,60),(6,80),(7,76),(8,50),建立一个顺序表,并写一个插入函数,把以上数据写到主函数中依次调用插入函数。要求:先输入的数据排在前面,不要按学号或成绩从大到小排序。

(2)  依次输出表中所有数据。

(3)  写一个函数对表中所有数据按照成绩从大到小进行排序。

(4)  再次输出所有数据。

(5)  建立另外一个顺序表,依次输入数据:(10,70),(20,85),(30,75), (40,90),并且按照(3)写好的函数对该顺序表进行排序。

(6)  写一个函数合并以上两个有序表,使得合并之后的表是有序的(从大到小)。即实现函数 merge(SqList *A, SqList *B, SqList *C),把A,B两个有序表合并在表C中去。并且最好不用二重循环实现算法,也不用重新调用排序函数。

  

#include "stdio.h"
#define MAXSIZE 100
typedef struct
{
    int NO;
    int score;
}ElemType;
typedef struct 
{
    ElemType elem[MAXSIZE];
    int length;
}SqList;

//初始化
void InitList(SqList *pL)
{
    pL->length =0;
}

//插入
void InsertList(SqList *pL,ElemType e,int i)
{
    if(pL->length >MAXSIZE)
        return;
    pL->elem [i]=e;
    pL->length ++;
}

//排序
void  SortScore(SqList *pL)
{
    for(int i=0;i<pL->length ;i++)
        for(int j=i+1;j<pL->length ;j++)
        {
            if(pL->elem [i].score<pL->elem [j].score )
            {
                ElemType temp=pL->elem [i];
                pL->elem [i]=pL->elem [j];
                pL->elem [j]=temp;
            }
        }
}


void Merge(SqList *pL,SqList *pS,SqList *T)
{
    int i=0,j=0,k=0;
    while(i<pL->length &&j<pS->length )
    {
        if(pL->elem [i].score >pS->elem [j].score )
            T->elem [k++]=pL->elem [i++];
            
        else
    
            T->elem [k++]=pS->elem [j++];
    
    }
    while(i<pL->length )
        T->elem [k ++]=pL->elem [i++ ];
    
    while(j<pS->length )
        T->elem [k ++]=pS->elem [j ++];

    
}

//输出
void Printf(SqList *pL)
{
    for(int i=0;i<pL->length ;i++)
        printf("(%2d,%2d)\n",pL->elem [i].NO ,pL->elem [i].score );
}


void main()
{
    SqList L,S,T;
    ElemType e;
    InitList(&L);
    e.NO =5; e.score =60;
    InsertList(&L,e,0);
    e.NO =6; e.score =80;
    InsertList(&L,e,1);
    e.NO =7; e.score =76;
    InsertList(&L,e,2);
    e.NO =8; e.score =50;
    InsertList(&L,e,3);
    printf("顺序表L:\n");
    Printf(&L);
    printf("\n按照成绩大小排序后的顺序表L:\n");
    SortScore(&L);
    Printf(&L);

    InitList(&S);
    e.NO =10; e.score =70;
    InsertList(&S,e,0);
    e.NO =20; e.score =85;
    InsertList(&S,e,1);
    e.NO =30; e.score =75;
    InsertList(&S,e,2);
    e.NO =40; e.score =90;
    InsertList(&S,e,3);
    printf("\n顺序表S:\n");
    Printf(&S);
    printf("\n按照成绩大小排序后的顺序表S:\n");
    SortScore(&S);
    Printf(&S);

    printf("\n\n");

    InitList(&T);
    T.length =L.length +S.length ;
    Merge(&L,&S,&T);
    Printf(&T);
    

}

    

 


 

转载于:https://www.cnblogs.com/best1/p/6133514.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值