习题1.9 有序数组的插入 (20分)
标签:efi func 插入 key 裁判 位置 comm insert icon
本题要求将任一给定元素插入从大到小排好序的数组中合适的位置,以保持结果依然有序。
bool Insert( List L, ElementType X );
其中List结构定义如下:
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
L是用户传入的一个线性表,其中ElementType元素可以通过>、=、<进行比较,并且题目保证传入的数据是递减有序的。函数Insert要将X插入Data[]中合适的位置,以保持结果依然有序(注意:元素从下标0开始存储)。但如果X已经在Data[]中了,就不要插入,返回失败的标记false;如果插入成功,则返回true。另外,因为Data[]中最多只能存MAXSIZE个元素,所以如果插入新元素之前已经满了,也不要插入,而是返回失败的标记false。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List ReadInput(); /* 裁判实现,细节不表。元素从下标0开始存储 */
void PrintList( List L ); /* 裁判实现,细节不表 */
bool Insert( List L, ElementType X );
int main()
{
List L;
ElementType X;
L = ReadInput();
scanf("%d", &X);
if ( Insert( L, X ) == false )
printf("Insertion failed.\n");
PrintList( L );
return 0;
}
/* 你的代码将被嵌在这里 */
5
35 12 8 7 3
10
35 12 10 8 7 3
Last = 5
6
35 12 10 8 7 3
8
Insertion failed.
35 12 10 8 7 3
Last = 5
AC代码如下:
bool Insert(List L, ElementType X)
{
int i=0, tag;
while(L->Data[i]>X)//数组data[]是递减排序的,此处是找x应该插入的位置,下标从0开始。第一个比x小的元素的下标就是这个位置
{
i++;
}
tag = i;
if(L->Data[tag]==X||(L->Last+1)>=MAXSIZE)//如果x已经在data[]中或者data[]中元素数量超过10
return false;
for(i= L->Last;i>=tag;i--)//元素后移
{
L->Data[i+1] = L->Data[i];
}
L->Data[tag] = X;//将X放入第tag位
L->Last++;
return true;
}
标签:efi func 插入 key 裁判 位置 comm insert icon
原文地址:https://www.cnblogs.com/qinmin/p/12261801.html