๋ฐ˜๊ฐ€์›Œ์š”! ํ—ˆ๋ธŒ์ž…๋‹ˆ๋‹ค!

์ €๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ํ˜„๋Œ€ ์—ฐ๊ธˆ์ˆ ์‚ฌ๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ๋Š๋‚€ ์ ๋“ค๊ณผ ์ด์•ผ๊ธฐ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.

์ž๋ฃŒ๊ตฌ์กฐ

[C์–ธ์–ด] ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ •ํ•œ ๊ฐ’ ํƒ์ƒ‰ํ•˜๊ธฐ(๋…ธ๋“œ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

mmin.h 2020. 11. 6. 15:08

์•ˆ๋…•ํ•˜์„ธ์š” ์ฃผ์ธ์žฅ H ์ž…๋‹ˆ๋‹ค. 

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ฝ”๋“œ ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด ๊ธฐ์กด์— ์ œ๊ฐ€ ์“ด ๊ธ€์„ ์ฝ์–ด๋ณด์‹œ๊ณ  ์˜ค์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 

modernalchemist.tistory.com/39

 

[C์–ธ์–ด] ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ (linked list)

linked representation ๋™์ ์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ณ  ์‚ญ์ œ๋‚˜ ์‚ฝ์ž… ์‹œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์—ฐ๊ฒฐ๋œ ํ‘œํ˜„ ์ด ์—ฐ๊ฒฐ๋œ ํ‘œํ˜„์€ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋“ค์„ ์—ฐ๊ฒฐ. ์ด๋Š” ํŠธ๋ฆฌ ๊ทธ๋ž˜ํ”„ ์Šคํƒ ํ ๋“ฑ

modernalchemist.tistory.com

๋…ธ๋“œ๊ฐ’ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„  ๋จผ์ € ํ—ค๋“œ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋งํฌ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ๋…ธ๋“œ๊ฐ€ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์™€ ์ฐพ๋Š” ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

๋งํฌ ๊ฐ’์ด NULL์ด๋ฉด ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋์— ๋„๋‹ฌํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ํƒ์ƒ‰์„ ์ค‘๋‹จํ•˜๊ณ  ๋ฐ˜ํ™˜ ๊ฐ’์€ ํƒ์ƒ‰๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋…ธ๋“œ์˜ ์ฃผ์†Œ์ด๋‹ค. 

ListNode* search_list(ListNode* head, element value){
	ListNode * p = head; 
    while (p != NULL){
    if(p->data == value) return p;
    p = p-> link;
    }
    return NULL;
}

์ด๋Ÿฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”. 
๊ทธ๋Ÿผ ์ด๊ฑธ ์ ์šฉํ•ด๋ณธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

#include <stdio.h>
#include <stdlib.h>

typedef int element; 

typedef struct ListNode {
	element data;
	struct ListNode* link;
} ListNode;

ListNode* insert_first(ListNode* head, element value) {
	ListNode* p = (ListNode*)malloc(sizeof(ListNode));
	
	p->data = value; 
	p->link = head; 
	head = p; 
	return head; 
}
ListNode* insert(ListNode* head, ListNode* pre, element value) {
	ListNode* p = (ListNode*)malloc(sizeof(ListNode));
	p->data = value; 
	p->link = pre->link; 
	pre->link = p; 
	return head; 
}
ListNode* delete_first(ListNode* head) {
	ListNode* removed; 
	if (head == NULL) return NULL;
	removed = head; 
	head = removed->link;
	free(removed);
	return head; 
}

ListNode* delete_pre(ListNode* head, ListNode* pre) {
	ListNode* removed; 
	removed = pre->link; 
	pre->link = removed->link;
	free(removed); 
	return head; 
}
void print_list(ListNode* head) {
	for (ListNode* p = head; p != NULL; p = p->link) {
		printf("%d -> ", p->data);
	}
		printf("NULL\n");
}

ListNode* search_list(ListNode* head, element vlaue) {
	ListNode* p = head; 
	if (p->data == NULL) return NULL; 
	while (p->link != NULL) {
		if (p->data = vlaue) return p; 
		p = p->link;
	}
	return NULL; //ํƒ์ƒ‰์‹คํŒจ
}
int main(void) {
	ListNode* head = NULL; //linked list๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” head pointer
	
	head = insert_first(head, 10);
	print_list(head);
	head = insert_first(head, 20);
	print_list(head);
	head = insert_first(head, 30);
	print_list(head);

	if (search_list(head, 20) != NULL) {
		printf("๋ฐ์ดํ„ฐ ๊ฐ’ (%d) ๋ฐœ๊ฒฌ\n", 20);
	}
	else {
		printf("๋ฐ์ดํ„ฐ ๊ฐ’ (%d) ์ฐพ์ง€๋ชปํ•จ\n", 20);
	}
	return 0; 
}