Skip to content

Instantly share code, notes, and snippets.

@Prince781
Created February 6, 2015 16:20
Show Gist options
  • Select an option

  • Save Prince781/21efbec65b6d934e2919 to your computer and use it in GitHub Desktop.

Select an option

Save Prince781/21efbec65b6d934e2919 to your computer and use it in GitHub Desktop.
Linked list implementation in C
#include "llist.h"
#include <stdio.h>
#include <stdlib.h>
struct llist *llist_new(void)
{
struct llist *ll;
if ((ll = malloc(sizeof(*ll))) == NULL) {
fprintf(stderr, "%s: could not create list\n", __func__);
return NULL;
}
ll->base = NULL;
ll->head = &ll->base;
ll->size = 0;
return ll;
}
void llist_add(struct llist *ll, void *elem)
{
struct llist_elem *lle;
lle = malloc(sizeof(*lle));
lle->data = elem;
lle->next = NULL;
*ll->head = lle;
ll->head = &lle->next;
++ll->size;
}
int llist_remove(struct llist *ll, void *elem)
{
struct llist_elem **lleptr;
struct llist_elem *llelem;
for (lleptr = &ll->base; *lleptr != NULL; lleptr = &(*lleptr)->next)
if ((*lleptr)->data == elem)
break;
if (*lleptr == NULL)
return 0;
if (&(*lleptr)->next == ll->head)
ll->head = lleptr;
llelem = *lleptr;
*lleptr = (*lleptr)->next;
free(llelem);
--ll->size;
return 1;
}
int llist_contains(struct llist *ll, void *elem)
{
struct llist_elem *llelem;
for (llelem = ll->base; llelem != NULL; llelem = llelem->next)
if (llelem->data == elem)
return 1;
return 0;
}
void *llist_get(struct llist *ll, int index)
{
struct llist_elem *llelem;
int p;
llelem = ll->base;
for (p=0; p<index && llelem != NULL; ++p)
llelem = llelem->next;
return llelem;
}
void llist_destroy(struct llist *ll)
{
struct llist_elem *llelem;
while (ll->size > 0)
llist_remove(ll, ll->base->data);
free(ll);
}
#ifndef _LLIST_H
#define _LLIST_H
struct llist_elem {
struct llist_elem *next;
void *data;
};
#define llelem_islast(lle) ((lle)->next == 0)
/* singly-linked list */
struct llist {
struct llist_elem *base;
struct llist_elem **head;
int size;
};
struct llist *llist_new(void);
void llist_add(struct llist *ll, void *elem);
/* returns bool: if removed */
int llist_remove(struct llist *ll, void *elem);
int llist_contains(struct llist *ll, void *elem);
void *llist_get(struct llist *ll, int index);
#define llist_empty(ll) ((ll)->size == 0)
#define llist_foreach(ll, e) for (e=ll->base; e!=0; e=e->next)
void llist_destroy(struct llist *ll);
#endif /* _LLIST_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment