/*头文件*/ #ifndef _SEQLIST_H_ #define _SEQLIST_H_ #include <stdio.h> #include <string.h> /** *Description:数据结构中:顺序表操作 */ typedef char TChar; typedef int TInt; #define MAXSIZE 5 typedef struct array { TChar tData[MAXSIZE]; TInt nLength; }*SeqList; void ErrorInfo(int nFlag); void InitSeqList(SeqList pSeqList); void AddElementOfSeqList(SeqList pSeqList, TChar ch); void DelElementOfSeqList(SeqList pSeqList, TInt nIndex); void ModifyElementOfSeqList(SeqList pSeqList, TInt nIndex, TChar ch); TInt FindChar(SeqList pSeqList, TChar ch); #endif /*cpp文件*/ /* 功能:操作错误信息 */ void ErrorInfo(int nFlag) { switch ( nFlag ) { case 0: printf("Array is overflow!"); break; case 1: printf("Not found element!"); break; default: printf("Success!"); break; } } /* 功能:初始化顺序表 */ void InitSeqList(SeqList pSeqList) { memset(pSeqList->tData, 0, MAXSIZE+1); pSeqList->nLength = 0; } /* 功能:向顺序表中添加元素 */ void AddElementOfSeqList(SeqList pSeqList, TChar ch) { if ( pSeqList->nLength >= MAXSIZE-1 )//超出顺序表申请最大额度 { ErrorInfo(0); return; } pSeqList->tData[pSeqList->nLength++] = ch; } /* 功能:删除顺序表中第nIndex位置上的元素 */ void DelElementOfSeqList(SeqList pSeqList, TInt nIndex) { if ( nIndex < 1 || nIndex > pSeqList->nLength ) { ErrorInfo(1); return; } /*abcde*/ TInt i = 0; for ( i = nIndex-1;i < pSeqList->nLength;++i ) { pSeqList->tData[i] = pSeqList->tData[i+1]; //pSeqList->tData[i+1] = 0; } --pSeqList->nLength; } /* 功能:修改顺序表中第nIndex位置上的元素 */ void ModifyElementOfSeqList(SeqList pSeqList, TInt nIndex, TChar ch) { if ( nIndex < 1 || nIndex >= pSeqList->nLength ) { ErrorInfo(1); return; } pSeqList->tData[nIndex-1] = ch; } /* 功能:查找顺序表中第nIndex位置上的元素 */ TInt FindChar(SeqList pSeqList, TChar ch) { TInt i = 0; for ( ;i < pSeqList->nLength;++i ) { if ( ch == pSeqList->tData[i] ) { return i; } } if ( i >= pSeqList->nLength ) { ErrorInfo(1); } return -1; }