fork download
  1. #include <stdio.h>
  2.  
  3. #ifndef __ARRAY_LIST_H__
  4. #define __ARRAY_LIST_H__
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. /*** ArrayList의 정의 ****/
  10. #define LIST_LEN 100
  11. typedef int LData;
  12.  
  13. typedef struct __ArrayList
  14. {
  15. LData arr[LIST_LEN];
  16. int numOfData;
  17. int curPosition;
  18. } ArrayList;
  19.  
  20.  
  21. /*** ArrayList와 관련된 연산들 ****/
  22. typedef ArrayList List;
  23.  
  24. void ListInit(List * plist);
  25. void LInsert(List * plist, LData data);
  26.  
  27. int LFirst(List * plist, LData * pdata);
  28. int LNext(List * plist, LData * pdata);
  29.  
  30. LData LRemove(List * plist);
  31. int LCount(List * plist);
  32.  
  33. #endif
  34.  
  35. void ListInit(List * plist)
  36. {
  37. (plist->numOfData) = 0;
  38. (plist->curPosition) = -1;
  39. }
  40.  
  41. void LInsert(List * plist, LData data)
  42. {
  43. if(plist->numOfData > LIST_LEN)
  44. {
  45. puts("저장이 불가능합니다.");
  46. return;
  47. }
  48.  
  49. plist->arr[plist->numOfData] = data;
  50. (plist->numOfData)++;
  51. }
  52.  
  53. int LFirst(List * plist, LData * pdata)
  54. {
  55. if(plist->numOfData == 0)
  56. return FALSE;
  57.  
  58. (plist->curPosition) = 0;
  59. *pdata = plist->arr[0];
  60. return TRUE;
  61. }
  62.  
  63. int LNext(List * plist, LData * pdata)
  64. {
  65. if(plist->curPosition >= (plist->numOfData)-1)
  66. return FALSE;
  67.  
  68. (plist->curPosition)++;
  69. *pdata = plist->arr[plist->curPosition];
  70. return TRUE;
  71. }
  72.  
  73. LData LRemove(List * plist)
  74. {
  75. int rpos = plist->curPosition;
  76. int num = plist->numOfData;
  77. int i;
  78. LData rdata = plist->arr[rpos];
  79.  
  80. for(i=rpos; i<num-1; i++)
  81. plist->arr[i] = plist->arr[i+1];
  82.  
  83. (plist->numOfData)--;
  84. (plist->curPosition)--;
  85. return rdata;
  86. }
  87.  
  88. int LCount(List * plist)
  89. {
  90. return plist->numOfData;
  91. }
  92.  
  93. int main(void)
  94. {
  95. /*** ArrayList의 생성 및 초기화 ***/
  96. List list;
  97. int data;
  98. ListInit(&list);
  99.  
  100. /*** 5개의 데이터 저장 ***/
  101. LInsert(&list, 11); LInsert(&list, 11);
  102. LInsert(&list, 22); LInsert(&list, 22);
  103. LInsert(&list, 33);
  104.  
  105. /*** 저장된 데이터의 전체 출력 ***/
  106. printf("현재 데이터의 수: %d \n", LCount(&list));
  107.  
  108. if(LFirst(&list, &data)) // 첫 번째 데이터 조회
  109. {
  110. printf("%d ", data);
  111.  
  112. while(LNext(&list, &data)) // 두 번째 이후의 데이터 조회
  113. printf("%d ", data);
  114. }
  115. printf("\n\n");
  116.  
  117. /*** 숫자 22을 탐색하여 모두 삭제 ***/
  118. if(LFirst(&list, &data))
  119. {
  120. if(data == 22)
  121. LRemove(&list);
  122.  
  123. while(LNext(&list, &data))
  124. {
  125. if(data == 22)
  126. LRemove(&list);
  127. }
  128. }
  129.  
  130. /*** 삭제 후 저장된 데이터 전체 출력 ***/
  131. printf("현재 데이터의 수: %d \n", LCount(&list));
  132.  
  133. if(LFirst(&list, &data))
  134. {
  135. printf("%d ", data);
  136.  
  137. while(LNext(&list, &data))
  138. printf("%d ", data);
  139. }
  140. printf("\n\n");
  141. return 0;
  142. }
Success #stdin #stdout 0s 4888KB
stdin
Standard input is empty
stdout
현재 데이터의 수: 5 
11 11 22 22 33 

현재 데이터의 수: 3 
11 11 33