数据结构-顺序表的简单实现

使用连续地址的数组实现简单的一个顺序表。

主要实现了线性表的初始化,在一个节点中插入数据,删除一个节点的数据,获取一个节点的数据的功能 。

#define LETTER_BUFFER_LENGTH    20    // 数组长度
#define LETTER_BUFFER_INCREAM    10    // 数组增加长度

typedef struct 
{
    char ch;
}StrLetter;
typedef struct Seq_List
{
    StrLetter *head;
    int length;
    int size;
}SeqList;
SeqList sl;

/*
 *初始化一个空的线性表
 */
int SeqListInit(SeqList *sl)
{
    sl->head = (StrLetter *)malloc(LETTER_BUFFER_LENGTH * sizeof(StrLetter));

    if(sl->head == NULL)
    {
        return -1;
    }

    sl->length = 0;
    sl->size = LETTER_BUFFER_LENGTH;

    return 0;
}

/*
 * 在index的位置插入数据ch
 */
int SeqListInsert(SeqList *sl, int index, StrLetter node)
{
    StrLetter * newbase;
    StrLetter *curNode, *otherNode;
    int cnts;
    
    if(index < 1 || index > sl->length + 1)    // 对插入位置进行检查
    {
        return -1;
    }
#if 1    
    if(sl->length >= sl->size)
    {
        newbase = (StrLetter *)realloc(sl->head, (sl->size + LETTER_BUFFER_INCREAM) * sizeof(StrLetter));
        
        if(newbase == NULL)
        {
            return -2;
        }
        printf("Buffer realloc!\n");
        sl->head = newbase;
        sl->size += LETTER_BUFFER_INCREAM;
    }
#endif
#if 1
    // 插入位置之后数据移位 方法1
    for(cnts = sl->length; cnts > index - 1; --cnts)
    {
        sl->head[cnts] = sl->head[cnts - 1];
    }
    sl->head[cnts] = node;
#else
    // 插入位置之后数据移位 方法2
    curNode = &(sl->head[index - 1]);
    otherNode = &(sl->head[sl->length - 1]);
    for(; otherNode >= curNode; otherNode--)
    {
        *(otherNode + 1) = *otherNode;
    }
    *curNode = node;
#endif    

    sl->length++;

    return 0;
}


/*
 * 删除索引位置index的数据,并将删除数据返回到ch中
 */
int SeqListDelete(SeqList *sl, int index, StrLetter *ch)
{
    int cnt;
    StrLetter *curNode, *otherNode;
    if(index < 1 || index > sl->length)
    {
        return -1;
    }
    
    *ch = sl->head[index - 1];
#if 1
    for(cnt = index; cnt < sl->length; cnt++)
    {
        sl->head[cnt - 1] = sl->head[cnt]; 
    }
#else
    curNode = &(sl->head[index - 1]);
    otherNode = &(sl->head[sl->length - 1]);
    for(; curNode < otherNode; ++curNode)
    {
        *curNode = *(curNode + 1);
    }
#endif
    sl->length--;

    return 0;
}

/*
 *获取索引位置index的数据,保存到ch中
 */
int SeqListGet(SeqList sl, int index, StrLetter *ch)
{
    if(index < 1 || index > sl.length)
    {
        return -1;
    }

    *ch = sl.head[index - 1];

    return 0;
}

int main()
{
    char ch;
    StrLetter node;
    int index;
    int ret;
/*******************************************************************************/
    ret = SeqListInit(&sl);

    if(ret != 0)
    {
        printf("error!\n");
    }
    else{
        printf("Init complete!\n");
    }
/*

PC:~/WorkSpace/seqlist$ gcc -o seqlist seq_list.c
LexyPC:~/WorkSpace/seqlist$ ./seqlist 
Init complete!

*/
    for(index = 1, ch = 'a'; ch <= 'z'; ++ch, ++index)
    {
        node.ch = ch;
        ret = SeqListInsert(&sl, index, node);
        if(ret == 0)
        {
            printf("%c insert!\t", ch);
        }
        if((index % 4) == 0)
        {
            printf("\n");
        }
    }
    printf("\n");
/*

a insert!       b insert!       c insert!       d insert!
e insert!       f insert!       g insert!       h insert!
i insert!       j insert!       k insert!       l insert!
m insert!       n insert!       o insert!       p insert!
q insert!       r insert!       s insert!       t insert!
Buffer realloc!
u insert!       v insert!       w insert!       x insert!
y insert!       z insert!

*/
    for(index = 1; index <= 26; ++index)
    {
        if(SeqListGet(sl, index, &node) != -1)
        {
            printf("\t%c", node.ch);
        }
        else
        {
            break;
        }
        if((index % 6) == 0)
        {
            printf("\n");
        }
    }
    printf("\n");
/*

 a       b       c       d       e       f
        g       h       i       j       k       l
        m       n       o       p       q       r
        s       t       u       v       w       x
        y       z

*/
    if(SeqListDelete(&sl, 22, &node) == 0)
    {
        printf("%c is deleted!\n", node.ch);
    }

/*

v is deleted!

*/ 
    printf("\n");
    for(index = 1; index <= 26; ++index)
    {
        if(SeqListGet(sl, index, &node) != -1)
        {
            printf("\t%c", node.ch);
        }
        else
        {
            break;
        }
        if((index % 6) == 0)
        {
            printf("\n");
        }
    }
    printf("\n");
/*

a       b       c       d       e       f
        g       h       i       j       k       l
        m       n       o       p       q       r
        s       t       u       w       x       y
        z

*/
    for(index = 1, ch = 'a'; ch <= 'z'; ++ch, ++index)
    {
        node.ch = ch;
        ret = SeqListInsert(&sl, index, node);
        if(ret == 0)
        {
            printf("%c insert!\t", ch);
        }
        if((index % 4) == 0)
        {
            printf("\n");
        }
    }
    printf("\n");
/*

a insert!       b insert!       c insert!       d insert!
e insert!       Buffer realloc!
f insert!       g insert!       h insert!
i insert!       j insert!       k insert!       l insert!
m insert!       n insert!       o insert!       Buffer realloc!
p insert!
q insert!       r insert!       s insert!       t insert!
u insert!       v insert!       w insert!       x insert!
y insert!       Buffer realloc!
z insert!

*/
    for(index = 1; index <= 52; ++index)
    {
        if(SeqListGet(sl, index, &node) != -1)
        {
            printf("\t%c", node.ch);
        }
        else
        {
            break;
        }
        if((index % 6) == 0)
        {
            printf("\n");
        }
    }
    printf("\n");
/*

a       b       c       d       e       f
        g       h       i       j       k       l
        m       n       o       p       q       r
        s       t       u       v       w       x
        y       z       a       b       c       d
        e       f       g       h       i       j
        k       l       m       n       o       p
        q       r       s       t       u       w
        x       y       z

*/
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dongxu_85/article/details/81542127