Skip to content

Instantly share code, notes, and snippets.

@aliou
Last active December 17, 2015 00:19
Show Gist options
  • Save aliou/5520030 to your computer and use it in GitHub Desktop.
Save aliou/5520030 to your computer and use it in GitHub Desktop.
/*
** llist.c for tools in /home/diallo_e/src/projets/zappy/src/tools
**
** Made by aliou diallo
** Login <[email protected]>
**
** Started on Sun May 05 09:12:36 2013 aliou diallo
** Last update Sun May 05 09:29:49 2013 aliou diallo
*/
#include "llist.h"
t_list *create_list()
{
t_list *list;
if ((list = malloc(sizeof(*list))) == NULL)
return (NULL);
list->length = 0;
list->head = NULL;
list->tail = NULL;
return (list);
}
void delete_list(t_list *list)
{
if (list)
free(list);
}
void push_front(t_list *list, t_node *node)
{
if (list && node)
{
node->prev = NULL;
node->next = list->head;
if (list->head)
list->head->prev = node;
if (list->tail == NULL)
list->tail = node;
list->head = node;
list->length += 1;
}
}
void push_back(t_list *list, t_node *node)
{
if (list && node)
{
node->prev = list->tail;
node->next = NULL;
if (list->tail != NULL)
list->tail->next = node;
if (list->head == NULL)
list->head = node;
list->tail = node;
list->length += 1;
}
}
void remove(t_list *list, t_node *node)
{
t_node *current;
if (list && node)
{
current = list->head;
while (current && current != node)
current = current->next;
if (current == NULL)
return ;
if (current->next)
current->next->prev = current->prev;
if (current->prev)
current->prev->next = current->next;
if (list->head == node)
list->head = node->next;
if (list->tail == node)
list->tail = node->prev;
delete_node(node);
}
}
/*
** llist.h for include in /home/diallo_e/src/projets/zappy/include
**
** Made by aliou diallo
** Login <[email protected]>
**
** Started on Sun May 05 09:07:02 2013 aliou diallo
** Last update Sun May 05 09:15:02 2013 aliou diallo
*/
#include <stdlib.h>
#ifndef LLIST_H_
# define LLIST_H_
typedef struct s_node
{
void *content;
struct s_node *prev;
struct s_node *next;
} t_node;
t_node *create_node(void *);
void delete_node(t_node *);
typedef struct s_list
{
int length;
t_node *head;
t_node *tail;
} t_list;
t_list *create_list();
void delete_list(t_list *);
void push_front(t_list *, t_node *);
void push_back(t_list *, t_node *);
void remove(t_list *, t_node *);
#endif /* !LLIST_H_ */
/*
** node.c for tools in /home/diallo_e/src/projets/zappy/src/tools
**
** Made by aliou diallo
** Login <[email protected]>
**
** Started on Sun May 05 09:16:02 2013 aliou diallo
** Last update Sun May 05 09:16:04 2013 aliou diallo
*/
#include "llist.h"
t_node *create_node(void *content)
{
t_node *node;
if ((node = malloc(sizeof(*node))) == NULL)
return (NULL);
node->content = content;
node->prev = NULL;
node->next = NULL;
return (node);
}
void delete_node(t_node *node)
{
if (node)
free(node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment