1// linked_list.c
2// 단일 연결 리스트 구현
3
4#include <stdio.h>
5#include <stdlib.h>
6
7// 노드 구조체
8typedef struct Node {
9 int data;
10 struct Node* next;
11} Node;
12
13// 연결 리스트 구조체
14typedef struct {
15 Node* head;
16 int size;
17} LinkedList;
18
19// 리스트 생성
20LinkedList* list_create(void) {
21 LinkedList* list = malloc(sizeof(LinkedList));
22 if (!list) return NULL;
23
24 list->head = NULL;
25 list->size = 0;
26 return list;
27}
28
29// 맨 앞에 추가
30void list_push_front(LinkedList* list, int data) {
31 Node* new_node = malloc(sizeof(Node));
32 if (!new_node) return;
33
34 new_node->data = data;
35 new_node->next = list->head;
36 list->head = new_node;
37 list->size++;
38}
39
40// 맨 뒤에 추가
41void list_push_back(LinkedList* list, int data) {
42 Node* new_node = malloc(sizeof(Node));
43 if (!new_node) return;
44
45 new_node->data = data;
46 new_node->next = NULL;
47
48 if (list->head == NULL) {
49 list->head = new_node;
50 } else {
51 Node* current = list->head;
52 while (current->next != NULL) {
53 current = current->next;
54 }
55 current->next = new_node;
56 }
57 list->size++;
58}
59
60// 맨 앞 제거
61int list_pop_front(LinkedList* list, int* data) {
62 if (list->head == NULL) return 0;
63
64 Node* temp = list->head;
65 *data = temp->data;
66 list->head = temp->next;
67 free(temp);
68 list->size--;
69
70 return 1;
71}
72
73// 특정 값 찾기
74Node* list_find(LinkedList* list, int data) {
75 Node* current = list->head;
76
77 while (current != NULL) {
78 if (current->data == data) {
79 return current;
80 }
81 current = current->next;
82 }
83
84 return NULL;
85}
86
87// 특정 값 삭제
88int list_remove(LinkedList* list, int data) {
89 if (list->head == NULL) return 0;
90
91 // 첫 노드가 삭제 대상인 경우
92 if (list->head->data == data) {
93 Node* temp = list->head;
94 list->head = list->head->next;
95 free(temp);
96 list->size--;
97 return 1;
98 }
99
100 // 중간 또는 끝 노드 삭제
101 Node* current = list->head;
102 while (current->next != NULL) {
103 if (current->next->data == data) {
104 Node* temp = current->next;
105 current->next = temp->next;
106 free(temp);
107 list->size--;
108 return 1;
109 }
110 current = current->next;
111 }
112
113 return 0; // 못 찾음
114}
115
116// 리스트 출력
117void list_print(LinkedList* list) {
118 Node* current = list->head;
119
120 printf("[");
121 while (current != NULL) {
122 printf("%d", current->data);
123 if (current->next != NULL) {
124 printf(" -> ");
125 }
126 current = current->next;
127 }
128 printf("]\n");
129}
130
131// 리스트 해제
132void list_destroy(LinkedList* list) {
133 Node* current = list->head;
134
135 while (current != NULL) {
136 Node* next = current->next;
137 free(current);
138 current = next;
139 }
140
141 free(list);
142}
143
144int main(void) {
145 LinkedList* list = list_create();
146
147 printf("=== 연결 리스트 테스트 ===\n\n");
148
149 // 데이터 추가
150 printf("맨 뒤에 추가: 10, 20, 30\n");
151 list_push_back(list, 10);
152 list_push_back(list, 20);
153 list_push_back(list, 30);
154 list_print(list);
155 printf("크기: %d\n\n", list->size);
156
157 // 맨 앞에 추가
158 printf("맨 앞에 추가: 5\n");
159 list_push_front(list, 5);
160 list_print(list);
161 printf("\n");
162
163 // 값 찾기
164 printf("값 20 찾기: ");
165 Node* found = list_find(list, 20);
166 if (found) {
167 printf("찾음! (주소: %p)\n", (void*)found);
168 } else {
169 printf("못 찾음\n");
170 }
171 printf("\n");
172
173 // 값 삭제
174 printf("값 20 삭제\n");
175 list_remove(list, 20);
176 list_print(list);
177 printf("\n");
178
179 // 맨 앞 제거
180 int data;
181 printf("맨 앞 제거\n");
182 list_pop_front(list, &data);
183 printf("제거된 값: %d\n", data);
184 list_print(list);
185 printf("\n");
186
187 // 메모리 해제
188 list_destroy(list);
189
190 return 0;
191}