【20年扬大真题】设顺序表va中的数据元素递增有序。试写一算法,将x插入到顺序表的适当位置上,以保障该表的有序性。

【20年扬大真题】

设顺序表va中的数据元素递增有序。

试写一算法,将x插入到顺序表的适当位置上,以保障该表的有序性。

c 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#define MaxSize 9//定义最大长度
int InitArr[10] = { 1,2,3,5,6,7,8,9,10 };

typedef struct {
	int data[MaxSize];//用静态的数据存放数据元素
	int length;//顺序表当前长度
}Sqlist;//顺序表的类型定义

//初始化一个顺序表
void InitList(Sqlist* L)
{
	for (int i = 0;i < MaxSize;i++)
	{
		L->data[i] = InitArr[i];//将所有数据元素设置为默认初始值
	}
	L->length = 9;//顺序表初始长度
}

void print(Sqlist* L)
{
	for (int i = 0;i < L->length;i++)
	{
		printf("%d ", L->data[i]);
	}
}

void InsertL(Sqlist* L,int x) {
	int i = 0;
	for (i = L->length-1;i >=0;i--)
	{
		if ((*L).data[i] > x) {
			(*L).data[i + 1] = (*L).data[i];
		}
		else {
			(*L).data[i + 1] = x;
			break;
		}
	}
}
int main()
{
	Sqlist L;
	InitList(&L);//初始化一个顺序表1 2 3 5 6 7 8 9 10
	
	printf("原始顺序表为:");
	print(&L);

	printf("\n");

	InsertL(&L, 4);
	printf("插入4后顺序表为:");
	print(&L);
}
相关推荐
caimouse6 分钟前
ReactOS 窗口系统分析(14):计时器/属性/加速键/热键 — timer.c + prop.c + accelerator.c + hotkey.c
c语言·开发语言·reactos
水饺编程14 分钟前
第5章,[Win32 章节] :贝塞尔样条曲线(一)
c语言·c++·windows·visual studio
Wang's Blog28 分钟前
PostgreSQL笔记49:向量检索核心算法、索引调优与过滤策略深度解析
笔记·算法·postgresql
疯狂打码的少年35 分钟前
【数据结构】交换类排序:冒泡与快速排序
数据结构·笔记·算法·排序算法
-凌凌漆-1 小时前
【C语言】结构体typedef struct与struct的区别
c语言·开发语言
Nil2081 小时前
leetcode 108有序数组转换为二叉搜索树
数据结构·算法·leetcode
高频因子挖掘机1 小时前
QuantDash 成交量单位统一实战:从“手”到“股”的跨市场量化数据清洗全流程
后端·算法·github
hn小菜鸡1 小时前
LeetCode 763、划分字母区间
数据结构·算法·leetcode
caimouse1 小时前
ReactOS 窗口系统分析(24):输入法 — ime.c
c语言·reactos
Escalating_xu1 小时前
【C++ STL简介】从六大组件到容器、迭代器与算法协作
java·c++·算法