数据结构---顺序表

来源:互联网 发布:deepin linux软件源 编辑:程序博客网 时间:2024/06/06 02:18


采用数组存储数据


typedef struct

{

   DATA ListData[MAXSIZE+1];

   int ListLen;

}SeqListType;


void SeqListInit(SeqListType *SL)

{
    SL->ListLen = 0;
}

int SeqListLength(SeqListType *SL)
{
    return (SL->ListLen);
}

int SeqListAdd(SeqListType *SL,DATA data)
{
    if(SL->ListLen >= MAXSIZE)
    {
        printf(" list is full \n");
        return 0 ;
    }
    SL->ListData[++SL->ListLen] = data;
    return 1;
}

int SeqListInsert(SeqListType *SL,int n,DATA data)
{
    int i;
    if(SL->ListLen >= MAXSIZE)
    {
        printf("list is full\n");
        return 0 ;
    }
    if(n < 1 || n > SL->ListLen-1)
    {
        printf("insert error\n");
        return 0;
    }
    for(i=SL->ListLen;i>=n;i--)
    {
        SL->ListData[i+1] = SL->ListData[i];
    }
    SL->ListData[n] = data;

    SL->ListLen++;
    return 1;
    
}

int SeqListDelete(SeqListType *SL,int n)
{
    int i;
    if(n < 1 || n > SL->ListLen +1)
    {
        printf("delete error\n");
        return 0;
    }
    for(i=n;i<SL->ListLen;i++)
        SL->ListData[i] = SL->ListData[i+1];

    SL->ListLen--;
    return 1;
}

DATA *SeqListFindByNum(SeqListType *SL,int n)
{
    if(n < 1 || n > SL->ListLen +1)
    {
        printf("node error \n");
        return NULL;
    }
    return &(SL->ListData[n]);
}

int SeqListFindByCont(SeqListType *SL,char *key)
{
    int i;
    for(i=1;i<=SL->ListLen;i++)
        if(strcmp(SL->ListData[i].key,key) == 0)
            return i;
    return 0;
}
0 0
原创粉丝点击