• 线性表


    #include<stdio.h>
    #include<stdlib.h>
    #define LIST_INIT_SIZE 100
    #define LISTINCREMEMT 10
    #define OK 1
    #define OVERFLOW -2
    #define ERROR -1
    typedef  int ElemType;
    typedef  int Status;
    typedef struct
    {
        ElemType *elem;
        int length;
        int listsize;
    } Sqlist;
    //**********构造空的线性表
    void 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;
    
    }
    //**********销毁线性表
    void  DestroyList(Sqlist &L)
    {
        if(L.length>0)free(L.elem);
    }
    //**********置空线性表
    void  ClearList(Sqlist &L)
    {
        if(L.length>0)L.length=0;
    }
    //判断线性表是否空
    Status  IsEmpty(Sqlist &L)
    {
        if(L.length>0)return 1;
        else return 0;
    }
    //取表长
    Status GetLength(Sqlist &L)
    {
        return L.length;
    }
    //返回指定位置元素值
    Status GetElem (Sqlist &L,int i)
    {
        return L.elem[i-1];
    }
    //**********值查找
    Status SearchElem_Val(Sqlist L,ElemType e)
    {
        ElemType *p=L.elem;
        int i=1;
        while(i<=L.length&&(*(p++)!=e))i++;
        if(i<=L.length)return i;
        else return 0;
    }
    //**********定位插入
    Status ListInsert(Sqlist &L,int i,ElemType e)
    {
        if(i<1||i+1>L.length+1)return ERROR;
        if(L.length>L.listsize)
        {
            ElemType  *newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMEMT)*sizeof(ElemType));
            if(!newbase)exit(OVERFLOW);
            L.elem=newbase;
            L.listsize+=LISTINCREMEMT;
        }
        ElemType *q=&(L.elem[i-1]),*p=&(L.elem[L.length-1]);
        for(; p>=q; p--)
            *(p+1)=*p;
        *q=e;
        ++L.length;
        return OK;
    }
    //**********定位删除
    Status ListDelete_loc(Sqlist &L,int i)
    {
        if(i<1||i>L.length)return ERROR;
        ElemType *p=&(L.elem[i-1]);
        ElemType *q=L.elem+L.length-1;
        for( ++p; p<=q; ++p)
            *(p-1)=*p;
        --L.length;
        return OK;
    }
    //定值删除
    void ListDelete_val(Sqlist &L,ElemType e)
    {
        ElemType *tmp,*p=L.elem,*q=L.elem+L.length-1;
        for(; p<=q; p++)
        {
            if(*p==e)
            {   L.length--;
                for( tmp=p; tmp<=q; tmp++)
                    *tmp=*(tmp+1);
            }
        }
    
    }
    //冒泡排序
    void Maopao(Sqlist &L)
    {
        int tmp,i,j,len=GetLength(L);
        for(int i=0;i<len;i++)
        {
            for(int j=i+1;j<len;j++)
            {
                if(L.elem[j]<L.elem[i])
                {
                    tmp=L.elem[j];
                    L.elem[j]=L.elem[i];
                    L.elem[i]=tmp;
                }
            }
        }
    
    }
    void Print(Sqlist L)
    {
        printf("
    ****打印线性表****
    ");
        for(int i=0; i<L.length; i++)
        {
            if(i==0)printf("%d",L.elem[i]);
            else  printf(" %d",L.elem[i]);
    
        }
        printf("
    ");
    }
    int main()
    {
    
        Sqlist L;
        InitLIst_Sq(L);
        while(~scanf("%d",&L.length))
        {
            for(int i=0; i<L.length; i++)
            {
                scanf("%d",&L.elem[i]);
            }
            Print(L);
            printf("
    ****排序****
    ");
            Maopao(L);
            Print(L);
            printf("
    ****判空线性表****
    ");
            if(IsEmpty(L))printf("非空
    ");
            else printf("空表
    ");
            printf("
    ****取表长****
    ");
            printf("%d
    ",GetLength(L));
            printf("
    ****定位元素****
    输入位置
    ");
            int num,num2;
            scanf("%d",&num);
            if(GetElem(L,num))
            {
                printf("%d
    ",GetElem(L,num));
            }
            printf("
    ****按值查找位置****
    输入值
    ");
            scanf("%d",&num);
            if(SearchElem_Val(L,num))
            {
                printf("%d
    ",SearchElem_Val(L,num));
            }
            printf("
    ****定位插入元素****
    输入位置和插入的值
    ");
            scanf("%d%d",&num,&num2);
            if(ListInsert(L,num,num2)==1)
            {
                Print(L);
            }
            printf("
    ****取表长****
    ");
            printf("%d
    ",GetLength(L));
            printf("
    ****定位删除元素****
    输入删除位置
    ");
            scanf("%d",&num);
            if(ListDelete_loc(L,num)==1)
            {
                Print(L);
            }
            printf("
    ****定值删除元素****
    输入值
    ");
            scanf("%d",&num);
            ListDelete_val(L,num);
            Print(L);
             printf("
    ****取表长****
    ");
            printf("%d
    ",GetLength(L));
    }
    }
  • 相关阅读:
    js中return 、return false 、return true、break、continue区别
    小程序mpvue下多层次的数据值改变但是页面不动态渲染
    Thinkpad T470p Clover and opencore 吃黑苹果 Catalina 10.15.5
    3dTiles 数据规范详解[4.5] *一个被废弃的非正式瓦片规范
    3dTiles 数据规范详解[4.4] cmpt瓦片二进制数据文件结构
    3dTiles 数据规范详解[4.3] pnts瓦片二进制数据文件结构
    3dTiles 数据规范详解[4.2] i3dm瓦片二进制数据文件结构
    记一次Windows MinGW g++编译c++代码
    3dTiles 数据规范详解[4.1] b3dm瓦片二进制数据文件结构
    3dTiles 数据规范详解[3] 内嵌在瓦片文件中的两大数据表
  • 原文地址:https://www.cnblogs.com/sxy-798013203/p/6044375.html
Copyright © 2020-2023  润新知