データ構造線形テーブル順序格納操作
ヘッダファイル:
cppファイル:
//****************************************
//* :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(" :
");
while(i < seqList->nLength)
{
printf("%d
", *(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
", addElement);
}
if ( DelSeqList(&seq, 1, &addElement) )
{
printf(" :%d
", addElement);
}
PrintSeqList(&seq);
*/
return 0;
}