ArrayList를 이용하여 List에 데이터를 넣었다가 빼고, 리스트에 존재하는지 등에 대한 Function들을 작성해보았습니다.


(1) arraylistmain.c

#include <stdio.h>

#include <stdlib.h>

#include "arraylist.h"


int main(int argc, char *argv[]) {

int i = 0, arrayCount = 0;


ArrayList *pList = NULL;

ArrayListNode *pValue = NULL;


pList = createArrayList(6);


if (pList != NULL) {

ArrayListNode node;

node.data = 1;

addALElement(pList, 0, node);

node.data = 3;

addALElement(pList, 1, node);

node.data = 5;

addALElement(pList, 2, node);


displayArrayList(pList);


removeALElement(pList, 0);


displayArrayList(pList);


arrayCount = getArrayListLength(pList);


for (i = 0; i < arrayCount; i++) {

pValue = getALElement(pList, i);

printf("ArrayList[%d]-%d\n", i, pValue->data);

}

deleteArrayList(pList);

}

return 0;

}



(2)arraylist.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "arraylist.h"


ArrayList* createArrayList(int maxElementCount) {

ArrayList *pReturn = NULL;

int i = 0;


if (maxElementCount > 0) {

pReturn = (ArrayList *)malloc(sizeof(ArrayList));


if (pReturn != NULL) {

pReturn->maxElementCount = maxElementCount;

pReturn->currentElementCount = 0;

pReturn->pElement = NULL;

}


else {

printf("오류, 메모리할당 createArrayList() \n");

return NULL;

}

}


else {

printf("오류, 최대 원소 개수는 0이상이어야 합니다.\n");

return NULL;

}


pReturn->pElement = (ArrayListNode *)malloc(sizeof(ArrayListNode)*maxElementCount);


if (pReturn->pElement == NULL) {

printf("오류, 2번째 메모리 할당 crateArrayList() \n");

free(pReturn); return NULL;

}

memset(pReturn->pElement, 0, sizeof(ArrayListNode)*maxElementCount);


return pReturn;

}


int addALElement(ArrayList* pList, int position, ArrayListNode element) {

int ret = FALSE;

int i = 0;


if (pList != NULL) {

if (isArrayListFull(pList) != TRUE) {

if (position >= 0 && position <= pList->currentElementCount) {

for (i = pList->currentElementCount - 1; i >= position; i--) {

pList->pElement[i + 1] = pList->pElement[i];

}


pList->pElement[position] = element;


pList->currentElementCount++;


ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d] 범위초과, addALElement()\n", position);

}

}


else {

printf("오류, 리스트 용량초과-[%d]/[%d]\n", position, pList->maxElementCount);

}

}


return ret;

}


int removeALElement(ArrayList* pList, int position) {

int ret = FALSE;

int i = 0;


if (pList != NULL) {

if (position >= 0 && position < pList->currentElementCount) {

for (i = position; i < pList->currentElementCount - 1; i++) {

pList->pElement[i] = pList->pElement[i + 1];

}


pList->currentElementCount--;

ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d] 범위 초과, removeALElement()\n", position);

}

}


return ret;

}


ArrayListNode* getALElement(ArrayList* pList, int position) {

ArrayListNode* pReturn = NULL;

if (pList != NULL) {

if (position >= 0 && position < getArrayListLength(pList)) {

pReturn = &(pList->pElement[position]);

}

else {

printf("오류, 위치 인덱스-[%d] 범위 초과, getTALElement() \n", position);

}

}


return pReturn;

}


void displayArrayList(ArrayList* pList) {

int i = 0;

if (pList != NULL) {

printf("최대 원소 개수 : %d\n", pList->maxElementCount);

printf("현재 원소 개수 : %d\n", pList->currentElementCount);


for (i = 0; i < pList->currentElementCount; i++) {

printf("[%d], %d\n", i, getALElement(pList, i)->data);

}


i = pList->currentElementCount;

for (; i < pList->maxElementCount; i++) {

printf("[%d], Empty\n", i);

}

}

else {

printf("ArrayList is NULL");

}

}


int isArrayListFull(ArrayList* pList) {

int ret = FALSE;


if (pList != NULL) {

if (pList->currentElementCount == pList->maxElementCount) {

ret = TRUE;

}

}

return ret;

}


int getArrayListLength(ArrayList* pList) {

int ret = 0;

if (pList != NULL) {

ret = pList->currentElementCount;

}

return ret;

}


void deleteArrayList(ArrayList* pList) {

int i = 0;

if (pList != NULL) {

free(pList->pElement);

free(pList);

}

}


(3)arraylist.h    (Header File)

#ifndef _ARRAYLIST_

#define _ARRAYLIST_


typedef struct ArrayListNodeType

{

int data;

} ArrayListNode;


typedef struct ArrayListType

{

int maxElementCount;

int currentElementCount;

ArrayListNode *pElement;

} ArrayList;


ArrayList* createArrayList(int maxElementCount);

int addALElement(ArrayList* pList, int position, ArrayListNode element);

int removeALElement(ArrayList* pList, int position);

ArrayListNode* getALElement(ArrayList* pList, int position);

void displayArrayList(ArrayList* pList);

int isArrayListFull(ArrayList* pList);

int getArrayListLength(ArrayList* pList);

void deleteArrayList(ArrayList* pList);


#endif


#ifndef _COMMON_LIST_DEF_

#define _COMMON_LIST_DEF_


#define TRUE 1

#define FALSE 0


#endif


'Programming > C' 카테고리의 다른 글

(5) DoubleLinkedList 사용하기  (0) 2016.10.30
(4) Linkedlist 를 이용하여 Reverse, Concat 응용  (0) 2016.10.30
(3) Linkedlist 사용하기  (0) 2016.10.30
(2) ArrayStack 사용하기  (0) 2016.10.30
C언어란?  (0) 2016.04.18

WRITTEN BY
SiriusJ

,