#include <stdio.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node n3 = {30, NULL};
struct Node n2 = {20, &n3};
struct Node n1 = {10, &n2};
struct Node *head = &n1;
printf("%d\n", head->next->next->data);
return 0;
}
🖍️ 30
head->next->next->data
->n1 -> n2 -> n3.data
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *next;
};
int main() {
struct Node *a = malloc(sizeof(struct Node));
struct Node *b = malloc(sizeof(struct Node));
struct Node *c = malloc(sizeof(struct Node));
a->value = 5; a->next = b;
b->value = 10; b->next = c;
c->value = 15; c->next = NULL;
struct Node *ptr = a;
int sum = 0;
while (ptr != NULL) {
sum += ptr->value;
ptr = ptr->next;
}
printf("%d\n", sum);
return 0;
}
🖍️ 30
- sum = 5 + 10 + 15 = 30
#include <stdio.h>
struct Item {
int id;
struct Item *link;
};
int main() {
struct Item a = {1, NULL};
struct Item b = {2, &a};
struct Item c = {3, &b};
struct Item *ptr = &c;
if (ptr->link->link == NULL)
printf("End\n");
else
printf("Not End\n");
return 0;
}
🖍️ Not End
ptr = &c ptr->link = &b ptr->link->link = &a → &a != NULL → "Not End"
#include <stdio.h>
#include <string.h>
struct Word {
char str[20];
struct Word *next;
};
int main() {
struct Word w1 = {"cat", NULL};
struct Word w2 = {"apple", &w1};
struct Word w3 = {"banana", &w2};
struct Word *ptr = &w3;
while (ptr != NULL) {
if (strchr(ptr->str, 'a') != NULL && strlen(ptr->str) > 4)
printf("%s\n", ptr->str);
ptr = ptr->next;
}
return 0;
}
🖍️
banana
apple
strchr(ptr->str, 'a') != NULL
: 문자열에 'a'가 포함strlen(ptr->str) > 4
: 문자열의 길이 > 4- "cat" : 문자열에 'a'는 포함되지만 문자열의 길이 4가 되지 않음 ∴ false
- "apple" : 문자열에 'a'가 포함되면서 동시에 문자열의 길이가 4가 넘음 ∴ true
- "banana": "apple"가 같은 이유 ∴ true
#include <stdio.h>
struct Data {
int num;
struct Data *next;
};
int main() {
struct Data d3 = {11, NULL};
struct Data d2 = {6, &d3};
struct Data d1 = {4, &d2};
struct Data *cur = &d1;
int sum = 0;
while (cur) {
if (cur->num % 2 == 0)
sum += cur->num;
cur = cur->next;
}
printf("%d\n", sum);
return 0;
}
🖍️ 10
- 구조체
Data
는int num
과 다음 노드를 가리키는 포인터*next
를 가지고 있어 → 단일 연결 리스트 구조d1 -> d2 -> d3
순서로 연결되어 있고, 값은 각각4 → 6 → 11
d1 % 2 = 4 % 2 == 0
->sum += 4
d2 % 2 = 6 % 2 == 0
->sum = 4 + 6
d3 % 2 = 11 % 2 != 0
#include <stdio.h>
void update(int **pp) {
**pp = 99;
}
int main() {
int a = 5;
int *p = &a;
update(&p);
printf("%d\n", a);
return 0;
}
🖍️ 99
int *p = &a;
→ 포인터p
는a
의 주소를 가리킴update(&p);
→p
의 주소를 전달 → 즉,pp
는int**
→ "포인터를 가리키는 포인터"*pp
는p
, 즉&a
**pp
는*(&a)
→ 결국a
∴**pp = 99
→a = 99
#include <stdio.h>
#include <stdlib.h>
struct Box {
int size;
};
int main() {
struct Box *arr = malloc(3 * sizeof(struct Box));
arr[0].size = 10;
arr[1].size = 20;
arr[2].size = 30;
int total = 0;
for (int i = 0; i < 3; i++) {
total += arr[i].size;
}
printf("%d\n", total);
free(arr);
return 0;
}
🖍️ 60
total = 10 + 20 + 30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
char *name;
};
int main() {
struct Person p;
p.name = malloc(20);
strcpy(p.name, "Charlie"); // 문자열 복사
printf("%s\n", p.name);
free(p.name);
return 0;
}
🖍️ Charlie
#include <stdio.h>
struct Node {
int val;
struct Node *next;
};
int main() {
struct Node n3 = {3, NULL};
struct Node n2 = {2, &n3};
struct Node n1 = {1, &n2};
struct Node *ptr = &n1;
int count = 0;
while (ptr) {
count++;
ptr = ptr->next;
}
printf("%d\n", count);
return 0;
}
🖍️ 3
- 노드 3개
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Animal {
char name[20];
struct Animal *next;
};
int main() {
struct Animal *a = malloc(sizeof(struct Animal));
struct Animal *b = malloc(sizeof(struct Animal));
strcpy(a->name, "dog");
a->next = b;
strcpy(b->name, "cat");
b->next = NULL;
struct Animal *cur = a;
while (cur != NULL) {
if (strcmp(cur->name, "cat") == 0)
printf("Meow\n");
cur = cur->next;
}
free(a);
free(b);
return 0;
}
🖍️ Meow
if (strcmp(cur->name, "cat") == 0)
->if (strcmp("dog", "cat") == 0)
-> not 0 ∴cur = cur->next;
if (strcmp("cat", "cat") == 0)
∴ strcmp == 0