sc/linked-list/README.md

53 lines
984 B
Markdown
Raw Normal View History

2021-02-07 22:31:04 +03:00
### Linked List
2020-12-27 16:14:34 +03:00
### Overview
- Intrusive doubly linked list.
2020-12-27 16:14:34 +03:00
- Basically, same as adding next and prev pointers to your structs.
- Add/remove from head/tail is possible so it can be used as list, stack,
queue, dequeue etc.
2021-02-07 22:31:04 +03:00
### Usage
2020-12-27 16:14:34 +03:00
```c
#include "sc_list.h"
#include <stdio.h>
2021-02-07 22:31:04 +03:00
int main()
2020-12-27 16:14:34 +03:00
{
struct user
{
char *name;
struct sc_list next;
};
2021-02-07 22:31:04 +03:00
struct user users[] = {{"first", {0}},
{"second", {0}},
{"third", {0}},
{"fourth", {0}},
{"fifth", {0}}};
2020-12-27 16:14:34 +03:00
struct sc_list list;
sc_list_init(&list);
2020-12-27 16:14:34 +03:00
for (int i = 0; i < 5; i++) {
sc_list_init(&users[i].next);
2020-12-27 16:14:34 +03:00
sc_list_add_tail(&list, &users[i].next);
}
struct sc_list *it;
struct user *user;
sc_list_foreach (&list, it) {
user = sc_list_entry(it, struct user, next);
printf("%s \n", user->name);
}
return 0;
}
```