C
Putting it together
Combine all parts into a working app.
Putting it together
In the final lesson you combine all pieces. The task_free function releases every node in the linked list. main performs creation, saving, loading, printing, counting and cleanup in sequence.
task_free walks the list and frees each node with free; finally sets *list = NULL. main adds two tasks, saves to file, frees the list, loads from file, prints the list, counts high-priority tasks, and cleans up. This is the skeleton of a real console application: create data, persist, reload, display, and clean up. When the project is complete, your intermediate C skills are synthesized.
Exercise Integrate all pieces
Write void task_free(Task **list); walk the list, free each node, set *list = NULL. In main: add two tasks, call task_save(g_list, "tasks.txt");, task_free(&g_list);, task_load(&g_list, "tasks.txt");, printf("liste:\n");, task_print(g_list);, print the high count, task_free(&g_list);, printf("bitti\n");.
Show example solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Task {
char name[32];
int priority;
struct Task *next;
} Task;
Task *g_list = NULL;
Task *task_new(const char *name, int priority) {
Task *t = malloc(sizeof(Task));
strncpy(t->name, name, 31);
t->name[31] = '\0';
t->priority = priority;
t->next = NULL;
return t;
}
void task_push_front(Task **list, Task *t) {
t->next = *list;
*list = t;
}
void task_print(const Task *list) {
const Task *cur = list;
while (cur) {
printf("%s: %d\n", cur->name, cur->priority);
cur = cur->next;
}
}
typedef int (*TaskFilter)(const Task *);
int is_high_priority(const Task *t) {
return t->priority >= 3;
}
int task_count_if(const Task *list, TaskFilter filter) {
int count = 0;
const Task *cur = list;
while (cur) {
if (filter(cur)) count++;
cur = cur->next;
}
return count;
}
int task_save(const Task *list, const char *path) {
FILE *f = fopen(path, "w");
if (!f) return 0;
const Task *cur = list;
while (cur) {
fprintf(f, "%s %d\n", cur->name, cur->priority);
cur = cur->next;
}
fclose(f);
return 1;
}
int task_load(Task **list, const char *path) {
FILE *f = fopen(path, "r");
if (!f) return 0;
char name[32];
int priority;
while (fscanf(f, "%31s %d", name, &priority) == 2) {
task_push_front(list, task_new(name, priority));
}
fclose(f);
return 1;
}
void task_free(Task **list) {
Task *cur = *list;
while (cur) {
Task *n = cur->next;
free(cur);
cur = n;
}
*list = NULL;
}
int main(void) {
task_push_front(&g_list, task_new("Ödeme", 1));
task_push_front(&g_list, task_new("Paketle", 3));
task_save(g_list, "tasks.txt");
task_free(&g_list);
task_load(&g_list, "tasks.txt");
printf("liste:\n");
task_print(g_list);
int high = task_count_if(g_list, is_high_priority);
printf("yüksek: %d\n", high);
task_free(&g_list);
printf("bitti\n");
return 0;
}
Project complete
Congratulations! You completed the five-lesson task manager project. In this project you combined struct, linked list, function pointer and file I/O.
The program now creates tasks, inserts them, saves to file, loads back, prints, filters, and cleanly frees memory. This covers all the core skills an intermediate C programmer needs. Advanced systems programming, multiprocessing and kernel basics await you. You have laid a solid foundation; the next steps will be deeper and more powerful.
Test yourself
-
What does task_free do?
- Adds a new task to the list
- Frees all nodes in the list
- Saves the list to a file
- Prints the list
Show answer
Frees all nodes in the list — task_free frees each node and sets the list to NULL.
-
Match the integration steps
- task_push_front
- Add task
- task_save
- Save to file
- task_load
- Load from file
- task_free
- Clean memory
-
Complete the task_free body
free(____);
Show answer
free(cur);
Next lesson: Intermediate section exam