zoukankan      html  css  js  c++  java
  • 顺序表

    #include<stdio.h>
    #include<stdlib.h>

    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INIT_SIZE 10
    #define INCREMENT_SIZE 10

    typedef int Elemtype;
    typedef int Status;
    typedef struct {
    Elemtype *base;
    int size;
    int length;
    }SqList;

    Status InitSqList(SqList *L)
    {
    L->base=(Elemtype *)malloc(INIT_SIZE*sizeof(Elemtype));
    if(!L->base)
    {
    printf("init_false: ");
    return ERROR;
    }
    L->size=INIT_SIZE;
    L->length=0;
    return OK;
    }
    Status InsertElem(SqList *L,int i,Elemtype e)
    {
    if(i<1||i>L->length+1)
    {
    return ERROR;
    }
    if(L->length>=L->size)
    {
    L->base=(Elemtype *)realloc(L->base,(L->size+INCREMENT_SIZE)*sizeof(Elemtype));
    if(!L->base)
    {
    printf("insert_fail ");
    return ERROR;
    }
    L->size+=INCREMENT_SIZE;
    }
    Elemtype *q=&L->base[L->length-1];
    Elemtype *p=&L->base[i-1];
    for(;q>=p;q--)
    {
    *(q+1)=*q;

    }
    *p=e;
    L->length++;
    return OK;
    }

    Status TraverseList(SqList *L)
    {
    int i;
    for(i=0;i<L->length;i++)
    printf("%d ",L->base[i]);
    }

    int main()
    { SqList L;
    InitSqList(&L);
    for(int i=0;i<10;i++)
    InsertElem(&L,i+1,i);
    TraverseList(&L);
    return 0;
    }

  • 相关阅读:
    纹理加载和异步
    地板上创建批量小方块
    创建自定义几何体(以立方体为例)
    WTForms
    angular和vue的差别
    vuejs简单介绍特点
    angularjs简单介绍和特点
    flask重要点
    redis
    DRF之认证组件源码解析
  • 原文地址:https://www.cnblogs.com/loveyan/p/4556597.html
Copyright © 2011-2022 走看看