mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-31 19:20:26 +00:00
forgot to add the basic linked_list
This commit is contained in:
parent
b9fa6cb54b
commit
b7df1c5af1
60
src/linked_list.c
Normal file
60
src/linked_list.c
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* linked_list.c
|
||||
*
|
||||
* Created by Matthias Ringwald on 7/13/09.
|
||||
*/
|
||||
|
||||
#include "linked_list.h"
|
||||
|
||||
/**
|
||||
* linked_list_add
|
||||
*/
|
||||
void linked_list_add(linked_list_t * list, linked_item_t *item){ // <-- add item to list
|
||||
// check if already in list
|
||||
linked_item_t *it;
|
||||
for (it = *list; it ; it = it->next){
|
||||
if (it == item) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// add first
|
||||
item->next = *list;
|
||||
*list = item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove data_source from run loop
|
||||
*
|
||||
* @note: assumes that data_source_t.next is first element in data_source
|
||||
*/
|
||||
int linked_list_remove(linked_list_t * list, linked_item_t *item){ // <-- remove item from list
|
||||
linked_item_t *it;
|
||||
for (it = (linked_item_t *) list; it ; it = it->next){
|
||||
if (it->next == item){
|
||||
it->next = item->next;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void linked_item_set_user(linked_item_t *item, void *user_data){
|
||||
item->next = (void *) 0;
|
||||
item->user_data = user_data;
|
||||
};
|
||||
|
||||
void * linked_item_get_user(linked_item_t *item) {
|
||||
return item->user_data;
|
||||
};
|
||||
|
||||
#include <strings.h>
|
||||
void test_linked_list(){
|
||||
linked_list_t testList = 0;
|
||||
linked_item_t itemA;
|
||||
linked_item_t itemB;
|
||||
linked_item_set_user(&itemA, (void *) 0);
|
||||
linked_item_set_user(&itemB, (void *) 0);
|
||||
linked_list_add(&testList, &itemA);
|
||||
linked_list_add(&testList, &itemB);
|
||||
linked_list_remove(&testList, &itemB);
|
||||
}
|
21
src/linked_list.h
Normal file
21
src/linked_list.h
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* linked_list.h
|
||||
*
|
||||
* Created by Matthias Ringwald on 7/13/09.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef struct linked_item {
|
||||
struct linked_item *next; // <-- next element in list, or NULL
|
||||
void *user_data; // <-- pointer to struct base
|
||||
} linked_item_t;
|
||||
|
||||
typedef linked_item_t * linked_list_t;
|
||||
|
||||
void linked_item_set_user(linked_item_t *item, void *user_data); // <-- set user data
|
||||
void * linked_item_get_user(linked_item_t *item); // <-- get user data
|
||||
void linked_list_add(linked_list_t * list, linked_item_t *item); // <-- add item to list
|
||||
int linked_list_remove(linked_list_t * list, linked_item_t *item); // <-- remove item from list
|
||||
|
||||
void test_linked_list();
|
Loading…
x
Reference in New Issue
Block a user