single_linked_list.c 4.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* 
 * Copyright (c) 2012 Christoph Mueller <ruunhb@googlemail.com>
 * 
 * Crystal is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Crystal 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
12
 * Lesser GNU General Public License for more derears.
13 14 15 16 17 18
 *
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "queue.h"
#include "stack.h"
#include <assert.h>


struct Node {
	struct Node* next;
	pointer data;
};


struct Stack {
	struct Node* begin;
};

34
struct CryQueue {
35
	struct Node* front;
36
	struct Node* rear;
37 38 39 40 41 42
};


struct CryQueue*  	
cry_queue_new(void)
{
43
	struct CryQueue* list = cry_malloc(struct CryQueue);
44
	list->front = 0;
45 46
	list->rear = 0;
	return list;
47 48 49 50
}


void            	
51
cry_queue_free(struct CryQueue* queue, cry_free_funptr handler)
52 53 54
{
	assert(queue != 0);

55
	struct Node* node = queue->front;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	struct Node* tmp = 0;

	while(node != 0) {
		tmp = node;
		node = node->next;

		if(handler != 0) {
			handler(tmp->data);
		}

		cry_free(node);
	}

	cry_free(queue);
}


int             	
cry_queue_size(struct CryQueue* queue)
{
	assert(queue != 0);

	size_t length = 0;
79
	struct Node* node = queue->front;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	while(node != 0) {
		node = node->next;
		++length;
	}

	return length;
}


pointer         	
cry_queue_front(struct CryQueue* queue)
{
	assert(queue != 0);

95
	if(queue->front == 0)
96 97
		return 0;
	else
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
		return queue->front->data;
}


pointer
cry_queue_rear(struct CryQueue* queue)
{
	assert(queue != 0);

	if(queue->rear == 0)
		return 0;
	else
		return queue->rear->data;
}


pointer
cry_queue_nth(struct CryQueue* queue, size_t index)
{
	assert(queue != 0);

	struct Node* node = queue->front;

	while(node != 0 && index-- > 0)
		node = node->next;

	return node;
125 126 127 128 129 130 131 132
}


pointer         	
cry_queue_dequeue(struct CryQueue* queue)
{
	assert(queue != 0);

133
	struct Node* front = queue->front;
134 135 136 137 138 139 140
	pointer data = 0;

	if(front == 0) 
		return data;

	data = front->data;

141 142
	if(queue->front == queue->rear) 
		queue->front = queue->rear = 0;
143
	else
144
		queue->front = front->next;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

	cry_free(front);

	return data;
}


void            	
cry_queue_enqueue(struct CryQueue* queue, pointer data)
{
	assert(queue != 0);

	struct Node* node = cry_malloc(struct Node);

	node->next = 0;
	node->data = data;

162 163 164
	if(queue->front == 0) {
		queue->front = node;
		queue->rear = node;
165
	 } else {
166 167
		queue->rear->next = node;
		queue->rear = node;
168 169 170 171 172 173 174 175 176 177 178 179 180 181
	}
}


struct CryStack*       	
cry_stack_new(void)
{
	struct Stack* stack = cry_malloc(struct Stack);
	stack->begin = 0;
	return cry_cast(struct CryStack*, stack);
}


void
182
cry_stack_free(struct CryStack* stack, cry_free_funptr handler)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
	assert(stack != 0);

	struct Node* node = cry_cast(struct Stack*, stack)->begin;
	struct Node* tmp = 0;

	while(node != 0) {
		tmp = node;
		node = node->next;
		
		if(handler != 0) {
			handler(tmp->data);
		}
		
		cry_free(node);
	}

	cry_free(stack);
}


size_t
cry_stack_size(struct CryStack* stack)
{
	assert(stack != 0);
	
	size_t length = 0;
	struct Node* node = cry_cast(struct Stack*, stack)->begin;

	while(node != 0) {
		node = node->next;
		++length;
	}

	return length;
}



pointer
cry_stack_top(struct CryStack* stack)
{
	assert(stack != 0);

	if(cry_cast(struct Stack*, stack)->begin == 0)
		return 0;
	else
		return cry_cast(struct Stack*, stack)->begin->data;
}


pointer
cry_stack_pop(struct CryStack* stack)
{
	assert(stack != 0);

	struct Stack* list = cry_cast(struct Stack*, stack);
	struct Node* node = list->begin;

	if(node == 0)
		return 0;

	pointer data = node->data;

	list->begin = node->next;

	cry_free(node);

	return data;
}


void
cry_stack_push(struct CryStack* stack, pointer data)
{
	assert(stack != 0);

	struct Stack* list = cry_cast(struct Stack*, stack);
	struct Node* node = cry_malloc(struct Node);
	
	node->next = list->begin;
	node->data = data;

	list->begin = node;
}