Created
June 16, 2014 20:46
-
-
Save oktal/d606148126914539ea1a to your computer and use it in GitHub Desktop.
Linked list implementation in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "list.h" | |
#include <stdlib.h> | |
static bool list_empty(struct list *this) | |
{ | |
return this == NULL ? TRUE : | |
this->m_size == 0 ? TRUE : FALSE; | |
} | |
static void *list_front(struct list *this) | |
{ | |
void *ret = NULL; | |
if (this != NULL) | |
{ | |
ret = this->begin->data; | |
} | |
return ret; | |
} | |
static void *list_back(struct list *this) | |
{ | |
void *ret = NULL; | |
if (this != NULL) | |
{ | |
ret = this->end->data; | |
} | |
return ret; | |
} | |
static void list_push_back(struct list *this, void *data) | |
{ | |
if (this != NULL) | |
{ | |
Listitem *new = malloc(sizeof *new); | |
if (new != NULL) | |
{ | |
new->data = data; | |
new->next = NULL; | |
new->prev = this->end; | |
this->end = new; | |
this->m_size++; | |
} | |
} | |
} | |
static void list_push_front(struct list *this, void *data) | |
{ | |
if (this != NULL) | |
{ | |
Listitem *new = malloc(sizeof *new); | |
if (new != NULL) | |
{ | |
new->data = data; | |
new->prev = NULL; | |
new->next = this->begin; | |
this->begin = new; | |
this->m_size++; | |
} | |
} | |
} | |
static void *list_pop_front(struct list *this) | |
{ | |
void *ret = NULL; | |
if (this != NULL) | |
{ | |
Listitem *tmp = this->begin; | |
ret = this->begin->data; | |
free(this->begin->data); | |
free(this->begin); | |
this->begin = tmp; | |
this->m_size--; | |
} | |
return ret; | |
} | |
static void *list_pop_back(struct list *this) | |
{ | |
void *ret = NULL; | |
if (this != NULL) | |
{ | |
Listitem *tmp = this->end; | |
ret = this->end->data; | |
free(this->end->data); | |
free(this->end); | |
this->end = tmp; | |
this->m_size--; | |
} | |
return ret; | |
} | |
static void list_clear(struct list *this) | |
{ | |
if (this != NULL) | |
{ | |
Listitem *it = this->begin; | |
while (it != NULL) | |
{ | |
Listitem *del = it; | |
it = it->next; | |
free(del->data); | |
free(del); | |
} | |
this->m_size = 0; | |
} | |
} | |
static size_t list_size(struct list *this) | |
{ | |
return this == NULL ? 0 : this->m_size; | |
} | |
static void list_init(struct list *this) | |
{ | |
this->empty = list_empty; | |
this->front = list_front; | |
this->back = list_back; | |
this->push_front = list_push_front; | |
this->pop_front = list_pop_front; | |
this->push_back = list_push_back; | |
this->pop_back = list_pop_back; | |
this->clear = list_clear; | |
this->size = list_size; | |
this->m_size = 0; | |
this->begin = this->end = NULL; | |
} | |
List *new_list(void) | |
{ | |
List *new = malloc(sizeof *new); | |
if (new != NULL) | |
{ | |
list_init(new); | |
} | |
return new; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LIST_H_INCLUDED | |
#define LIST_H_INCLUDED | |
#include <stddef.h> | |
typedef unsigned char bool; | |
enum | |
{ | |
FALSE = 0, TRUE, | |
LIST_EMPTY | |
}; | |
typedef struct listitem | |
{ | |
void *data; | |
struct listitem *prev; | |
struct listitem *next; | |
} Listitem; | |
typedef bool (*compareFunc) (void *data); | |
typedef struct list | |
{ | |
bool (*empty) (struct list *this); | |
void * (*front) (struct list *this); | |
void * (*back) (struct list *this); | |
void (*push_front) (struct list *this, void *data); | |
void * (*pop_front) (struct list *this); | |
void (*push_back) (struct list *this, void *data); | |
void * (*pop_back) (struct list *this); | |
void (*clear) (struct list *this); | |
void (*remove) (struct list *this); | |
void (*remove_if) (struct list *this, compareFunc func); | |
void (*reverse) (struct list *this); | |
size_t (*size) (struct list *this); | |
size_t m_size; | |
Listitem *begin; | |
Listitem *end; | |
} List; | |
List * new_list(void); | |
#endif // LIST_H_INCLUDED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment