Notice
Recent Comments
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 안드로이드
- 스시
- git
- c#
- chart
- 여행지도
- Android
- 갤럭시 노트
- 깃허브
- 서면 맛집
- 깃
- 경성대 맛집
- chrome
- 부경대 맛집
- 타이젠
- 태국지도
- 태국여행준비물
- 태국 여행정보
- github
- 일정관리
- 태국여행
- 자바
- 크롬
- 갤럭시 노트 해외판
- 맛집
- 소프트웨어
- Tizen
- java
- 삼성소프트웨어멤버십
- SDK
- Today
- 0
- Total
- 211,887
DREAM IT, WISH IT, DO IT
[C - 동적배열] 메모리 재할당을 통한 동적배열 본문
#include stdio.h
#include string.h
#include stdlib.h
#define ELETYPE int
ELETYPE *ar;
unsigned size;
unsigned num;
unsigned growby;
void InitArray(unsigned asize, unsigned agrowby)
{
size = asize;
growby = agrowby;
num = 0;
ar = (ELETYPE *)malloc(size * sizeof(ELETYPE));
}
void Insert(int index, ELETYPE value)
{
unsigned need;
need = num + 1;
if (need > size) {
size = need + growby;
ar = (ELETYPE *)realloc(ar, size * sizeof(ELETYPE));
}
memmove(ar + index + 1, ar + index, (num - index) * sizeof(ELETYPE));
ar[index] = value;
num++;
}
void Delete(int index)
{
memmove(ar + index, ar + index + 1, (num - index - 1)*sizeof(ELETYPE));
num--;
}
void Append(ELETYPE value)
{
Insert(num, value);
}
void UnInitArray()
{
free(ar);
}
void DumpArray(char* sMark)
{
unsigned i;
printf("%16s => Size = %02d, Count = %02d : ", sMark, size, num);
for (i = 0; i < num; i++) {
printf("%2d ",ar[i]);
}
printf("\n");
}
int main()
{
int i;
InitArray(10, 5);
DumpArray("First");
for (i = 0; i <= 8; i++) {
Append(i);
}
DumpArray("Append 8 => ");
Insert(5, 5);
DumpArray("Insert 10 => ");
Insert(5, 5);
DumpArray("Insert 11 => ");
Insert(5, 5);
DumpArray("Insert 12 => ");
Delete(7);
DumpArray("Delete index 7 => ");
UnInitArray();
return 0;
}
'Programming > C/C++' 카테고리의 다른 글
| Editplus, Visual Studio Compiler 연동하기 (1) | 2013.02.18 |
|---|---|
| [Linux Kernel] 리눅스 커널 소스입니다. (0) | 2012.10.20 |
| [C - 동적배열] 동적배열 활용 예제 (0) | 2012.01.26 |
| [C - 동적배열] 메모리 재할당을 통한 동적배열 (0) | 2012.01.26 |
| [C - 메모리 관리 함수] (0) | 2012.01.26 |
| [C - 동적배열] 배열 요소의 삽입과 삭제 (0) | 2012.01.26 |
0 Comments