#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <mem.h>
#include <string.h>
/*
customer struct2 -- s2sp
*/
typedef struct
{
char chKey[128];
char chVal[256];
void** pStruct;
}S2SP, *PS2SP;
/*----------------------------------------------------*/
typedef union UVal
{
char c;
int i;
float f;
double d;
long l;
S2SP _s2sp;
}UVAL, *LPUVAL;
typedef struct SNode
{
UVAL uvalType;
int index;
int init;
struct SNode* pNext;
struct SNode* pBefore;
}SNODE, *LPNODE;
/*
InitArray
*/
int InitArray(LPNODE* pNode)
{
int i=0;
LPNODE pTmpNode=NULL;
*pNode=(LPNODE)malloc(sizeof(SNODE));
(*pNode)->pBefore=NULL;
(*pNode)->pNext=NULL;
(*pNode)->index=0;
(*pNode)->init=-1;
pTmpNode=*pNode;
return pTmpNode->index;
}
/*
Free
*/
void FreeArray(LPNODE* pNode)
{
LPNODE pTmpEnd=NULL;
/*Find End Node*/
while((*pNode)->pNext)
{
(*pNode)=(*pNode)->pNext;
}
/*Free Node until Head Node*/
while((*pNode)->pBefore)
{
pTmpEnd=(*pNode);
(*pNode)=(*pNode)->pBefore;
free(pTmpEnd);
}
/*Free Head Node*/
free(*pNode);
return;
}
/*
Find
if found, then return Node address in LPNODE* pNode, return val is 0
if not found, return val is -1
*/
int FindInArray(LPNODE* pRoot, int index, LPNODE* pNode)
{
int ret=-1;
LPNODE pTmp=(*pRoot);
if(index<0)
return ret;
while(1)
{
if(pTmp->pNext==NULL)/*End Node*/
{
if(pTmp->index==index)
{
*pNode=pTmp;
ret=0;
break;
}
else
return ret;
}
else/*Not End Node*/
{
if(pTmp->index == index)
{
*pNode=pTmp;
ret=0;
break;
}
else
pTmp=pTmp->pNext;
}
}
return ret;
}
/*
Find
if found, then return Node address in LPNODE* pNode, return val is 0
if not found, return val is -1
*/
int FindStrInArray(LPNODE* pRoot, char* pstr, LPNODE* pNode)
{
int ret=-1;
LPNODE pTmp=(*pRoot);
while(1)
{
if(pTmp->pNext==NULL)/*End Node*/
{
if(!strcmp(pTmp->uvalType._s2sp.chKey, pstr))
{
*pNode=pTmp;
ret=0;
break;
}
else
return ret;
}
else/*Not End Node*/
{
if(!strcmp(pTmp->uvalType._s2sp.chKey, pstr))
{
*pNode=pTmp;
ret=0;
break;
}
else
pTmp=pTmp->pNext;
}
}
return ret;
}
/*
Add a item
*/
int AppendItem(LPNODE* pRoot, SNODE sNode)
{
LPNODE pTmpEnd=*pRoot;
LPNODE pNewItem=NULL;
/*Find End Node*/
while(pTmpEnd->pNext || (pTmpEnd->init!=-1))
{
if (pTmpEnd->pNext)
pTmpEnd=pTmpEnd->pNext;
else
break;
}
/*Check 'init' mark if init not ok then transport value to headNode*/
if ((!pTmpEnd->index) && (pTmpEnd->init==-1))
{
memcpy(&pTmpEnd->uvalType, &sNode.uvalType, sizeof(UVAL));
pTmpEnd->init=0;
return pTmpEnd->index;
}
pNewItem=(LPNODE)malloc(sizeof(SNODE));
pNewItem->index=pTmpEnd->index+1;
pNewItem->pBefore=pTmpEnd;
pNewItem->pNext=NULL;
memcpy(&pNewItem->uvalType, &sNode.uvalType, sizeof(UVAL));
pTmpEnd->pNext=pNewItem;
return pNewItem->index;
}
/*
Get a item use index number
*/
SNODE GetItem(LPNODE* pRoot, int index)
{
SNODE ret;
LPNODE pNode=NULL;
FindInArray(&*pRoot, index, &pNode);
ret.index=pNode->index;
ret.pBefore=pNode->pBefore;
ret.pNext=pNode->pNext;
memcpy(&ret, &pNode->uvalType, sizeof(UVAL));
return ret;
}
/*
Get Size
*/
int GetArrSize(LPNODE* pRoot)
{
LPNODE pTmpEnd=(*pRoot);
/*Find End Node*/
while(pTmpEnd->pNext)
{
pTmpEnd=pTmpEnd->pNext;
}
return pTmpEnd->index + 1;
}
/*使用方法如下范例*/
int main(int argc, char* argv[])
{
LPNODE pRoot = NULL;
InitArray(&pRoot);
SNODE s;
s.uvalType.c = 'c';
for (int i = 0; i < 5; i++)
{
AppendItem(&pRoot, s);
}
int ii = GetArrSize(&pRoot);
for (int j = 0; j < ii; j++)
{
printf("%d\n", GetItem(&pRoot, j).index);
}
printf("%d",GetArrSize(&pRoot));
FreeArray(&pRoot);
return 0;
}