数据结构:有序顺序表里插入一个数,使得顺序表依然有序。
废话不多说,直接上程序 #include <stdio.h> #include <stdlib.h> #define MaxSize 20 typedef int ElemType; typedef struct { int length; ElemType data[MaxSize+1]; }SqList; //为了方便,顺序表的第一个位置不放值,也就是下标为0的 void CreateList(SqList *&L,ElemType a[],int n) { int i; for(i=1;i<=n;i++) L->data[i]=a[i]; L->length=n; } //初始化顺序表,向系统申请空间 void InitList(SqList *&L) { L=(SqList *)malloc(sizeof(SqList)); L->length =0; } //在有序表中加入一个数,使得表依然有序 bool InsertList(SqList *&L,ElemType e) { if(L->length==MaxSize) return false; int n=L->length+1; L->length++; while(n>=2 && edata[n-1]) L->data[n+1]=L->data[–n]; L->data[n]=e; return true; }