zoukankan      html  css  js  c++  java
  • 数据结构入门题1

    题目:

    线性表存放在向量A[arrsize]的前elenum个分量中,且递增有序。将x插入到线性表的适当位置上,以保持线性表的有序性。

    其中arrsize表示数组A的大小。elenum代表元素个数。

    输入输出样例:1组

    样例输入:
        7 //代表arrsize大小
        1 2 3 4 5 6 7 //输入A向量中的值
        5 //elenum的值
        6 //插入元素x的值

    样例输出:
        1 2 3 4 5 6 6 7

    摘要:

    顺序表存储,插入函数,选择排序。

    源代码:

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 1024
    typedef struct seqlist
    {
        int data[MAXSIZE];
        int last;
    }SeqList;
    
    SeqList *init_SeqList()
    {
        SeqList *L;
        L=(SeqList *)malloc(sizeof(SeqList));
        L->last=-1;
        return L;
    }
    
    void Insert(SeqList *L,int ele,int dat)
    {
        int i,j,temp;
        if(ele>=L->last)
            printf("error");
        else
        {
            for(j=L->last;j>ele-1;j--)
            L->data[j+1]=L->data[j];
            L->data[j+1]=dat;
            L->last++;
        }
        for(i=L->last;i>0;i--)
        {
            if(L->data[i] < L->data[i-1])
            {
                temp=L->data[i];
                L->data[i]=L->data[i-1];
                L->data[i-1]=temp;
            }
        }
    }
    
    void Display(SeqList *L,int n)
    {
        int i;
        for(i=0;i<n;i++)
            printf("%d ",L->data[i]);
    }
    
    int main()
    {
        SeqList *A;
        int x,elenum;
        int i,n,arrsize;
        A=init_SeqList();
        scanf("%d",&arrsize);
        for(i=0;i<arrsize;i++)
        {
            scanf("%d",&A->data[i]);
            A->last++;
        }
        scanf("%d",&elenum);
        scanf("%d",&x);
        Insert(A,elenum-1,x);
        printf("
    ");
        Display(A,A->last+1);
        return 0;
    }

    测试:

    样例输入:
        5 //代表arrsize大小
        3 4 5 6 7 //输入A向量中的值
        4 //elenum的值
        2 //插入元素x的值

    样例输出:
        2 3 4 5 6 7

    样例输入:
        10 //代表arrsize大小
        2 3 4 6 7 8 9 10 21 22//输入A向量中的值
        8 //elenum的值
        5 //插入元素x的值

    样例输出:
        2 3 4 6 7 8 9 10 21 22

    总结:

    博主有点懒,但争取日更好吧

  • 相关阅读:
    C#下对象与JSON串互相转换
    靠纯技术是否能渡过中年危机
    个人小结
    Qt:Drag-Drop操作在QGraphicsView及Model/View框架下的实现
    Lex&Yacc Parser错误发生后再次parser之前恢复初始状态
    lex中yyrestart()的使用
    go特性-数组与切片
    go特性-defer
    golang实现mysql udf
    go创建动态库
  • 原文地址:https://www.cnblogs.com/Andre/p/11913741.html
Copyright © 2011-2022 走看看