本题要求实现一个函数,要求返回顺序表中小于指定元素值的个数,空表返回0。题目保证顺序表中所有元素都为正整数
//顺序表结构体定义
typedef int DataType;
typedef struct node {
DataType data[MAXSIZE];
int length;
}SeqList, * PSeqList;
您要实现的函数为:
int GetSmaller(PSeqList PL, DataType x);
函数GetSmaller返回顺序表L中比x小的元素的个数,若顺序表为空,则返回0
用到的函数有:
PSeqList Init_SeqList( );//创建一个顺序表
int Insert_SeqList( PSeqList PL, int i, DataType x);//在顺序表第i个元素之前插入元素x;
int Destroy_SeqList(&PL);//销毁顺序表
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 120
typedef int DataType;
//顺序表结构体定义
typedef struct node {
DataType data[MAXSIZE];
int length;
}SeqList, * PSeqList;
//函数申明区
PSeqList Init_SeqList();//创建一个空顺序表
int Destroy_SeqList(PSeqList *PL);//销毁顺序表
int Insert_SeqList( PSeqList PL, int i, DataType x);//在顺序表中插入
int GetSmaller(PSeqList PL, DataType x);//统计顺序表中小于x的个数
//主函数
int main( )
{
PSeqList PL;
DataType x;
int i=1,count;
PL = Init_SeqList();//创建一个空顺序表
scanf("%d",&x);
while((x!=-1)
{
Inset_SeqList(L,i,x); //在L的第i个元素位置上插入x
i++;
scanf("%d",&x);
}
scanf("\n%d",&x);
count=GetSmaller(PL,x);
printf("The number smaller than %d is %d.\n",x, count);
Destroy_SeqList(&PL);
return 0;
}
/* 请在这里填写答案 */
#输入格式: 输入数据有2行,第一行给出以-1结束的顺序表元素(不超过10000个,-1不属于顺序表元素),所有数据之间用空格分隔。题目保证输入的顺序元素都是整数。第二行给出要指定的元素值。
输入样例:
2 6 4 13 6 9 -1
6
输出样例:
The number smaller than 6 is 2.