C언어로 쉽게 풀어쓴 자료구조 [연습문제 3장]

Minseok Jo·2023년 10월 10일
0
post-thumbnail
  1. 10(행) × 20(열) × 4(int형 크기) = 800 byte  ∴ (4)번

  2. 1000(시작주소) + 4(float형 크기) × 9(10번째 원소는 첫번째 원소 9번째 뒤이므로) = 1036번지
    → 책에서는 10번째 원소이므로 1000+4×10=1040 이 답으로 되어있는데, 이는 문제 오류이다.

  3. (1): 4×10, (2): 8×10, (3): 1×40, (4): 4×10  ∴ (2)번

int main(void) {
	int two[10];
	
    for(int i=0;i<10;i++) {
    	two[i] = pow(2, i);
        printf("%d ", two[i]);
    }
}        

struct person {
	char name[20];
    int age;
    float wage;
};    

typedef struct {
	float real, imaginary;
} complex;

int main(void) {
	complex c1, c2;
}    

typedef struct {
	double real, imag;
} complex;

complex add(complex a, complex b) {
	complex temp = {a.real+b.real, a.imag+b.imag};
    return temp;
}    

void insert(int array[], int loc, int value) {
	if (loc<items) {	// 만약 지정한 위치에 원소가 이미 존재하는 경우
    	for (int i=items;i>loc;i--)	// 지정한 위치부터 ~ 그 뒤에 있는 원소들을 뒤로 한칸씩 밀기
        	arr[i] = arr[i-1];
	}
    arr[loc] = value; // 지정한 위치에 정수 삽입
    items++; // 원소의 개수 1개 증가 (전역변수로 선언되었다고 가정)
}    

  1. 정수가 삽입될때마다 n개의 원소들이 이동하여야 하므로, 시간복잡도는 O(n) 이다.

void delete(int array[], int loc) {
	if (loc<items-1) {	// 지정한 위치 뒤에 원소가 존재하는 경우
    	for (int i=loc;i<items-1;i++)	// 뒤에 원소들을 삭제된 위치까지 앞으로 당긴다
        	arr[i] = arr[i+1]
	}
    items--;	// 원소의 개수 1개 감소 (전역변수로 선언되었다고 가정)
    arr[items] = NULL;	// 당겨지지 않은 마지막 원소 삭제
}    

11. 원소가 삭제될때마다 n개의 원소들이 이동하여야 하므로, 시간복잡도는 O(n) 이다.
typedef struct {
	int num;
    char str[20];
} strt;

int main(void) {
	strt* s= (strt*)malloc(sizeof(strt));
    s->num = 100;
    strcpy(s->str, "just testing");
    
    free(s);
}    

0개의 댓글