데이터 구조 (엄 울 민) C 언어 실현 - 선형 표 의 순서 저장
16828 단어 C 언어 승급
이번에 본 것 은 선형 표 의 순서 저장 입 니 다. 코드 는 다음 과 같 습 니 다.
1. 코드 의 가 독성 을 높이 기 위해 매크로 정 의 를 사 용 했 습 니 다. 파일 이름 은 BaseDefine. h 이 고 내용 은 다음 과 같 습 니 다.
#ifndef BASE_DEFINE_H
#define BASE_DEFINE_H
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int bool;
#endif
2. 선형 헤더 파일, 이름 은 SequenceList. h 이 고 내용 은 다음 과 같다.#ifndef SEQUENCE_LIST_H
#define SEQUENCE_LIST_H
#include "BaseDefine.h"
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
typedef int ElemType;
typedef struct
{
ElemType* pBase;
int length;
int listSize;
}SqList;
//The pointer of function
typedef bool (*pFunCompare)(ElemType elemA,ElemType elemB);
typedef Status (*pFunVisit)(ElemType elem);
Status InitList_Sq(SqList *pSqList);
Status DestoryList_Sq(SqList *pSqList);
Status ClearList_Sq(SqList *pSqList);
bool ListEmpty_Sq(const SqList sqList);
int ListLength_Sq(const SqList sqList);
Status GetElem_Sq(const SqList sqList,int location,ElemType *elem);
int LocateElem_Sq(const SqList sqList,const ElemType e,pFunCompare comFun);
Status PriorElem(const SqList sqList,const ElemType curElem,ElemType *preElem);
Status NextElem(const SqList sqList,const ElemType curElem,ElemType *nextElem);
Status InsertList_Sq(SqList *pSqList,int insertLocation,const ElemType elem);
Status ListDelete_Sq(SqList *pSqList,int deleteLocation,ElemType *pElem);
Status ListTraverse(SqList sqList,pFunVisit visitFun);
bool compareElem(const ElemType elemA,const ElemType elemB);
Status visitElem(const ElemType elem);
Status copyElem(ElemType *dstElem,const ElemType srcElem);
void printList_Sq(const SqList sqList);
#endif
3. 선형 표 의 실현, 이름 은 SequenceList. c 이 고 내용 은 다음 과 같다.
#include "SequenceList.h"
#include
#include
#include
#include
Status InitList_Sq(SqList * pSqList)
{
pSqList->pBase = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!pSqList->pBase)
exit(OVERFLOW);
memset(pSqList->pBase,0,LIST_INIT_SIZE * sizeof(ElemType));
pSqList->length = 0;
pSqList->listSize = LIST_INIT_SIZE;
}
Status DestoryList_Sq(SqList * pSqlist)
{
if(!pSqlist->pBase)
{
free(pSqlist->pBase);
pSqlist->pBase = NULL;
}
pSqlist->length = 0;
pSqlist->listSize = 0;
return TRUE;
}
Status ClearList_Sq(SqList * pSqList)
{
//how to judge the sq list is exist or not?
pSqList->length = 0;
memset(pSqList->pBase,0,pSqList->listSize * sizeof(ElemType));
return OK;
}
bool ListEmpty_Sq(const SqList sqList)
{
if(0 == sqList.length)
return TRUE;
return FALSE;
}
int ListLength_Sq(const SqList sqList)
{
return sqList.length;
}
Status GetElem_Sq(const SqList sqList, int location, ElemType * elem)
{
if((location < 1) || (location > sqList.length))
return ERROR;
copyElem(elem,sqList.pBase[location - 1]);
return OK;
}
int LocateElem_Sq(const SqList sqList, const ElemType e, pFunCompare comFun)
{
int i = 0;
for(i = 0;i < sqList.length;i++)
{
if(comFun(sqList.pBase[i],e))
return i + 1;
}
return INFEASIBLE;
}
Status PriorElem(const SqList sqList, const ElemType curElem, ElemType * preElem)
{
int location = LocateElem_Sq(sqList,curElem,compareElem);
if(location > 1)
{
copyElem(preElem,sqList.pBase[location - 1 - 1]);
return OK;
}
return ERROR;
}
Status NextElem(const SqList sqList, const ElemType curElem, ElemType * nextElem)
{
int location = LocateElem_Sq(sqList,curElem,compareElem);
if(location < sqList.length)
{
copyElem(nextElem,sqList.pBase[location - 1 + 1]);
return OK;
}
return ERROR;
}
Status InsertList_Sq(SqList * pSqList, int insertLocation,const ElemType e)
{
ElemType * p_new_base = NULL;
int i = 0;
if((insertLocation < 1) || (insertLocation > (pSqList->length) + 1))
return ERROR;
if(pSqList->length >= pSqList->listSize)
{
p_new_base = (ElemType *)realloc(pSqList->pBase,(pSqList->listSize + LIST_INCREMENT)*sizeof(ElemType));
if(!p_new_base)
exit(OVERFLOW);
pSqList->listSize += LIST_INCREMENT;
pSqList->pBase = p_new_base;
}
for(i = (pSqList->length - 1);i >= insertLocation - 1;i-- )
{
pSqList->pBase[i + 1] = pSqList->pBase[i];
}
pSqList->pBase[insertLocation - 1] = e;
pSqList->length++;
}
Status ListDelete_Sq(SqList * pSqList, int deleteLocation, ElemType * pElem)
{
int i = 0;
if((deleteLocation < 1) || (deleteLocation > pSqList->length))
return ERROR;
(*pElem) = pSqList->pBase[deleteLocation - 1];
for(i = deleteLocation;i < pSqList->length;i++)
{
pSqList->pBase[i - 1] = pSqList->pBase[i];
}
pSqList->length--;
return OK;
}
Status ListTraverse(SqList sqList, pFunVisit visitFun)
{
int i = 0;
for(i = 0;i < sqList.length;i++)
{
visitFun(sqList.pBase[i]);
}
}
bool compareElem(const ElemType elemA,const ElemType elemB)
{
return (elemA == elemB) ? TRUE:FALSE;
}
Status visitElem(const ElemType elem)
{
printf("Fun visit:the elem value is [%d]
",elem);
}
Status copyElem(ElemType * dstElem, const ElemType srcElem)
{
*dstElem = srcElem;
}
void printList_Sq(const SqList sqList)
{
int i = 0;
if(!sqList.pBase)
exit(INFEASIBLE);
printf("Printf sqlist info start
");
printf("SqList info:BaseAddr[%d],Length[%d],MallocSize[%d]
",sqList.pBase,sqList.length,sqList.listSize);
for(i = 0;i < sqList.length;i++)
{
printf("Elem [%d],value[%d]
",i + 1,sqList.pBase[i]);
}
printf("Printf sqlist info end
");
}
4. 이 방법 들 을 호출 하여 정확 한 지 검증 합 니 다. 파일 이름 main. c. 내용 은 다음 과 같 습 니 다.
#include
#include "SequenceList.h"
int main()
{
SqList s_list;
ElemType elem;
ElemType preElem;
ElemType nextElem;
int i = 0;
printf("------------The main function start------------
");
printf("------------InitList_Sq start------------
");
InitList_Sq(&s_list);
printList_Sq(s_list);
printf("------------InitList_Sq end------------
");
printf("------------InsertList_Sq start------------
");
for(i = 1;i <= 110;i++)
{
InsertList_Sq(&s_list,1,i);
}
printList_Sq(s_list);
printf("------------InsertList_Sq end------------
");
printf("------------ListDelete_Sq start------------
");
for(i = 1;i <= 55;i++)
{
if(ListDelete_Sq(&s_list,i,&elem))
printf("Delete elem[%d],value[%d]
",i,elem);
}
printList_Sq(s_list);
printf("------------ListDelete_Sq end------------
");
printf("------------GetElem_Sq start------------
");
GetElem_Sq(s_list,27,&elem);
printf("The location[%d] elem is [%d]
",27,elem);
printf("------------GetElem_Sq end------------
");
printf("------------LocateElem_Sq start------------
");
printf("The elem[%d] location is [%d]
",elem,LocateElem_Sq(s_list,elem,compareElem));
printf("------------LocateElem_Sq end------------
");
printf("------------PriorElem start------------
");
PriorElem(s_list,elem,&preElem);
printf("The elem[%d] preElem is [%d]
",elem,preElem);
printf("------------PriorElem end------------
");
printf("------------NextElem start------------
");
NextElem(s_list,elem,&nextElem);
printf("The elem[%d] nextElem is [%d]
",elem,nextElem);
printf("------------NextElem end------------
");
printf("------------ListTraverse start------------
");
ListTraverse(s_list,visitElem);
printf("------------ListTraverse end------------
");
printf("------------ClearList_Sq start------------
");
ClearList_Sq(&s_list);
printf("------------ClearList_Sq end------------
");
printf("------------ListEmpty_Sq start------------
");
if(ListEmpty_Sq(s_list))
{
printf("The list is empty
");
}
else
{
printf("The list is not empty
");
}
printf("------------ListEmpty_Sq end------------
");
printf("------------DestoryList_Sq start------------
");
DestoryList_Sq(&s_list);
printf("------------DestoryList_Sq end------------
");
printf("------------The main function end------------
");
return OK;
}
5. 컴 파일
gcc main.c SequenceList.c -o main
6. 집행
./main
7. 실행 결과
------------The main function start------------
------------InitList_Sq start------------
Printf sqlist info start
SqList info:BaseAddr[18399248],Length[0],MallocSize[100]
Printf sqlist info end
------------InitList_Sq end------------
------------InsertList_Sq start------------
Printf sqlist info start
SqList info:BaseAddr[18399248],Length[110],MallocSize[110]
Elem [1],value[110]
Elem [2],value[109]
Elem [3],value[108]
Elem [4],value[107]
Elem [5],value[106]
Elem [6],value[105]
Elem [7],value[104]
Elem [8],value[103]
Elem [9],value[102]
Elem [10],value[101]
Elem [11],value[100]
Elem [12],value[99]
Elem [13],value[98]
Elem [14],value[97]
Elem [15],value[96]
Elem [16],value[95]
Elem [17],value[94]
Elem [18],value[93]
Elem [19],value[92]
Elem [20],value[91]
Elem [21],value[90]
Elem [22],value[89]
Elem [23],value[88]
Elem [24],value[87]
Elem [25],value[86]
Elem [26],value[85]
Elem [27],value[84]
Elem [28],value[83]
Elem [29],value[82]
Elem [30],value[81]
Elem [31],value[80]
Elem [32],value[79]
Elem [33],value[78]
Elem [34],value[77]
Elem [35],value[76]
Elem [36],value[75]
Elem [37],value[74]
Elem [38],value[73]
Elem [39],value[72]
Elem [40],value[71]
Elem [41],value[70]
Elem [42],value[69]
Elem [43],value[68]
Elem [44],value[67]
Elem [45],value[66]
Elem [46],value[65]
Elem [47],value[64]
Elem [48],value[63]
Elem [49],value[62]
Elem [50],value[61]
Elem [51],value[60]
Elem [52],value[59]
Elem [53],value[58]
Elem [54],value[57]
Elem [55],value[56]
Elem [56],value[55]
Elem [57],value[54]
Elem [58],value[53]
Elem [59],value[52]
Elem [60],value[51]
Elem [61],value[50]
Elem [62],value[49]
Elem [63],value[48]
Elem [64],value[47]
Elem [65],value[46]
Elem [66],value[45]
Elem [67],value[44]
Elem [68],value[43]
Elem [69],value[42]
Elem [70],value[41]
Elem [71],value[40]
Elem [72],value[39]
Elem [73],value[38]
Elem [74],value[37]
Elem [75],value[36]
Elem [76],value[35]
Elem [77],value[34]
Elem [78],value[33]
Elem [79],value[32]
Elem [80],value[31]
Elem [81],value[30]
Elem [82],value[29]
Elem [83],value[28]
Elem [84],value[27]
Elem [85],value[26]
Elem [86],value[25]
Elem [87],value[24]
Elem [88],value[23]
Elem [89],value[22]
Elem [90],value[21]
Elem [91],value[20]
Elem [92],value[19]
Elem [93],value[18]
Elem [94],value[17]
Elem [95],value[16]
Elem [96],value[15]
Elem [97],value[14]
Elem [98],value[13]
Elem [99],value[12]
Elem [100],value[11]
Elem [101],value[10]
Elem [102],value[9]
Elem [103],value[8]
Elem [104],value[7]
Elem [105],value[6]
Elem [106],value[5]
Elem [107],value[4]
Elem [108],value[3]
Elem [109],value[2]
Elem [110],value[1]
Printf sqlist info end
------------InsertList_Sq end------------
------------ListDelete_Sq start------------
Delete elem[1],value[110]
Delete elem[2],value[108]
Delete elem[3],value[106]
Delete elem[4],value[104]
Delete elem[5],value[102]
Delete elem[6],value[100]
Delete elem[7],value[98]
Delete elem[8],value[96]
Delete elem[9],value[94]
Delete elem[10],value[92]
Delete elem[11],value[90]
Delete elem[12],value[88]
Delete elem[13],value[86]
Delete elem[14],value[84]
Delete elem[15],value[82]
Delete elem[16],value[80]
Delete elem[17],value[78]
Delete elem[18],value[76]
Delete elem[19],value[74]
Delete elem[20],value[72]
Delete elem[21],value[70]
Delete elem[22],value[68]
Delete elem[23],value[66]
Delete elem[24],value[64]
Delete elem[25],value[62]
Delete elem[26],value[60]
Delete elem[27],value[58]
Delete elem[28],value[56]
Delete elem[29],value[54]
Delete elem[30],value[52]
Delete elem[31],value[50]
Delete elem[32],value[48]
Delete elem[33],value[46]
Delete elem[34],value[44]
Delete elem[35],value[42]
Delete elem[36],value[40]
Delete elem[37],value[38]
Delete elem[38],value[36]
Delete elem[39],value[34]
Delete elem[40],value[32]
Delete elem[41],value[30]
Delete elem[42],value[28]
Delete elem[43],value[26]
Delete elem[44],value[24]
Delete elem[45],value[22]
Delete elem[46],value[20]
Delete elem[47],value[18]
Delete elem[48],value[16]
Delete elem[49],value[14]
Delete elem[50],value[12]
Delete elem[51],value[10]
Delete elem[52],value[8]
Delete elem[53],value[6]
Delete elem[54],value[4]
Delete elem[55],value[2]
Printf sqlist info start
SqList info:BaseAddr[18399248],Length[55],MallocSize[110]
Elem [1],value[109]
Elem [2],value[107]
Elem [3],value[105]
Elem [4],value[103]
Elem [5],value[101]
Elem [6],value[99]
Elem [7],value[97]
Elem [8],value[95]
Elem [9],value[93]
Elem [10],value[91]
Elem [11],value[89]
Elem [12],value[87]
Elem [13],value[85]
Elem [14],value[83]
Elem [15],value[81]
Elem [16],value[79]
Elem [17],value[77]
Elem [18],value[75]
Elem [19],value[73]
Elem [20],value[71]
Elem [21],value[69]
Elem [22],value[67]
Elem [23],value[65]
Elem [24],value[63]
Elem [25],value[61]
Elem [26],value[59]
Elem [27],value[57]
Elem [28],value[55]
Elem [29],value[53]
Elem [30],value[51]
Elem [31],value[49]
Elem [32],value[47]
Elem [33],value[45]
Elem [34],value[43]
Elem [35],value[41]
Elem [36],value[39]
Elem [37],value[37]
Elem [38],value[35]
Elem [39],value[33]
Elem [40],value[31]
Elem [41],value[29]
Elem [42],value[27]
Elem [43],value[25]
Elem [44],value[23]
Elem [45],value[21]
Elem [46],value[19]
Elem [47],value[17]
Elem [48],value[15]
Elem [49],value[13]
Elem [50],value[11]
Elem [51],value[9]
Elem [52],value[7]
Elem [53],value[5]
Elem [54],value[3]
Elem [55],value[1]
Printf sqlist info end
------------ListDelete_Sq end------------
------------GetElem_Sq start------------
The location[27] elem is [57]
------------GetElem_Sq end------------
------------LocateElem_Sq start------------
The elem[57] location is [27]
------------LocateElem_Sq end------------
------------PriorElem start------------
The elem[57] preElem is [59]
------------PriorElem end------------
------------NextElem start------------
The elem[57] nextElem is [55]
------------NextElem end------------
------------ListTraverse start------------
Fun visit:the elem value is [109]
Fun visit:the elem value is [107]
Fun visit:the elem value is [105]
Fun visit:the elem value is [103]
Fun visit:the elem value is [101]
Fun visit:the elem value is [99]
Fun visit:the elem value is [97]
Fun visit:the elem value is [95]
Fun visit:the elem value is [93]
Fun visit:the elem value is [91]
Fun visit:the elem value is [89]
Fun visit:the elem value is [87]
Fun visit:the elem value is [85]
Fun visit:the elem value is [83]
Fun visit:the elem value is [81]
Fun visit:the elem value is [79]
Fun visit:the elem value is [77]
Fun visit:the elem value is [75]
Fun visit:the elem value is [73]
Fun visit:the elem value is [71]
Fun visit:the elem value is [69]
Fun visit:the elem value is [67]
Fun visit:the elem value is [65]
Fun visit:the elem value is [63]
Fun visit:the elem value is [61]
Fun visit:the elem value is [59]
Fun visit:the elem value is [57]
Fun visit:the elem value is [55]
Fun visit:the elem value is [53]
Fun visit:the elem value is [51]
Fun visit:the elem value is [49]
Fun visit:the elem value is [47]
Fun visit:the elem value is [45]
Fun visit:the elem value is [43]
Fun visit:the elem value is [41]
Fun visit:the elem value is [39]
Fun visit:the elem value is [37]
Fun visit:the elem value is [35]
Fun visit:the elem value is [33]
Fun visit:the elem value is [31]
Fun visit:the elem value is [29]
Fun visit:the elem value is [27]
Fun visit:the elem value is [25]
Fun visit:the elem value is [23]
Fun visit:the elem value is [21]
Fun visit:the elem value is [19]
Fun visit:the elem value is [17]
Fun visit:the elem value is [15]
Fun visit:the elem value is [13]
Fun visit:the elem value is [11]
Fun visit:the elem value is [9]
Fun visit:the elem value is [7]
Fun visit:the elem value is [5]
Fun visit:the elem value is [3]
Fun visit:the elem value is [1]
------------ListTraverse end------------
------------ClearList_Sq start------------
------------ClearList_Sq end------------
------------ListEmpty_Sq start------------
The list is empty
------------ListEmpty_Sq end------------
------------DestoryList_Sq start------------
------------DestoryList_Sq end------------
------------The main function end------------
8. 의문
어떻게 선형 표 가 존재 하 는 지 판단 합 니까?
함수 형 삼 에 const 수식 을 넣 어야 합 니까?