์๋ ํ์ธ์ ์ฃผ์ธ์ฅ H ์ ๋๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ ์ฝ๋ ๊ณต๋ถ๋ฅผ ์ํด ๊ธฐ์กด์ ์ ๊ฐ ์ด ๊ธ์ ์ฝ์ด๋ณด์๊ณ ์ค์๊ธฐ ๋ฐ๋๋๋ค.
modernalchemist.tistory.com/39
๋ ธ๋๊ฐ ํ์ ์๊ณ ๋ฆฌ์ฆ์ ๋ง๋ค๊ธฐ ์ํด์ ๋จผ์ ํค๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๋ ธ๋๋ถํฐ ์์๋๋ก ๋งํฌ๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉด์ ๋ ธ๋๊ฐ ์ ์ฅํ๊ณ ์๋ ๋ฐ์ดํฐ์ ์ฐพ๋ ๊ฐ์ ๋น๊ตํ๋ฉด ๋ฉ๋๋ค.
๋งํฌ ๊ฐ์ด 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;
}
'์๋ฃ๊ตฌ์กฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C์ธ์ด]์ฐ๊ฒฐ๋ฆฌ์คํธ ๊ฑฐ๊พธ๋ก ์ถ๋ ฅํ๊ธฐ (์ญ์์ผ๋ก ๋ง๋ค๊ธฐ) (0) | 2020.11.08 |
---|---|
[C์ธ์ด] ๋จ์ด๋ค์ ์ ์ฅํ๋ ์ฐ๊ฒฐ๋ฆฌ์คํธ (0) | 2020.11.06 |
[C์ธ์ด] ์ฐ๊ฒฐ๋ฆฌ์คํธ (linked list) (0) | 2020.11.06 |
[C์ธ์ด] ๋ฐฐ์ด๋ก ๊ตฌํํ ๋ฆฌ์คํธ (0) | 2020.11.06 |
[์๋ฃ๊ตฌ์กฐ] counting sort ๊ณ์์ ๋ ฌ (0) | 2020.08.18 |