PTA 6-2 有序顺序表的插入

本文介绍了一种在递增有序顺序表中插入元素的方法,并考虑了数组扩容的情况。通过具体的C语言代码示例,展示了如何保持表内元素的递增顺序及如何处理超出表容量的边界情况。
6-2 有序顺序表的插入(10 分)

本题要求实现递增顺序表的有序插入函数。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   -2

typedef int  Status;

//顺序表的存储结构定义
#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10
typedef 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)
{
    int i,j;
    for(i=0; e>*(L.elem+i)&&i<L.length; i++);//遍历单链表
    for(j=L.length; j>i; j--)
    {
        *(L.elem+j)=*(L.elem+j-1);//元素往后移一个位置
    }
    *(L.elem+i)=e;
    L.length++;//增加链表长度
    return OK;
}

//扩容代码
 /*   ElemType *temp;
    if(L.length==L.listsize)
    {
        temp=(ElemType*)realloc(L.elem,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(ElemType));
        if(!temp)
            exit(OVERFLOW);
        L.elem=temp;
        L.listsize+=LISTINCREMENT;
    }*/





                
PTA 平台上 7 - 1 递增有序顺序表插入问题,可通过顺序表和链表两种不同存储结构来实现。 ### 顺序表实现 该问题旨在将一个新元素插入到递增有序顺序表中,同时保持顺序表有序性。输入包含顺序表的长度、递增有序顺序表元素以及要插入的元素,输出为插入新元素后的递增有序顺序表。 ```python # 读取输入 n = int(input()) arr = list(map(int, input().split())) x = int(input()) # 找到插入位置 index = 0 while index < n and arr[index] < x: index = index + 1 # 插入元素 arr.insert(index, x) # 输出结果 print(','.join(map(str, arr)) + ',') ``` ### 链表实现 通过链表实现该问题,需定义链表节点结构,创建链表,找到插入位置,插入新节点并输出链表元素。 ```c #include <stdio.h> #include <stdlib.h> typedef struct LNode { int data; struct LNode *next; } *List, LNode; // 创建链表 List CreateList(int n) { List L, tail; L = (LNode*)malloc(sizeof(LNode)); L->next = NULL; tail = L; int x; for (int i = 0; i < n; i++) { scanf("%d", &x); LNode *p = (LNode*)malloc(sizeof(LNode)); p->data = x; p->next = NULL; tail->next = p; tail = p; } return L; } // 插入元素 void Insert(List L, int a) { LNode *m = (LNode*)malloc(sizeof(LNode)); m->data = a; m->next = NULL; LNode *p = L->next, *q = L; while (p && p->data < a) { q = p; p = p->next; } m->next = p; q->next = m; } // 输出链表 void PrintList(List L) { LNode *p = L->next; while (p) { if (p->next == NULL) { printf("%d", p->data); } else { printf("%d ", p->data); } p = p->next; } } int main() { int n; scanf("%d", &n); List L = CreateList(n); int a; scanf("%d", &a); Insert(L, a); PrintList(L); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值