Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TLIST_CONTENT int
- #define TRUE 1
- #define FALSE 0
- #define BOOLEAN short
- struct slist {
- struct stlistitem *head, *tail;
- int elem_count;
- };
- typedef struct slist tlist;
- struct stlistitem {
- struct stlistitem *next, *previous;
- TLIST_CONTENT *content;
- };
- typedef struct stlistitem tlistitem;
- tlist *tlist_init();
- void tlist_add(tlist *list, TLIST_CONTENT *content);
- void tlist_free(tlist *list);
- tlistitem *_tlist_new_item();
- int main(int argc, char *argv[]) {
- tlist *a;
- TLIST_CONTENT value1, value2;
- value1 = 99;
- value2 = 88;
- a = tlist_init();
- tlist_add(a,&value1);
- tlist_add(a,&value2);
- printf("%d\n", *(a->head->next->content));
- printf("%d\n", *(a->head->next->next->content));
- tlist_free(a);
- }
- tlist *tlist_init() {
- tlist *l;
- l = (tlist *)malloc(sizeof(tlist));
- tlistitem *head;
- head = _tlist_new_item();
- l->head = head;
- l->tail = l->head;
- l->elem_count = 0;
- return l;
- }
- void tlist_add(tlist *list, TLIST_CONTENT *content) {
- tlistitem *new_item, *temp;
- new_item = (tlistitem *)malloc(sizeof(tlistitem));
- list->tail->next = new_item;
- new_item->previous = list->tail;
- new_item->next = NULL;
- new_item->content = content;
- list->tail = new_item;
- list->elem_count++;
- }
- tlistitem *_tlist_new_item() {
- tlistitem *item;
- item = (tlistitem *)malloc(sizeof(tlistitem));
- item->previous = NULL;
- item->next = NULL;
- item->content = NULL;
- return item;
- }
- tlistitem *tlist_get_head(tlist *list) {
- return list->head->next;
- }
- tlistitem *tlist_get_tail(tlist *list) {
- return list->tail;
- }
- tlistitem *tlist_get_next(tlistitem *item) {
- return item->next;
- }
- tlistitem *tlist_get_previous(tlistitem *item) {
- return item->previous;
- }
- BOOLEAN tlist_is_tail(tlist *list, tlistitem *item) {
- return (item == (list->tail));
- }
- BOOLEAN tlist_is_head(tlist *list, tlistitem *item) {
- return (item == (list->head));
- }
- void tlist_free_item(tlistitem *item) {
- free(item->next);
- item->next = NULL;
- free(item->previous);
- item->previous = NULL;
- free(item);
- item = NULL;
- }
- void tlist_free(tlist *list) {
- tlistitem *item = tlist_get_tail(list);
- tlistitem *prev;
- if(item != NULL) {
- prev = tlist_get_previous(item);
- while(!tlist_is_head(list,item)) {
- tlist_free_item(item);
- item = prev;
- prev = tlist_get_previous(item);
- }
- }
- free(list);
- }
Add Comment
Please, Sign In to add comment