头文件:
//****************************************
//* 文件:Sequence.h
//* 描述:线性表顺序存储操作
//* 创建人:草根帮主
//* 创建日期:2009-6-6
//* 修改日期:2010-6-6
//****************************************
#ifndef _SEQUENCE_H_
#define _SEQUENCE_H_
#include <stdio.h>
#include <stdlib.h>
#define INIT_ALLOCATION 100 //存储空间初始化大小
#define DATA_INCREMENT 10 //存储空间分配增量
typedef unsigned int DATATYPE;
enum STATUS_LIST
{
SEQLIST_ERROR = -1, //分配空间错误
SEQLIST_SUCCESS = 1 //分配空间成功
};
typedef struct SEQUENCELIST
{
DATATYPE* element; //数据元素
unsigned int nLength; //当前顺序表长度
unsigned int listSize; //初始化分配大小
}*SeqList;
//构造一个空的线性表顺序存储结构
STATUS_LIST Init_SeqList(SeqList seqList);
//返回线性表当前长度
unsigned int GetSeqLength(SeqList seqList);
//查找元素
bool FindElement(SeqList seqList, unsigned int i, DATATYPE* findElement);
//插入元素
bool InsertSeqList(SeqList seqList, unsigned int i, DATATYPE addElement);
//删除元素
bool DelSeqList(SeqList seqList, unsigned int i, DATATYPE* delEle);
//打印所有元素
void PrintSeqList(SeqList seqList);
#endif
cpp文件:
#include "Sequence.h"
//**********************************************
//* 函数:Init_SeqList
//* 功能:构造一个空的线性表顺序存储结构
//* 参数:
//* seqList:线性表顺序存储结构
//* 返回值:
//* 创建人:草根帮主
//* 创建时间:
//* 备注:
//**********************************************
STATUS_LIST Init_SeqList(SeqList seqList)
{
seqList->element = (DATATYPE*)malloc(INIT_ALLOCATION*sizeof(DATATYPE));
if ( !seqList->element )
return SEQLIST_ERROR;
seqList->listSize = INIT_ALLOCATION;
seqList->nLength = 0;
return SEQLIST_SUCCESS;
}
//**********************************************
//* 函数:GetSeqLength
//* 功能:返回线性表当前长度
//* 参数:
//* seqList:线性表顺序存储结构
//* 返回值:
//* 创建人:草根帮主
//* 创建时间:
//* 备注:
//**********************************************
unsigned int GetSeqLength(SeqList seqList)
{
return seqList->nLength;
}
//**********************************************
//* 函数:GetElement
//* 功能:查找元素
//* 参数:0
//* seqList:线性表顺序存储结构,i:查找元素的位置,findElement:查找到的元素
//* 返回值:
//* 创建人:草根帮主
//* 创建时间:
//* 备注:
//**********************************************
bool FindElement(SeqList seqList, unsigned int i, DATATYPE* findElement)
{
if ( i < 1 || i > seqList->nLength+1 ) return false;
*findElement = *(seqList->element+i-1);
return true;
}
//**********************************************
//* 函数:InsertSeqList
//* 功能:插入元素
//* 参数:0
//* seqList:线性表顺序存储结构,i:插入元素的位置,addElement:添加的元素
//* 返回值:
//* 创建人:草根帮主
//* 创建时间:
//* 备注:
//**********************************************
bool InsertSeqList(SeqList seqList, unsigned int i, DATATYPE addElement)
{
if ( i < 1 || i > seqList->nLength + 1 ) return false;
//考虑元素添加过程中,初始化分配空间不足,需要重新分配空间
if ( seqList->nLength >= seqList->listSize )
{
seqList->element = (DATATYPE*)realloc(seqList->element, (seqList->listSize+DATA_INCREMENT)*sizeof(DATATYPE));
seqList->listSize += DATA_INCREMENT;
}
//最后一个元素
DATATYPE* q = seqList->element + (seqList->nLength-1);
for(;q >= seqList->element+i-1;--q)
{
*(q+1) = *q;
}
*(seqList->element+i-1) = addElement;
++seqList->nLength;
return true;
}
//**********************************************
//* 函数:DelSeqList
//* 功能:删除元素
//* 参数:
//* seqList:线性表顺序存储结构,i:删除元素的位置,delEle:删除元素
//* 返回值:
//* 创建人:草根帮主
//* 创建时间:
//* 备注:
//**********************************************
bool DelSeqList(SeqList seqList, unsigned int i, DATATYPE* delEle)
{
if ( i < 1 || i >= seqList->nLength + 1 ) return false;
DATATYPE* q = seqList->element+i-1;
*delEle = *q;
for(++q; q <= (seqList->element+seqList->nLength-1);++q)
{
*(q-1) = *q;
}
--seqList->nLength;
return true;
}
//**********************************************
//* 函数:PrintSeqList
//* 功能:打印所有元素
//* 参数:
//* seqList:线性表顺序存储结构
//* 返回值:
//* 创建人:草根帮主
//* 创建时间:
//* 备注:
//**********************************************
void PrintSeqList(SeqList seqList)
{
if ( !seqList ) return;
unsigned int i = 0;
printf("存储元素列表:\n");
while(i < seqList->nLength)
{
printf("%d\n", *(seqList->element+i++));
}
}
//入口函数
int main(void)
{
struct SEQUENCELIST seq;
Init_SeqList(&seq);
DATATYPE addElement = 1;
InsertSeqList(&seq, 1, addElement);
addElement = 2;
InsertSeqList(&seq, 2, addElement);
addElement = 3;
InsertSeqList(&seq, 3, addElement);
PrintSeqList(&seq);
/*
if ( FindElement(&seq, 1, &addElement) )
{
printf("查找元素:%d\n", addElement);
}
if ( DelSeqList(&seq, 1, &addElement) )
{
printf("删除元素:%d\n", addElement);
}
PrintSeqList(&seq);
*/
return 0;
}