Putting it together
- C course
- Intermediate
- Lesson 116/116
- 6 min
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.
Practice 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");.
Starter code
#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;
}
// task_free fonksiyonunu yaz
// main'de kaydet, serbest bırak, yükle, yazdır, say, serbest bırak, bitti yazdır
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 *cur = g_list;
while (cur) { Task *n = cur->next; free(cur); cur = n; }
g_list = NULL;
task_load(&g_list, "tasks.txt");
int high = task_count_if(g_list, is_high_priority);
printf("yüksek: %d\n", high);
return 0;
}Sample 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;
}Expected output
liste: Ödeme: 1 Paketle: 3 yüksek: 1 bitti
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.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does task_free do?
- Adds a new task to the list
- Frees all nodes in the list Correct answer
- Saves the list to a file
- Prints the list
Explanation: task_free frees each node and sets the list to NULL.
2 Match the integration steps
| Term | Meaning |
|---|---|
task_push_front |
Add task |
task_save |
Save to file |
task_load |
Load from file |
task_free |
Clean memory |
3 Complete the task_free body
free(____);
free(cur);
Explanation: free(cur); frees the current node.