[go: up one dir, main page]

Menu

[r353]: / libpetey / linked.cc  Maximize  Restore  History

Download this file

211 lines (184 with data), 4.0 kB

  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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
#include <assert.h>
#include <stdlib.h>
#include "time_class.h"
#include "string_petey.h"
#include "linked.h"
#pragma implementation
namespace libpetey {
template <class ds>
linked_list<ds>::linked_list() {
head=NULL;
tail=head;
current=head;
n_elements=0;
}
template <class ds>
linked_list<ds>::~linked_list() {
reset();
}
//reset the linked list:
template <class ds>
void linked_list<ds>::reset() {
ll_element<ds> *prev;
if (n_elements==0) return;
//recursive algorithm eats up stack space
//(can cause segmentation fault for large lists...):
//head->del();
//delete head;
prev=head;
current=head;
for (long i=1; i<n_elements; i++) {
current=current->next;
delete prev;
prev=current;
}
assert(current==tail);
delete current;
n_elements=0;
head=NULL;
tail=head;
current=head;
}
template <class ds>
//returns the number of elements in the list if successful
//otherwise, returns -1
long linked_list<ds>::push(ds data) {
ll_element<ds> *tween;
if (head==NULL) {
head=new ll_element<ds>(data);
if (head==NULL) return -1;
tail=head;
} else {
tween=head;
head=new ll_element<ds>(data);
if (head==NULL) {
head=tween;
return -1;
}
head->next=tween;
}
current=head;
last=0;
return ++n_elements;
}
template <class ds>
long linked_list<ds>::pop(ds &data) {
ll_element<ds> *next;
if (n_elements==0L) {
return -1L;
} else {
data=head->data;
next=head->next;
delete head;
head=next;
current=head;
last=0;
return --n_elements;
}
}
template <class ds>
long linked_list<ds>::add(ds data) {
if (head==NULL) {
head=new ll_element<ds>(data);
if (head==NULL) return -1;
tail=head;
} else {
tail->next=new ll_element<ds>(data);
if (tail->next==NULL) return -1;
tail=tail->next;
}
current=tail;
last=++n_elements;
return n_elements;
}
template <class ds>
ds *linked_list<ds>::make_array(long &n) {
ds *array;
ll_element<ds> *current;
long i;
if (n_elements==0) {
n=0;
return NULL;
}
array=new ds[n_elements];
current=head;
for (i=0;i<n_elements;i++) {
array[i]=current->data;
current=current->next;
}
n=n_elements;
return array;
}
template <class ds>
ds linked_list<ds>::del (long n) {
ds data;
long i, n1;
ll_element<ds> * next;
if (head==NULL) return data;
if (n==0) {
data=head->data;
next=head->next;
delete head;
head=next;
n_elements--;
return data;
}
if (n>=last+1) {
n1=n-last-1;
} else {
n1=n-1;
current=head;
last=0;
}
i=0;
while (i < n1 && current->next != NULL) {
current=current->next;
i++;
}
last+=i;
if (current->next != NULL) {
if (current->next == tail) tail=current;
next=current->next->next;
data=current->next->data;
delete current->next;
current->next=next;
n_elements--;
} else {
printf("Warning: attempting to delete out-of-bounds element\n");
}
return data;
}
template <class ds>
ds linked_list<ds>::operator [] (long n) {
long i, n1;
if (n>=last) {
n1=n-last;
} else {
n1=n;
current=head;
last=0;
}
i=0;
while (i < n1 && current->next != NULL) {
current=current->next;
i++;
}
last+=i;
return current->data;
}
template class linked_list<float>;
template class linked_list<long>;
template class linked_list<int>;
template class linked_list<double>;
template class linked_list<time_class>;
template class linked_list<string_petey>;
template class linked_list<char *>;
template class linked_list<char>;
/*
template class ll_element<float>;
template class ll_element<long>;
template class ll_element<double>;
template class ll_element<time_class>;
template class ll_element<string_petey>;
*/
} //end namespace libpetey