顺序表的相关算法

来源:互联网 发布:u深度安装linux 编辑:程序博客网 时间:2024/05/21 14:52
#include<stdio.h>#include<malloc.h>#define MAXSIZE 100    //定义一个顺序表typedef struct {    char data{ MAXSIZE };    int last;}Seqlist;Seqlist *initSeqlist()     //初始化顺序表{    Seqlist *L;             //定义一个指向结构体的指针    L = (Seqlist *)malloc(sizeof(Seqlist));   // http://blog.csdn.net/xw13106209/article/details/4962479    L->last = -1;                       //分配长度为num_bytes字节的内存块    return L;}int LengthList(Seqlist *L)       //统计顺序表长度{    int length;    length = L->last + 1;    return length;}int InsertSeq(Seqlist *L, int i, char x )   //顺序表插入算法{    int j;    if (L->last == MAXSIZE - 1)    {        printf("表满!");        return(-1);    }    else if (i<1||i>L->last+2)    {        printf("插入位置不合法");        return(0);    }    else    {        for(j=L->last;j>=i-1;j--)            L->data[j+1] = L->data[j];        L->data[i-1]=x;        L->last++;        return(1);    }}int DeletSeq(Seqlist *L,int i)      //顺序表删除算法{    int j;    if(L->last == -1)    {        printf("顺序表为空,不能进行删除操作!");        return(-1);    }    else if(i<1||i>L->last+1)    {        printf("删除位置不合法");        return(0);    }    else {        for(j=i;j<L->last;j++)            L->data[j-1]=L-data[j];        L->last--;        return(1);    }}
0 0
原创粉丝点击