// aaa.cpp 메인문


#include <stdio.h>

#include "bbb.h"


int main(){


data list;  // 구조체

int num;  // 리턴하는 데이터 받을 변수


listinit(&list);  // 구조체 초기화



// 구조체 삽입


insert(&list, 11);

insert(&list, 12);

insert(&list, 13);

insert(&list, 12);


// 모두 탐색


if (fir(&list, &num)){


printf("%d ", num);


while (next(&list, &num)){

printf("%d ", num);

}


printf("총 카운트 수 : %d \n", count(&list));

}


// 특정 숫자만 탐색해서 삭제


if (fir(&list, &num)){


if (num == 12){

remove(&list);

}

while (next(&list, &num)){


if (num == 12){

remove(&list);

}

}

printf("\n");

}


// 출력


if (fir(&list, &num)){


printf("%d ", num);


while (next(&list, &num)){

printf("%d ", num);

}


printf("총 카운트 수 : %d \n", count(&list));

}


return 0;

}




// bbb.c 정의문


#include <stdio.h>

#include "bbb.h"


void listinit(struct data * a){


a->count = 0;  // 전체 수

a->pos = -1; // 선택할 인덱스 

}


void insert(struct data *a, int num){


if (a->count > 100){

puts("가득참");

return;

}


a->arr[a->count] = num;

(a->count)++;  // 삽입할땐 카운트로 접근할땐 pos로

}


int fir(struct data *a, int * num){


if (a->count == 0){  // 전체가 0이라면

return false;

}


a->pos = 0;  // 인덱스 0

*num = a->arr[a->pos];  // 인덱스 0 값 지정


return true;

}


int next(struct data *a, int *num){


if (a->pos >= (a->count) - 1){  //  인덱스를 1부터 끝까지 접근하기 위해

return false;

}


(a->pos)++;

*num = a->arr[a->pos];  // 인덱스 1부터 끝까지 값 넣어줌


return true;

}


void remove(struct data *a){


int i;


for (i = a->pos; i < (a->count) - 1; i++){  // 자신의 인덱스부터 배열끝까지


  a->arr[i] = a->arr[i + 1];  // 하나큰 배열에서 덮어씀


}


(a->count)--; // 하나씩 덮어썼으니 전체 개수 하나 뺌

(a->pos)--;  // 인덱스 위치를 하나뺀다. 


// 예를들어 인덱스 0에 지웠다면 pos는 -1이 되고 next 함수에서 pos를 1 더하므로 0에서 다시 시작한다.


}


int count(struct data *a){

return a->count;

}




// bbb.h 선언문


#ifndef __aa

#define __aa


struct data{


int count;

int pos;


int arr[100];


};


void listinit(struct data * a);


void insert(struct data *a, int num);


int fir(struct data *a, int * num);


int next(struct data *a, int *num);


void remove(struct data *a);


int count(struct data *a);


#endif





// 답 


11 12 13 12 총 카운트 수 : 4

11 13 총 카운트 수 : 2

'자료구조' 카테고리의 다른 글

[자료구조] 배열로 된 스택  (0) 2019.12.12
[자료구조] 링크드 리스트의 이해  (0) 2019.12.12
[자료구조] 선택정렬  (0) 2019.12.07
[자료구조] 버블소트  (0) 2019.12.07
[자료구조] 이진탐색  (0) 2019.12.07
블로그 이미지

ryancha9

https://blog.naver.com/7246lsy

,