-
Notifications
You must be signed in to change notification settings - Fork 6
/
clist.c
110 lines (98 loc) · 3.13 KB
/
clist.c
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
/*******************************************************************************
* Dynamic list in C *
* Copyright (C) 2011-averyfarwaydate Luca Bertoncello *
* Hartigstrasse, 12 - 01127 Dresden Deutschland *
* E-Mail: [email protected], [email protected] *
* http://www.lucabert.de/ http://www.lucabert.com/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "incs/clist.h"
/**
* Creates a new list
*
* @return struct cList* the pointer to the new created list
*/
struct cList *createCList()
{
struct cList *l;
l = malloc(sizeof(struct cList));
l->prev = l->next = NULL;
l->data = NULL;
return(l);
}
/**
* Add an element to the given list
*
* @param struct cList *list the list to add to
* @param void *data the data of the element (content)
*/
void addToCList(struct cList *list, void *data)
{
struct cList *l, *n;
if(list->data == NULL) // first insert
list->data = data;
else
{
l = list;
while(l->next != NULL)
l = l->next;
n = malloc(sizeof(struct cList));
n->data = data;
n->next = NULL;
n->prev = l;
l->next = n;
}
}
/**
* Delete an element from the given list
*
* @param struct cList *list the list to delete from
* @param struct cList *element the element to be deleted
* @return struct cList* the pointer to the new list
*/
struct cList *deleteFromCList(struct cList *list, struct cList *element)
{
struct cList *p, *n;
free(element->data);
p = element->prev;
n = element->next;
free(element);
if(p != NULL)
p->next = n;
else
list = n;
if(n != NULL)
n->prev = p;
return(list);
}
/**
* Destroy a List
*
* @param struct cList *list the list to be destroyed
*/
void destroyCList(struct cList *list)
{
struct cList *l, *n;
l = list;
while(l != NULL)
{
n = l->next;
free(l->data);
free(l);
l = n;
}
}