废话不多说直接上代码
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
typedef struct D_List
{
char* _array;
size_t _size;
size_t _capicity;
}D_List;
void InitSqList(D_List &List_addr,size_t L_size)
{
List_addr._array=(char *)malloc(sizeof(char)*L_size);
memset(List_addr._array,'\0',L_size);
if(NULL==List_addr._array)
{
printf("Malloc List error");
return;
}
List_addr._size=0;
List_addr._capicity= L_size;
}
void setData(unsigned int Index,char Data,D_List &List_addr)//设置数据
{
List_addr._size=List_addr._size+1;//数据量+1
*(List_addr._array+Index)=Data;//下标赋值
}
void LocateElem(D_List &List_addr, int &Curr_Index ,char Elem_Data)//找到下标
{
int i=0;
char* p = List_addr._array;
while(NULL!=p && i<List_addr._capicity && *(p+i)!=Elem_Data)
{
i++;
}
if(*(p+i)==Elem_Data)
{
Curr_Index=i;
return;
}
Curr_Index=-1;
return;
}
void ListInsert(D_List &_list_addr,int Curr_Index,char Insert_Elem_Data)
{
char* Befor_P=_list_addr._array;
size_t Befor_Size=_list_addr._size;
size_t Befor_capicity=_list_addr._capicity;
InitSqList(_list_addr,Befor_capicity+1);//新内存
strcpy_s(_list_addr._array,Befor_Size+1,Befor_P);
_list_addr._capicity=Befor_capicity+1;
_list_addr._size=Befor_Size;
int i=(Befor_capicity-Curr_Index-1);//最大迭代
while(Curr_Index+i<Befor_capicity
&& NULL != ((Befor_P+Befor_capicity)-i)
&& i >= 0)
{
*(_list_addr._array+Curr_Index+i+1) = *(Befor_P+Curr_Index+i);
i--;
}
if(i>=0)
{
return;
}
if(NULL==Befor_P+Befor_capicity-i)
{
return;
}
free(Befor_P);
Befor_P=NULL;
*(_list_addr._array+Curr_Index)=Insert_Elem_Data;
}
void ListPrintf(D_List &List_addr)
{
int Formax = List_addr._capicity;
int i = 0;
for(i;i<Formax;i++)
{
printf("Index is %d,Data is :%c\n",i,*(List_addr._array+i));
}
printf("Printf End\n");
}
void ListRemoveElem(D_List &_List_addr,char Elem)
{
int index;
char* p = _List_addr._array;
size_t Befor_capicity=_List_addr._capicity;
size_t Befor_size=_List_addr._size;
LocateElem(_List_addr,index,Elem);
int i=0;
while(index+i<Befor_capicity)
{
*(p+index+i) = *(p+index+i+1);
i++;
}
InitSqList(_List_addr,Befor_capicity);
_List_addr._capicity=Befor_capicity-1;
// strcpy_s(_List_addr._array,strlen(p)+1,p);
strcpy(_List_addr._array,p);
}
void ListRemoveIndex(D_List &_list_addr,int index)
{
}
int main()
{
D_List MStruct;
int Index;
InitSqList(MStruct,10);
setData(0,'a',MStruct);
setData(1,'b',MStruct);
setData(2,'c',MStruct);
setData(3,'d',MStruct);
setData(4,'e',MStruct);
setData(5,'f',MStruct);
setData(6,'g',MStruct);
setData(7,'h',MStruct);
setData(8,'i',MStruct);
setData(9,'j',MStruct);
ListPrintf(MStruct);
LocateElem(MStruct,Index,'a');
printf("locateIndex is %d\n\n",Index);
ListInsert(MStruct,9,'-');
LocateElem(MStruct,Index,'a');
ListPrintf(MStruct);
ListRemoveElem(MStruct,'a');
ListPrintf(MStruct);
printf("locateIndex is %d\n\n",Index);
}
顺带提一句 Char*在C语言中很多函数都是采用默认 char* 代表String。printf,所以在使用C的有些函数的时候需要注意:
char* p="hello world";
char a = *p;
printf("%c",&a);
printf("%s",p);
printf("%c",*p)
---------------------
int* p=10;
int a = *p;
printf("%d",a);
printf("%d",*p);
在上面的两种操作中其实都是一样的操作,
上面的printf("%c",&a)和printf("%c",*p)结果是一样的,printf("%s",p);因为默认的Printf()的参数带有char* 这种传递类型,所以会出现连续打印;