关闭

SeqList——顺序表

358人阅读 评论(0) 收藏 举报
/*
** 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 
#include "SeqList.h"

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

    L->length = 0;
}

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

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

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

    return SUCCESS;
}

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

    *data = (L->_seqList)[index];
    for(i = index; i < L->length; ++i)
    {
	(L->_seqList)[i] = (L->_seqList)[i + 1];
    }

    --L->length;

    return SUCCESS;
}

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

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

    return SUCCESS;
}

int GetSeqListLength(SeqList *L)
{
    return L->length;
}
#include 
#include 
#include "SeqList.h"

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

    for(i = 0; i < L->length; )
    {
	printf("%4d", (L->_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(&L);
    for(i = 0; i < MAX_SIZE; ++i)
    {
	InsertEle(&L, 0, rand() % 30);
    }

    PrintSeqList(&L);

    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9465次
    • 积分:197
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类