-2 有序顺序表的插入(10 分)
2017-12-18 08:52
323 查看
本题要求实现递增顺序表的有序插入函数。L是一个递增的有序顺序表,函数Status ListInsert_SortedSq(SqList &L, ElemType e)用于向顺序表中按递增的顺序插入一个数据。 比如:原数据有:2 5,要插入一个元素3,那么插入后顺序表为2 3 5。 要考虑扩容的问题。
函数接口定义:
Status ListInsert_SortedSq(SqList &L, ElemType e);
裁判测试程序样例:
//库函数头文件包含#include<stdio.h>#include<malloc.h>#include<stdlib.h>//函数状态码定义#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int Status;//顺序表的存储结构定义#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef int ElemType; //假设线性表中的元素均为整型typedef struct{ElemType* elem; //存储空间基地址int length; //表中元素的个数int listsize; //表容量大小}SqList; //顺序表类型定义//函数声明Status ListInsert_SortedSq(SqList &L, ElemType e);//顺序表初始化函数Status InitList_Sq(SqList &L){//开辟一段空间L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));//检测开辟是否成功if(!L.elem){exit(OVERFLOW);}//赋值L.length = 0;L.listsize = LIST_INIT_SIZE;return OK;}//顺序表输出函数void ListPrint_Sq(SqList L){ElemType *p = L.elem;//遍历元素用的指针for(int i = 0; i < L.length; ++i){if(i == L.length - 1){printf("%d", *(p+i));}else{printf("%d ", *(p+i));}}}int main(){//声明一个顺序表SqList L;//初始化顺序表InitList_Sq(L);int number = 0;ElemType e;scanf("%d", &number);//插入数据的个数for(int i = 0; i < number; ++i){scanf("%d", &e);//输入数据ListInsert_SortedSq(L, e);}ListPrint_Sq(L);return 0;}/* 请在这里填写答案 */输入格式: 第一行输入接下来要插入的数字的个数 第二行输入数字 输出格式: 输出插入之后的数字
输入样例:
5 2 3 9 8 4
输出样例:
2 3 4 8 9
Status ListInsert_SortedSq(SqList &L, ElemType e){ ElemType *p; if(L.length >= L.listsize) { p = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType)); if(p == NULL) return ERROR; else { L.listsize += LISTINCREMENT; L.elem = p; } } int i; for(i = 0; i < L.length&& *(L.elem + i) <= e; i ++) { } int m, j; for(m = i, j = 0; m < L.length; m ++, j ++) { L.elem[L.length - j] = L.elem[L.length - 1 - j]; //*(L.elem + L.length - j) = *(L.elem + L.length - j- 1); } L.elem[i] = e; //*(L.elem + i) = e; L.length ++;}
相关文章推荐
- 6-2 有序顺序表的插入(10 分)
- 顺序表——有序顺序表的插入
- 在一个递增有序链表中插入新结点并保持链表递增顺序不变
- 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数(2,4,6,8,10,…),现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值
- 有序顺序表的插入
- 60}的C程序 高手帮忙写下`` 46 35 27 21 16 10 9 5 然后输出数组a。已知 int a[11]={1 使数组a仍然有序 将x插入到有序整型数组a中 由键盘输入一个整数x
- 数据结构 2-11设顺序表va中的数据元素递增有序。试写一算法,将x插入到顺序表的适当位置上,以保持该表的有序性。
- 10实现有序双向循环链表的插入操作
- Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。 Input 输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数
- 1.有序顺序表的插入
- 顺序表的创建,插入,删除,查找,逆序以及合并两个有序的顺序表
- 从键盘读入n个整数(升序),请编写算法实现: 建立带表头结点的单链表; 显示单链表,(形如:H->10->20->30->40); 在有序单链表中插入新的数据元素x; 将单链表就地逆
- 将x插入到顺序表中保持顺序表的递增有序
- 往一递增有序的顺序表中插入x,仍保持气有序性
- 把4,7,11,10,13,9按顺序插入形成平衡二叉树的高度是几
- 设顺序表a中的数据元素递增有序,试设计一个算法,将x插入到顺序表的适当位置,以保持该表的有序性。
- 有序顺序表的插入实现
- (顺序有序表)插入元素
- 6-2 有序顺序表的插入
- 五个数由大到小排列,插入一个数,顺序保持有序