Data model
- C course
- Intermediate
- Lesson 113/116
- 6 min
Set up the data model with structs and linked list.
Creating tasks
The second lesson defines task creation and list insertion functions. task_new creates a new task with dynamic memory; task_push_front inserts at the front.
task_new allocates with malloc, copies the name with strncpy and sets next to NULL. task_push_front places the new node at the head of g_list. task_print walks the list from head to tail and prints each task. These three functions are the basic operation set of a linked list: create, insert, display. The skeleton from the previous lesson is included in the starting code.
Inserting at head
task_push_front places the new node at the head of the list. This is the fastest way to insert into a linked list; the head pointer is already at hand.
Implementation: t->next = *list; *list = t;. First the new node's next points to the old head; then the head links to the new node. Insertion is done in two steps. If you add two tasks, the second one added comes first; so the print order is the reverse of insertion. This behavior is also the basis of the stack data structure; last in, first out (LIFO).
Practice Task creation and listing
Write Task *task_new(const char *name, int priority); allocate with malloc, copy name with strncpy. Write void task_push_front(Task **list, Task *t); insert at head. Write void task_print(const Task *list); walk and print. In main add task_push_front(&g_list, task_new("Ödeme", 1)); and task_push_front(&g_list, task_new("Paketle", 3));, call task_print(g_list);.
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_new fonksiyonunu yaz
// task_push_front fonksiyonunu yaz
// task_print fonksiyonunu yaz
// main'de iki görev ekle ve yazdır
int main(void) {
printf("Görev yöneticisi hazır\n");
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;
}
}
int main(void) {
task_push_front(&g_list, task_new("Ödeme", 1));
task_push_front(&g_list, task_new("Paketle", 3));
task_print(g_list);
return 0;
}Expected output
Paketle: 3 Ödeme: 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match functions to purpose
| Term | Meaning |
|---|---|
task_new |
New task |
task_push_front |
Insert at head |
task_print |
Print list |
malloc |
Allocate |
2 If two tasks are added at head, what is the print order?
- Only the last task
- Random
- Same as insertion
- Reverse of insertion Correct answer
Explanation: Head insertion is LIFO; the last added is printed first.
3 Complete the task_new call
task_push_front(&g_list, task_new("____", 3));
task_push_front(&g_list, task_new("Paketle", 3));
Explanation: task_new("Paketle", 3) creates a new task.