SeqList——顺序表

/*
** File name: SeqList.h
** Author: ZhouFeng
** Date: 2012/03/25
*/
#ifndef SEQ_LIST_H
#define SEQ_LIST_H

#define MAX_SIZE 100
#define ERROR 0
#define SUCCESS 1

typedef int EleType;
typedef struct SeqList
{
    EleType _seqList[MAX_SIZE];
    int length;
}SeqList; 

void InitSeqList(SeqList *L);
int InsertEle(SeqList *L, int index, EleType data);
int DeleteEle(SeqList *L, int index, EleType *data);
int GetEle(SeqList *L, int index, EleType *data);
int GetSeqListLength(SeqList *L);

#endif
/*
** File name: SeqList.c
** Author: ZhouFeng
** Date: 2012/03/25
*/
#include <stddef h="">
#include "SeqList.h"

void InitSeqList(SeqList *L)
{
    if(L == NULL)
    {
	return;
    }

    L-&gt;length = 0;
}

int InsertEle(SeqList *L, int index, EleType data)
{
    int i = 0;
    
    if(L == NULL &amp;&amp; (index &lt; 0 &amp;&amp; index &gt; L-&gt;length - 1)
	|| L-&gt;length &gt;= MAX_SIZE)
    {
	return ERROR;
    }

    /* insert a new element front of 'index'*/
    for(i = L-&gt;length - 1; i &gt; index - 1; --i)
    {
	(L-&gt;_seqList)[i + 1] = (L-&gt;_seqList)[i];
    }

    (L-&gt;_seqList)[index] = data;
    ++L-&gt;length;

    return SUCCESS;
}

int DeleteEle(SeqList *L, int index, EleType *data)
{
    int i = 0;
    
    if(L == NULL &amp;&amp; index &lt; 0 &amp;&amp; index &gt; L-&gt;length - 1
	&amp;&amp; data == NULL &amp;&amp; L-&gt;length &lt;= 0)
    {
	return ERROR;
    }

    *data = (L-&gt;_seqList)[index];
    for(i = index; i &lt; L-&gt;length; ++i)
    {
	(L-&gt;_seqList)[i] = (L-&gt;_seqList)[i + 1];
    }

    --L-&gt;length;

    return SUCCESS;
}

int GetEle(SeqList *L, int index, EleType *data)
{
    if(L == NULL &amp;&amp; index &lt; 0 &amp;&amp; index &gt; L-&gt;length - 1)
    {
	return ERROR;
    }

    *data = (L-&gt;_seqList)[index];

    return SUCCESS;
}

int GetSeqListLength(SeqList *L)
{
    return L-&gt;length;
}
</stddef>
#include <stdio h="">
#include <stdlib h="">
#include "SeqList.h"

/* Print */
void PrintSeqList(SeqList *L)
{
    int i = 0;

    for(i = 0; i &lt; L-&gt;length; )
    {
	printf("%4d", (L-&gt;_seqList)[i]);
	++i;
	if(i % 5 == 0)
	{
	    printf("\n");
	}
    }
}

int main(int argc, char* argv[])
{
    int i = 0;
    SeqList L;

    srand((unsigned long)time());
    
    InitSeqList(&amp;L);
    for(i = 0; i &lt; MAX_SIZE; ++i)
    {
	InsertEle(&amp;L, 0, rand() % 30);
    }

    PrintSeqList(&amp;L);

    return 0;
}
</stdlib></stdio>

你可能感兴趣的:(list)