문자열 목록 의 C 언어 구현:cstrlist

5678 단어 list
이틀 전에 문자열 배열 을 사용 해 야 합 니 다.동적 인 증가 와 감소 가 필요 합 니 다.자바 의 Array List이 제공 하 는 기능 과 비슷 하지만 C 에 기 존 라 이브 러 리 와 함수 가 없어 서 직접 만 들 었 습 니 다.
쓸데없는 말 도 안 하고 친구 가 필요 할 수도 있 으 니까 코드 를 바로 붙 여 주세요.
#include <stdio.h>

#include <stdlib.h>

#include <string.h>



typedef struct{

    unsigned int size;        //      

    char **list;            //              

}st_strlist;



/*

 *           

 *   :strlist_malloc()   strlist_free(st_strlist*)      

 */

st_strlist* strlist_malloc()

{

    st_strlist *strlist = (st_strlist*)malloc(sizeof(st_strlist));

    memset(strlist, 0, sizeof(st_strlist));

    return strlist;

}



/*

 *  strlist            

 */

int strlist_add(st_strlist *strlist, char *str)

{

    int len = strlen(str);

    strlist->size++;

    strlist->list = (char**)realloc(strlist->list, sizeof(char*) * strlist->size);

    if(strlist->list == NULL){

        /* out of memory! */

        printf("error: not enough memory (realloc returned NULL)
"); return -1; } strlist->list[strlist->size-1]=(char*)malloc(len+1); memcpy(strlist->list[strlist->size-1], str, len); (strlist->list[strlist->size-1])[len]=0; return strlist->size; } /* * index str */ int strlist_insertAfter(st_strlist *strlist, char* str, unsigned int index) { int i, len; if(index >= strlist->size){ return -1; } len = strlen(str); strlist->size++; strlist->list = (char**)realloc(strlist->list, sizeof(char*) * strlist->size); if(strlist->list == NULL){ /* out of memory! */ printf("error: not enough memory (realloc returned NULL)
"); return -1; } for(i= strlist->size-1; i>index+1; i--){ strlist->list[i] = strlist->list[i-1]; } strlist->list[index+1] = (char*)malloc(len+1); memcpy(strlist->list[index+1], str, len); strlist->list[index+1][len]=0; return 0; } /* * strlist */ int strlist_removeLast(st_strlist *strlist){ if(strlist->size <=0){ return -1; } free(strlist->list[strlist->size-1]); strlist->size--; return strlist->size; } /* * index */ int strlist_removeAt(st_strlist *strlist, unsigned int index) { int i; if(index >= strlist->size){ return -1; } free(strlist->list[index]); for(i=index; i< strlist->size-1; i++){ strlist->list[i] = strlist->list[i+1]; } strlist->size--; return strlist->size; } /* * strlist index 。 */ char* strlist_getStrAt(st_strlist *strlist, unsigned int index) { if(strlist==NULL || index>=strlist->size){ return NULL; } return strlist->list[index]; } /* * strlist str。 */ int strlist_contains(st_strlist *strlist, char* str) { int i; for(i=0; i<strlist->size; i++){ if(0 == (strcmp(strlist->list[i], str))){ return 1; } } return 0; } /* * */ int strlist_free(st_strlist *strlist) { int i; if(!strlist){ return -1; } for(i=0; i<strlist->size; i++){ free(strlist->list[i]); } free(strlist->list); free(strlist); return 0; } /* * */ void strlist_infolog(st_strlist *strlist) { int i; printf("
>>>
"); printf("strlist info:
"); printf("strlist size: %d
", strlist->size); for(i=0; i<strlist->size; i++){ printf("%d, %s
",i, strlist->list[i]); } } int main(int argc, char *argv[]) { printf("hello c_strlist!"); st_strlist *strlist = strlist_malloc(); strlist_infolog(strlist); strlist_add(strlist,(char*)"aaaa"); strlist_infolog(strlist); strlist_add(strlist,(char*)"bbbb"); strlist_infolog(strlist); strlist_add(strlist,(char*)" "); strlist_infolog(strlist); strlist_insertAfter(strlist, (char*)"ccccc", 1); strlist_infolog(strlist); printf("
is strlist contians \"123\"? %d
", strlist_contains(strlist,(char*)"123")); printf("
is strlist contians \" \" ? %d
", strlist_contains(strlist,(char*)" ")); strlist_removeLast(strlist); strlist_infolog(strlist); printf("
is strlist contians \" \" ? %d
", strlist_contains(strlist,(char*)" ")); strlist_removeAt(strlist,1); strlist_infolog(strlist); strlist_free(strlist); return 0; } /************************ *** : hello c_strlist! >>> strlist info: strlist size: 0 >>> strlist info: strlist size: 1 0, aaaa >>> strlist info: strlist size: 2 0, aaaa 1, bbbb >>> strlist info: strlist size: 3 0, aaaa 1, bbbb 2, >>> strlist info: strlist size: 4 0, aaaa 1, bbbb 2, ccccc 3, is strlist contians "123"? 0 is strlist contians " " ? 1 >>> strlist info: strlist size: 3 0, aaaa 1, bbbb 2, ccccc is strlist contians " " ? 0 >>> strlist info: strlist size: 2 0, aaaa 1, ccccc ************************/

좋은 웹페이지 즐겨찾기