#include "LineTable.h"
Status ShowList_Sq(SqList *L,char *Message)
{
if(L->elem==NULL)
{
return ERROR;
}
printf("-------------------%s Start------------------------:\n",Message);
printf("\tData:%s\n",L->elem);
printf("\tLen:%d\n",L->length);
printf("\tSize:%d\n",L->listsize);
printf("-------------------%s End------------------------:\n",Message);
return TRUE;
}//Show
Status InitList_Sq(SqList **L)
{
(*L)->elem=(char *)malloc(LIST_INIT_SIZE*sizeof(char));
if((*L)->elem==NULL)
{
return OVERFLOW;
}
(*L)->length=0;
(*L)->listsize=LIST_INIT_SIZE;
memset((*L)->elem,0,(*L)->listsize);
printf("Init OK!\n");
return OK;
}//Init
Status EqualList_Sq(SqList *L,char *s)
{
int len=strlen(s);
if(len > L->listsize-1)
{
return ERROR;
}
strncpy(L->elem,s,len);
L->length=len+1;
return OK;
}//Equal
Status InsertList_Sq(SqList **L,int pos, char e)
{
if(pos<1||pos>(*L)->length+1)
{
return ERROR;
}
if((*L)->length>=(*L)->listsize)
{
printf("Need Realloc!\n");
(*L)->elem=(char*)realloc((*L)->elem,((*L)->listsize+LISTINCREMENT)*sizeof(char));
if((*L)->elem==NULL)
{
return OVERFLOW;
}
}
char *pPos=&((*L)->elem[pos-1]);
char *pEnd=&((*L)->elem[(*L)->length-1]);
for(;pEnd>=pPos;pEnd--)
*(pEnd+1)=*pEnd;
*pPos=e;
++(*L)->length;
(*L)->listsize+=LISTINCREMENT;
return OK;
}//Insert
Status DeletList_Sq(SqList *L,int pos,char *e)
{
if(pos<1||pos>L->length+1)
{
return ERROR;
}
char *p=&(L->elem[pos-1]);
char *q=&(L->elem[L->length-1]);
*e=*p;
for(;p<=q;p++)
*p=*(p+1);
L->length--;
}//Delete
Status FindElem_Sq(SqList *L,char e)
{
if(L->elem==NULL)
{
return ERROR;
}
char *p=L->elem;
while(*p++)
{
if(*p==e)
{
return OK;
}
}
return FALSE;
}//FindElem
Status Append_Sq(SqList **L,SqList *p)
{
if((*L)->elem==NULL||p->elem==NULL)
{
return ERROR;
}
if((*L)->length + p->length>(*L)->listsize)
{
printf("Memory is not enough!\n");
(*L)->elem=(char *)realloc((*L)->elem,(((*L)->length + p->length -1 +LISTINCREMENT)*sizeof(char)));
}
char *pL=(*L)->elem;
char *pp=p->elem;
while(*pL++);
pL--;
while(*pL++=*pp++);
(*L)->length=(*L)->length + p->length-1;
(*L)->listsize=(*L)->length+LISTINCREMENT-1;
return OK;
}//Union
Status Union_Sq(SqList **L,SqList*p)
{
if((*L)->elem==NULL||p->elem==NULL)
{
return ERROR;
}
if((*L)->length + p->length>(*L)->listsize)
{
printf("Memory is not enough!\n");
(*L)->elem=(char *)realloc((*L)->elem,(((*L)->length + p->length -1 +LISTINCREMENT)*sizeof(char)));
}
int i=0;
char *pp=p->elem;
for(;i<p->length-1;i++,pp++)
{
if(FALSE==FindElem_Sq(*L,*pp))
{
printf("Insert[%c]\n",*pp);
InsertList_Sq(L,(*L)->length,*pp);
}
}
}//Union
int
main(void)
{
SqList Array,*pArray=&Array;
SqList Src,*pSrc=&Src;
char c;
char e='3';
if((InitList_Sq(&pArray)!=OK)||(InitList_Sq(&pSrc)!=OK))
{
printf("Init Error!\n");
return ERROR;
}
EqualList_Sq(pArray,"a234bcdefgh");
EqualList_Sq(pSrc,"tomwht");
ShowList_Sq(pArray,"Init");
InsertList_Sq(&pArray,4, e);
ShowList_Sq(pArray,"Insert");
DeletList_Sq(pArray,5,&c);
ShowList_Sq(pArray,"Delete");
if(TRUE==FindElem_Sq(pArray,e))
{
printf("[%c] is found!\n",e);
}
Append_Sq(&pArray,pSrc);
ShowList_Sq(pArray,"Append");
// Union_Sq(&pArray,pSrc);
// ShowList_Sq(pArray,"Union");
free(pArray->elem);
pArray->elem=NULL;
free(pSrc->elem);
pSrc->elem=NULL;
return OK;
|