File: AdLinkedList.c

package info (click to toggle)
adun.app 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,824 kB
  • ctags: 713
  • sloc: objc: 49,683; ansic: 4,680; sh: 523; python: 79; makefile: 67; cpp: 33
file content (156 lines) | stat: -rwxr-xr-x 3,678 bytes parent folder | download | duplicates (2)
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
 /*
   Project: Adun

   Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa

   Author: Michael Johnston

   This application 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; either
   version 2 of the License, or (at your option) any later version.

   This application 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
   Library General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/

#include <Base/AdLinkedList.h>
 
 
ListElement* AdLinkedListCreate(ListElement* liststart_p)
{
	ListElement *listend_p;
	 
	listend_p = (ListElement*)malloc(sizeof(ListElement));
	
	liststart_p->next = listend_p;
	liststart_p->previous = NULL;
	listend_p->previous = liststart_p;
	listend_p->next = NULL;
	 
	 return listend_p;
 }
 
inline int AdSafeLinkedListAdd(ListElement *list_el, ListElement *list_end, int index)
{
     	//check if we were passed the last element of the list
     
     	if(list_end->next != NULL)
	{
	    printf("This is not the last element of the list\n");
	    return -1;
	}

	return AdUnsafeLinkedListAdd(list_el, list_end, index);	
}

inline int AdUnsafeLinkedListAdd(ListElement *list_el, ListElement *list_end, int index)
{
	//insert the element by pointing its previous and last members at the 
	//second last and last members repectively
	
	list_el->next = list_end;
	list_el->previous = list_end->previous;
	
	//now do the same for the next memeber of the second last element
	//and the previous member of the last element
	
	list_end->previous = list_el;
	list_el->previous->next = list_el; 

	return 1;	
}

inline int AdUnsafeLinkedListReinsert(ListElement *list_el)
{
	//reinsert the element 	

	list_el->next->previous = list_el;
	list_el->previous->next = list_el;
	
	return 1;	
}

inline int AdSafeLinkedListRemove(ListElement *list_el)
{
   	//check if this is the first or last element
    
    	if(list_el->next == NULL || list_el->previous == NULL)
	{
		printf("You may be removing the first or last element!!\n");
		return -1;
	}
	
	return AdUnsafeLinkedListRemove(list_el);
}     

inline int AdUnsafeLinkedListRemove(ListElement *list_el)
{
    	/*
    	Remove the element by pointing the previous elements next
    	member to the next element and vice versa. Then set
    	list_els members to point to null
    	*/
    
    	(list_el->previous)->next = list_el->next;
    	(list_el->next)->previous = list_el->previous;
    
    	list_el->previous = NULL;
    	list_el->next = NULL;
	
	return 1;
}     

inline int AdUnsafeLinkedListExtract(ListElement *list_el)
{
    	(list_el->previous)->next = list_el->next;
    	(list_el->next)->previous = list_el->previous;
    
	return 1;
}     

inline ListElement* AdLinkedListEnd(ListElement *list_el)
{
	//search through the list until list_el->next == NULL
	
	while(list_el->next != NULL)
	{
		list_el = list_el->next;
	}

	return list_el;	
}
     
 
inline ListElement* AdLinkedListStart(ListElement *list_el)
{
 	//search through the list until list_el->previous == NULL
	
	while(list_el->previous != NULL)
	{
		list_el = list_el->previous;
	}

	return list_el;	     
}

inline int AdLinkedListCount(ListElement *list_el)
{
	int count;
	
	count = 0;
	
	while(list_el->next != NULL)
	{
		list_el = list_el->next;
		count++;
	}
	
	return count;
}