#include"LNode.h"
LNode *Creat_List(int NodeNum)
{
LNode *pHead=NULL;
LNode *p1,*p2;
p1=p2=(LNode *)malloc(sizeof(LNode));
if(p1==NULL)
{
printf ("Malloc Error!\n");
return NULL;
}
memset(p1,0,sizeof(LNode));
p1->Data=(char *)malloc(LENGTH_DATA*sizeof(char));
if(p1->Data==NULL)
{
printf ("Malloc Error!\n");
return NULL;
}
pHead=p1;
int i=0;
for (;i<NodeNum-1;i++)
{
p1=(LNode *)malloc(sizeof(LNode));
if(p1==NULL)
{
printf ("Malloc Error!\n");
return NULL;
}
memset(p1,0,sizeof(LNode));
p1->Data=(char *)malloc(LENGTH_DATA*sizeof(char));
if(p1->Data==NULL)
{
printf ("Malloc Error!\n");
return NULL;
}
p2->next=p1;
p2=p1;
}
p1->next=NULL;
return pHead;
}
Status Show_List(LNode *L,char *Message)
{
if(L==NULL)
{
return ERROR;
}
LNode *pL=L;
int i=1;
printf("-------------------After %s---Show List-----------------------\n",Message);
while(pL)
{
printf("[%d]Data:%s\n",i,pL->Data);
pL=pL->next;
i++;
}
printf("-----------------------------------------------------------\n");
return OK;
}
Status Free_List(LNode *L)
{
if(L==NULL)
{
return ERROR;
}
LNode *pHead=L;
LNode *pNext=pHead->next;
while(pHead->next!=NULL)
{
pHead->next=pNext->next;
if(pNext->Data!=NULL){
free(pNext->Data);
pNext->Data=NULL;
}
if(pNext!=NULL){
free(pNext);
}
pNext=pHead->next;
}
if(pHead->next==NULL)
{
if(pHead->Data!=NULL){
free(pHead->Data);
pHead->Data=NULL;
}
if(pHead!=NULL){
free(pHead);
pHead=NULL;
}
}
return OK;
}//General Method
void Destroy_List(LNode*L)
{
if(L!=NULL)
{
Destroy_List(L->next);
if(L->Data!=NULL){
free(L->Data);
}
if(L){
free(L);
}
L=NULL;
}
}// Recursion
Status Equal_Node(LNode *L,int iPos,const char *src,int iDatalen)
{
//iDatalen is the length of L->Data
if(L==NULL||iPos<1)
{
return ERROR;
}
int i=1;
LNode *pL=L;
while(pL&&i<iPos)
{
pL=pL->next;
i++;
}
if(L==NULL&&i<iPos)
{
return OVERFLOW;
}
int length=(iDatalen>=strlen(src)?strlen(src):iDatalen);
strncpy(pL->Data,src,length);
return OK;
}
Status GetData_Node(LNode *L,int iPos,char *dst)
{
if(L==NULL||iPos<1)
{
return ERROR;
}
LNode *pL=L;
int i=1;
while(pL!=NULL&&i<iPos)
{
pL=pL->next;
i++;
}
if(L==NULL&&i<iPos)
{
return OVERFLOW;
}
strcpy(dst,pL->Data);
return OK;
}
Status Insert_Node(LNode **L,int iPos,const char * str,int len)
{
if(L==NULL||iPos<1)
{
return ERROR;
}
LNode *pL=*L;
LNode *pNew=NULL;
LNode *pNext=NULL;
pNew=(LNode *)malloc(sizeof(LNode));
if(pNew==NULL)
{
printf("Creat NewNode Error!");
return OVERFLOW;
}
pNew->Data=(char *)malloc(sizeof(char)*LENGTH_DATA);
if(pNew->Data==NULL)
{
printf("Creat NewNode_Data Error!");
return OVERFLOW;
}
int length=(len>LENGTH_DATA?LENGTH_DATA:len);
strncpy(pNew->Data,str,length);
int i=1;
if(iPos=1)
{
*L=pNew;
pNew->next=pL;
}
else
{
while(pL&&i<iPos-1)
{
pL=pL->next;
i++;
}//Get the previous Node of pointed Node;
if(pL==NULL&&i<iPos-1)
{
return ERROR;
}
if(pL->next==NULL)
{
return ERROR;
}
pNext=pL->next;
pL->next=pNew;
pNew->next=pNext;
}
return OK;
}//Insert NewNode before pointed_node
Status Delete_Node(LNode **L,int iPos)
{
if(L==NULL||iPos<1)
{
return ERROR;
}
int i=1;
LNode *pL=*L;
LNode *pNext,*pPre;
if(iPos==1)
{
*L=pL->next;
if(pL){
free(pL->Data);
free(pL);
}
}
else
{
while(pL&&i<iPos)
{
pPre=pL;
pL=pL->next;
if(pL)
{
pNext=pL->next;
}
else
{
pNext=NULL;
}
i++;
}
if(pL==NULL&&i<iPos)
{
return ERROR;
}
pPre->next=pNext;
if(pL){
free(pL->Data);
free(pL);
}
}
return OK;
}//Delete the pointed node
Status LocateData_Node(LNode *L,const char *str)
{
if (!L || !str)
{
return ERROR;
}
LNode *pL=L;
while(pL)
{
if(0==strncmp(pL->Data,str,strlen(str)))
{
return OK;
}
pL=pL->next;
}
return ERROR;
}//Find Data
int
main(void)
{
LNode *pList=NULL;
pList=Creat_List(5);
char ShowBuf[128]={0};
if(pList!=NULL)
{
printf("Creat_List Success!\n");
}
Equal_Node(pList,1,"FirstNode",LENGTH_DATA);
GetData_Node(pList,1,ShowBuf);
printf("Data:%s\n",ShowBuf);
Equal_Node(pList,2,"SecondNode",LENGTH_DATA);
Equal_Node(pList,3,"ThirdNode",LENGTH_DATA);
Equal_Node(pList,4,"ForthNode",LENGTH_DATA);
Equal_Node(pList,5,"FifthNode",LENGTH_DATA);
Show_List(pList,"Equal");
Insert_Node(&pList,1,"NewFifthNode",strlen("NewForthNode"));
Show_List(pList,"Insert");
Delete_Node(&pList,1);
Show_List(pList,"Delete");
if(OK==LocateData_Node(pList,"ThirdNode"))
{
printf("find Data!\n");
}
#if 0
if(Free_List(pList)==OK)
{
printf("Free_List Success!\n");
}
return 0;
#else
Destroy_List(pList);
#endif
}
|