6-4 顺序表的有序插入操作 (10 分)
本文最后更新于1811天前,其中的信息可能已经有所发展或是发生改变。

本题要求实现一个函数,要求将指定元素插入到有序表的合适位置,使得插入后仍然保持有序,若插入失败返回0;插入成功则返回1,并且顺序表的长度加1.

函数接口定义:

int SqInsert(SqList &L,ElemType e);

其中SqList结构定义如下:

typedef struct{
	ElemType *elem;
	int length;
 }SqList;
 

裁判测试程序样例:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
}SqList;
void InitList(SqList &L);/*函数的实现此处不再显示*/
int SqInsert(SqList &L,ElemType e);
int main()
{
	SqList L;
	InitList(L);
	ElemType e;
	scanf("%d",&e);
	int result=SqInsert(L,e);
	if(result==0){
		printf("Insertion Error.The storage space is full!");	
	}else if(result==1){
		printf("Insertion Success.The elements of the SequenceList L are:");	
		for(int j=0;j<L.length;j++){
			printf(" %d",L.elem[j]);
		}
	}
	return 0;
}

/* 请在这里填写答案 */

输入格式:

输入数据有1行,首先给出以-1结束的非递减顺序表元素值(不超过100个,-1不属于顺序表元素,),然后是被插入元素值。所有数据之间用空格分隔。

输入样例:

4 8 20 -1 10 

输出样例:

Insertion Success.The elements of the SequenceList L are: 4 8 10 20
int SqInsert(SqList &L,ElemType e)
{
    int temp = L.length;
    if(L.length==MAXSIZE) return 0;
    for(int i=L.length-1;i>=0&&L.elem[i]>e;i--)
    {
        L.elem[i+1] = L.elem[i];
        temp = i;
    }
    L.elem[temp] = e;
    L.length++;
    return 1;
}

 

点击数:1449

    评论

    1. mondayice
      博主
      Windows Chrome 73.0.3683.86
      5年前
      2019-5-11 6:15:13

      ...

    发送评论 编辑评论

    
    				
    |´・ω・)ノ
    ヾ(≧∇≦*)ゝ
    (☆ω☆)
    (╯‵□′)╯︵┴─┴
     ̄﹃ ̄
    (/ω\)
    ∠( ᐛ 」∠)_
    (๑•̀ㅁ•́ฅ)
    →_→
    ୧(๑•̀⌄•́๑)૭
    ٩(ˊᗜˋ*)و
    (ノ°ο°)ノ
    (´இ皿இ`)
    ⌇●﹏●⌇
    (ฅ´ω`ฅ)
    (╯°A°)╯︵○○○
    φ( ̄∇ ̄o)
    ヾ(´・ ・`。)ノ"
    ( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
    (ó﹏ò。)
    Σ(っ °Д °;)っ
    ( ,,´・ω・)ノ"(´っω・`。)
    ╮(╯▽╰)╭
    o(*////▽////*)q
    >﹏<
    ( ๑´•ω•) "(ㆆᴗㆆ)
    😂
    😀
    😅
    😊
    🙂
    🙃
    😌
    😍
    😘
    😜
    😝
    😏
    😒
    🙄
    😳
    😡
    😔
    😫
    😱
    😭
    💩
    👻
    🙌
    🖕
    👍
    👫
    👬
    👭
    🌚
    🌝
    🙈
    💊
    😶
    🙏
    🍦
    🍉
    😣
    Source: github.com/k4yt3x/flowerhd
    颜文字
    Emoji
    小恐龙
    花!
    上一篇
    下一篇