↘ [C]/↘ [자료구조]

[C/자료구조] 배열로 구현한 리스트

↘Hwibin.com 2013. 4. 16. 11:02
   1: // arraylish.h
   2: typedef struct ArrayNodeType
   3: {
   4:     int data;
   5: }ArrayListNode;
   6:  
   7: typedef struct ArrayListType
   8: {
   9:     int maxElementCount;        // 최대 원소 개수
  10:     int currentElementCount;    // 현재 원소의 개수
  11:     ArrayListNode *pElement;    // 원소 저장을 위한 1차원 배열
  12: }ArrayList;
  13:  
  14: ArrayList*  createArrayList(int maxElementCount);
  15: void        deleteArrayList(ArrayList *pList);
  16: int         isArrayListFull(ArrayList* pList);
  17: int         addALElement   (ArrayList *pList, int position, ArrayListNode element);
  18: int         removeALElement(ArrayList* pList, int position);
  19: ArrayListNode* getALElement(ArrayList* pList, int position);
  20: void displayArrayList      (ArrayList* pList);
  21: void clearArrayList        (ArrayList* pList);
  22: int         getArrayListLength(ArrayList* pList);
  23:  
  24: #define TRUE        1
  25: #define FALSE       2
  26:  
  27: // arraylist.c
  28:  
  29: #include <stdio.h>
  30: #include <stdlib.h>
  31: #include <string.h>
  32:  
  33: ArrayList*  createArrayList(int maxElementCount)
  34: {
  35:     ArrayList *pReturn = NULL;
  36:     int i = 0;
  37:  
  38:     if(maxElementCount > 0)     // 입력 파라미터 유효성 점검
  39:     {
  40:         pReturn = (ArrayList *)malloc(sizeof(ArrayList));       // 메모리 할당
  41:  
  42:         if(pReturn != NULL)
  43:         {
  44:             pReturn->maxElementCount = maxElementCount;
  45:             pReturn->currentElementCount = 0;
  46:             pReturn->pElement = NULL;
  47:         }
  48:         else
  49:         {
  50:             printf("오류, 메모리할당실패 createArrayList()\n");
  51:             return NULL;
  52:         }
  53:     }
  54:  
  55:     pReturn->pElement = (ArrayListNode*)malloc(sizeof(ArrayListNode)*maxElementCount);
  56:  
  57:     if(pReturn->pElement==NULL)
  58:     {
  59:         printf("오류, 2번째 메모리할당 createArrayList()\n");
  60:         free(pReturn);
  61:         return NULL;
  62:     }
  63:     memset(pReturn->pElement, 0, sizeof(ArrayListNode) * maxElementCount);
  64:  
  65:     return pReturn;
  66: }
  67:  
  68: int addALElement(ArrayList *pList, int position, ArrayListNode element)
  69: {
  70:     int ret = FALSE;
  71:     int i=0;
  72:  
  73:     if(pList != NULL)
  74:     {
  75:         if(isArrayListFull(pList) != TRUE)
  76:         {
  77:             if(position >= 0 && position <= pList->currentElementCount)
  78:             {
  79:                 for(i= pList->currentElementCount-1; i>=position; i--)
  80:                 {
  81:                     pList->pElement[i+1] = pList->pElement[i];
  82:                 }
  83:                 pList->pElement[position] = element;
  84:                 pList->currentElementCount++;
  85:  
  86:                 ret = TRUE;
  87:             }
  88:             else
  89:             {
  90:                 printf("오류, 위치 인덱스-[%d] 범위 초과, addALElement()\n", position);
  91:             }
  92:         }
  93:         else
  94:         {
  95:             printf("오류, 리스트 용량 초과-[%d]/[%d]\n", position, pList->maxElementCount);
  96:         }
  97:     }
  98:  
  99:     return ret;
 100: }
 101:  
 102: int removeALElement(ArrayList *pList, int position)
 103: {
 104:     int ret = FALSE;
 105:     int i=0;
 106:  
 107:     if(pList != NULL)
 108:     {
 109:         if(position >= 0 && position < pList->currentElementCount)
 110:         {
 111:             for(i = position; i<pList->currentElementCount-1; i++)
 112:             {
 113:                 pList->pElement[i] = pList->pElement[i+1];
 114:             }
 115:  
 116:             pList->currentElementCount--;
 117:             ret = TRUE;
 118:         }
 119:         else
 120:         {
 121:             printf("오류, 위치 인덱스-[%d] 범위 초과, removeALElement()\n", position);
 122:         }
 123:     }
 124:  
 125:     return ret;
 126: }
 127:  
 128: ArrayListNode* getALElement(ArrayList* pList, int position)
 129: {
 130:     ArrayListNode *pReturn = NULL;
 131:     if(pList != NULL)
 132:     {
 133:         if(position >=0 && position < getArrayListLength(pList))
 134:         {
 135:             pReturn = &(pList->pElement[position]);
 136:         }
 137:         else
 138:         {
 139:             printf("오류 위치 인덱스-[%d] 범위 초과, getALElement()\n",position);
 140:         }
 141:     }
 142:  
 143:     return pReturn;
 144: }
 145:  
 146: void displayArrayList(ArrayList *pList)
 147: {
 148:     int i=0;
 149:     if(pList != NULL)
 150:     {
 151:         printf("최대 원소 개수: %d\n",pList->maxElementCount);
 152:         printf("현재 원소 개수: %d\n",pList->currentElementCount);
 153:  
 154:         for(i=0; i<pList->currentElementCount; i++)
 155:         {
 156:             printf("[%d,%d\n\n", i, getALElement(pList, i)->data);
 157:         }
 158:  
 159:         i=pList->currentElementCount;
 160:         for(;i<pList->currentElementCount;i++)
 161:         {
 162:             printf("[%d],Empty\n", i);
 163:         }
 164:     }
 165:  
 166:     else
 167:     {
 168:         printf("ArrayList is NULL");
 169:     }
 170: }
 171:  
 172: int isArrayListFull(ArrayList* pList)
 173: {
 174:     int ret = FALSE;
 175:  
 176:     if(pList != NULL)
 177:     {
 178:         if(pList->currentElementCount == pList->maxElementCount)
 179:         {
 180:             ret = TRUE;
 181:         }
 182:     }
 183:  
 184:     return ret;
 185: }
 186:  
 187: int getArrayListLength(ArrayList *pList)
 188: {
 189:     int ret=0;
 190:  
 191:     if(pList!=NULL)
 192:         ret = pList->currentElementCount;
 193:  
 194:     return ret;
 195: }
 196:  
 197: void deleteArrayList(ArrayList* pList)
 198: {
 199:     int i=0;
 200:     if(pList != NULL)
 201:     {
 202:         free(pList->pElement);
 203:         free(pList);
 204:     }
 205: }
 206:  
 207: /*int addALElementFirst(ArrayList* pList, int element)
 208: {
 209:     int position =0;
 210:     return addALElement(pList, position, (ArrayListNode)element);
 211: }
 212: 
 213: int addALElementLast(ArrayList* pList, int element)
 214: {
 215:     int ret = FALSE;
 216:     int position =0;
 217:     if(pList != NULL)
 218:     {
 219:         position = getArrayListLength(pList);
 220:         ret = addALElement(pList, position, element);
 221:     }
 222: 
 223:     return ret;
 224: }*/
 225:  
 226: // example03_01.c
 227: int main(int argc, char **argv[])
 228: {
 229:     int i=0;
 230:     int arrayCount =0;
 231:     ArrayList *pList=NULL;
 232:     ArrayListNode *pValue = NULL;
 233:  
 234:     pList = createArrayList(6);
 235:     if(pList!=NULL)
 236:     {
 237:         ArrayListNode node;
 238:  
 239:         //리스트 초기화: 1, 3, 5 추가
 240:         node.data = 1;
 241:         addALElement(pList, 0, node);
 242:  
 243:         node.data = 3;
 244:         addALElement(pList, 1, node);
 245:  
 246:         node.data = 5;
 247:         addALElement(pList, 2, node);
 248:         displayArrayList(pList);
 249:  
 250:         //첫번째 원소 제거
 251:         removeALElement(pList, 0);
 252:         displayArrayList(pList);
 253:  
 254:         arrayCount = getArrayListLength(pList);
 255:  
 256:         for(i=0; i<arrayCount; i++)
 257:         {
 258:             pValue = getALElement(pList, i);
 259:             printf("위치[%d-%d\n", i, pValue->data);
 260:         }
 261:  
 262:         deleteArrayList(pList);
 263:     }
 264:  
 265:     return 0;
 266: }