1. //用来定义数据元素
    2. #define MAX_SIZE 255
    3. //定义数据元素
    4. typedef struct {
    5. int id;
    6. char * name;
    7. }ElementType;
    8. //定义顺序表结构
    9. typedef struct{
    10. ElementType datas[MAX_SIZE];//顺序表中的数据元素集合
    11. int length;//当前顺序表中的元素个数
    12. }SeqList;
    //定义顺序表
    #include <stdio.h>
    #include <stdlib.h>
    #include "DataElement.h"
    
    //初始化顺序表
    void InitList(SeqList * seqList,ElementType *elementArray,int length);
    
    //按照下标插入元素
    void InsertElement(SeqList *seqList,int index,ElementType element);
    
    //打印
    void PrintList(SeqList* seqLiat);
    
    //删除顺序表中指定下标的元素,返回删除的元素,如果删除失败返回Null
    ElementType * DeleteElement(SeqList* seqList,int index);
    
    //返回指定下标的元素
    ElementType *GetElement(SeqList *seqList,int index);
    
    //返回顺序表的长度
    int GetLength(SeqList * seqList);
    
    //返回顺序表是否为空
    int IsEmpty(SeqList *seqList);
    
    //清空顺序表
    void ClearList(SeqList *seqList);
    
    #include "SequenceList.h"
    
    int i;
    int j;
    //初始化顺序表
    void InitList(SeqList * seqList,ElementType *elemArray,int length){
        if(length>MAX_SIZE){
        printf("超出了最大容量%d\n",length);
        }
        seqList->length=0;
        for(i=0;i<length;i++){
        //每次循环都在下标为i的位置插入一个元素
            InsertElement(seqList,i,elemArray[i]);
        }
    }
    
    //按照下标插入元素
    void InsertElement(SeqList *seqList,int index,ElementType element){
        //验证插入后的空间是否超过MAX_SIZE
        //index的值是否合法[0,MAX_SIZE-1]
        //插入的index应该在length之内
        //从第length-1个下标开始,前面一个元素赋值给后面一个元素
        if(seqList->length+1 >=MAX_SIZE){
        printf("数组已满,插入元素失败%d\n",seqList->length);
        return ;
        }
        if(index<0||index >MAX_SIZE-1){
        printf("超出范围la\n");
        return ;
        }
        if(index > seqList->length){
            printf("超出范围啦%d%d\n",index,seqList->length);
            return;
        }
        //从最后一个到要插入的序号复制并后退一个
        for(j=seqList->length-1;j>=index;j--){
            seqList->datas[j+1]=seqList->datas[j];
        }
        seqList->datas[index]=element;
        //顺序表的总长度加1
        seqList->length++;
    }
    
    //删除顺序表中指定下标的元素,返回删除的元素,如果删除失败返回Null
    ElementType * DeleteElement(SeqList* seqList,int index){
    
    
        if(index<0 || index>MAX_SIZE){
        printf("下标越界,无法删除指定下标的元素\n");
        return NULL;
        }
        //1.找到要删除的元素,并保存以便返回(保存的是已删除元素的副本)
        ElementType * delElement=(ElementType *)malloc(sizeof(ElementType));
        //单独定义并调用查找函数,返回要删除元素的指针
        *delElement=*GetElement(seqList,index);
    
        //2.从指定位置删除,后面一个元素赋值给前面一个元素
        for(i=index;i<seqList->length;i++){
        seqList->datas[i]=seqList->datas[i+1];
        }
        seqList->length--;
        //3.顺序表的总长度-1
    
        return delElement;//建议使用完毕后进行free,否则会造成内存泄露
    }
    
    //返回指定下标的元素
    ElementType *GetElement(SeqList *seqList,int index){
        if(index<0 || index>MAX_SIZE){
        printf("下标越界,无法找到指定下标的元素\n");
        return NULL;
        }
        ElementType *element;//要查找的元素
        element=&seqList->datas[index];
        return element;
    }
    
    
    //返回顺序表的长度
    int GetLength(SeqList * seqList){
        if(seqList==NULL)
            return 0;
    return seqList->length;
    }
    
    //返回顺序表是否为空
    int IsEmpty(SeqList *seqList){
    return GetLength(seqList)==0 ? 0 :seqList->length;
    }
    
    //清空顺序表
    void ClearList(SeqList *seqList){
        if(seqList==NULL){
        return;
        }
        seqList->length=0;
    }
    
    
    //打印
    void PrintList(SeqList * seqList){
        for(i=0;i<seqList->length;i++){
        printf("%d\t%s\t%d\n",seqList->datas[i].id,seqList->datas[i].name,i);
        }
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "SequenceList.h"
    
    
    
    ElementType dataArray[]={
        {1,"萧炎"},
        {2,"唐三"},
        {3,"林动"},
        {4,"叶笑"}
    };
    
    
    void TestSequenceList();
    
    
    void TestSequenceList(){
        SeqList seqList;
    
        InitList(&seqList,dataArray,sizeof(dataArray)/sizeof(dataArray[0]));
        PrintList(&seqList);
        ElementType *delElement;
    
        delElement=DeleteElement(&seqList,2);
        printf("删除后:\n");
        PrintList(&seqList);
        printf("被删除的元素:\n");
        printf("%d\t%s\n",delElement->id,delElement->name);
        free(delElement);//一定要记得释放资源
    }
    int main(){
        TestSequenceList();
        system("pause");
    return 0;
    }