1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* $Id: list.c,v 1.18 2010/02/05 23:50:22 simakov Exp $
*
* EPSILON - wavelet image compression library.
* Copyright (C) 2006,2007,2010 Alexander Simakov, <xander@entropyware.info>
*
* This file is part of EPSILON
*
* EPSILON is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EPSILON is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with EPSILON. If not, see <http://www.gnu.org/licenses/>.
*
* http://epsilon-project.sourceforge.net
*/
#include <common.h>
#include <list.h>
#include <mem_alloc.h>
linked_list *alloc_linked_list(void)
{
linked_list *list;
list = (linked_list *) xmalloc(sizeof(linked_list));
list->first = list->last = NULL;
return list;
}
void free_linked_list(linked_list *list)
{
list_node *cur_node;
list_node *next_node;
cur_node = list->first;
while (cur_node) {
next_node = cur_node->next;
free_list_node(cur_node);
cur_node = next_node;
}
free(list);
}
list_node *alloc_list_node(int data_size)
{
list_node *node;
node = (list_node *) xmalloc(sizeof(list_node));
node->data = xmalloc(data_size);
node->next = node->prev = NULL;
return node;
}
void free_list_node(list_node *node)
{
free(node->data);
free(node);
}
void append_list_node(linked_list *list, list_node *node)
{
if ((list->first == NULL) && (list->last == NULL)) {
node->next = node->prev = NULL;
list->first = list->last = node;
return;
}
node->next = NULL;
node->prev = list->last;
list->last->next = node;
list->last = node;
}
void prepend_list_node(linked_list *list, list_node *node)
{
if ((list->first == NULL) && (list->last == NULL)) {
node->next = node->prev = NULL;
list->first = list->last = node;
return;
}
node->prev = NULL;
node->next = list->first;
list->first->prev = node;
list->first = node;
}
void remove_list_node_link(linked_list *list, list_node *node)
{
if (node->prev) {
node->prev->next = node->next;
} else {
list->first = node->next;
}
if (node->next) {
node->next->prev = node->prev;
} else {
list->last = node->prev;
}
}
void remove_list_node(linked_list *list, list_node *node)
{
remove_list_node_link(list, node);
free_list_node(node);
}
void move_list_node(linked_list *src_list, linked_list *dst_list, list_node *node)
{
remove_list_node_link(src_list, node);
append_list_node(dst_list, node);
}
void insert_before_list_node(linked_list *list, list_node *node, list_node *new_node)
{
if (!node) {
prepend_list_node(list, new_node);
return;
}
if (node->prev) {
new_node->next = node;
new_node->prev = node->prev;
node->prev = node->prev->next = new_node;
} else {
new_node->prev = NULL;
new_node->next = node;
node->prev = list->first = new_node;
}
}
void insert_after_list_node(linked_list *list, list_node *node, list_node *new_node)
{
if (!node) {
append_list_node(list, new_node);
return;
}
if (node->next) {
new_node->prev = node;
new_node->next = node->next;
node->next = node->next->prev = new_node;
} else {
new_node->next = NULL;
new_node->prev = node;
node->next = list->last = new_node;
}
}
|