码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之顺序表的合并

时间:2016-12-05 14:15:26      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:++   int   程序   amp   efi   code   str   else   成绩   

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

(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);
    

}

    

 


 

数据结构之顺序表的合并

标签:++   int   程序   amp   efi   code   str   else   成绩   

原文地址:http://www.cnblogs.com/best1/p/6133514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!