1 |
#include "StringLinkedList.h"
|
2 |
#include "../System/SystemDefine.h"
|
3 |
#include "../String/StringTools.h"
|
4 |
#include "../Sort/StringLinkedListQuickSort.h"
|
5 |
|
6 |
StringLinkedList::StringLinkedList()
|
7 |
{
|
8 |
howfar=0;
|
9 |
}
|
10 |
|
11 |
////////////////////////////////////////////////////////////////////////////////////
|
12 |
//free memory for all Elements that exsist in the LinkedList
|
13 |
////////////////////////////////////////////////////////////////////////////////////
|
14 |
|
15 |
StringLinkedList::~StringLinkedList()
|
16 |
{
|
17 |
removeAllElement();
|
18 |
}
|
19 |
|
20 |
////////////////////////////////////////////////////////////////////////////////////
|
21 |
//free memory for all Elements that exsist in the LinkedList
|
22 |
////////////////////////////////////////////////////////////////////////////////////
|
23 |
|
24 |
|
25 |
void StringLinkedList::removeAllElement()
|
26 |
{
|
27 |
|
28 |
if ( howfar != 0 )
|
29 |
{
|
30 |
StringElement *element=first;
|
31 |
|
32 |
for ( int s=0; s<howfar ; s++ )
|
33 |
{
|
34 |
StringElement *tempElement=element->next;
|
35 |
delete element;
|
36 |
element=tempElement;
|
37 |
}
|
38 |
}
|
39 |
|
40 |
}
|
41 |
|
42 |
////////////////////////////////////////////////////////////////////////////////////
|
43 |
//How many Elements is it in the LinkedList
|
44 |
////////////////////////////////////////////////////////////////////////////////////
|
45 |
|
46 |
int StringLinkedList::size()
|
47 |
{
|
48 |
return howfar;
|
49 |
}
|
50 |
|
51 |
////////////////////////////////////////////////////////////////////////////////////
|
52 |
//add a Element to the LinkedList
|
53 |
////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
55 |
void StringLinkedList::addLast(String &iString)
|
56 |
{
|
57 |
addLastMain(iString.buffer,iString.length);
|
58 |
}
|
59 |
|
60 |
void StringLinkedList::addLast(String *iString)
|
61 |
{
|
62 |
addLastMain(iString->buffer,iString->length);
|
63 |
}
|
64 |
|
65 |
void StringLinkedList::addLast(char *iBuffer)
|
66 |
{
|
67 |
StringTools stringTools;
|
68 |
addLastMain(iBuffer,stringTools.sizeOfStringWithOutEndNull(iBuffer));
|
69 |
}
|
70 |
|
71 |
|
72 |
void StringLinkedList::addLastMain(char *iBuffer, int length)
|
73 |
{
|
74 |
if (howfar == 0)
|
75 |
{
|
76 |
first = new StringElement(iBuffer,length);
|
77 |
last=first;
|
78 |
}
|
79 |
else
|
80 |
{
|
81 |
last->next = new StringElement(iBuffer,length);
|
82 |
last->next->previos=last;
|
83 |
last=last->next;
|
84 |
}
|
85 |
|
86 |
howfar++;
|
87 |
|
88 |
}
|
89 |
|
90 |
////////////////////////////////////////////////////////////////////////////////////
|
91 |
//get a Element from the LinkedList
|
92 |
////////////////////////////////////////////////////////////////////////////////////
|
93 |
|
94 |
char *StringLinkedList::getElement(int index)
|
95 |
{
|
96 |
|
97 |
if ( index >= 0 && index < howfar)
|
98 |
{
|
99 |
StringElement *element=first;
|
100 |
|
101 |
for ( int s=0; s<index ; s++ )
|
102 |
{
|
103 |
element=element->next;
|
104 |
}
|
105 |
|
106 |
stringLength=element->length;
|
107 |
|
108 |
return element->buffer;
|
109 |
}
|
110 |
|
111 |
return null;
|
112 |
}
|
113 |
|
114 |
|
115 |
////////////////////////////////////////////////////////////////////////////////////
|
116 |
//set a Element in the LinkedList
|
117 |
////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
119 |
void StringLinkedList::setElement( char *iBuffer, int iLength, int index)
|
120 |
{
|
121 |
if ( index < howfar && index >= 0)
|
122 |
{
|
123 |
StringElement *element=first;
|
124 |
|
125 |
for ( int s=0; s<index ; s++ )
|
126 |
{
|
127 |
element=element->next;
|
128 |
}
|
129 |
|
130 |
char *tempBuffer=element->buffer;
|
131 |
element->createElement(iBuffer,iLength);
|
132 |
free(tempBuffer);
|
133 |
}
|
134 |
}
|
135 |
|
136 |
////////////////////////////////////////////////////////////////////////////////////
|
137 |
//sort element in linkedlist
|
138 |
////////////////////////////////////////////////////////////////////////////////////
|
139 |
|
140 |
void StringLinkedList::sortLargestOrder()
|
141 |
{
|
142 |
StringLinkedListQuickSort::setStatement(1);
|
143 |
StringLinkedListQuickSort::sortLargestOrder(this,0,howfar-1);
|
144 |
}
|
145 |
|
146 |
void StringLinkedList::sortSmallestOrder()
|
147 |
{
|
148 |
StringLinkedListQuickSort::setStatement(0);
|
149 |
StringLinkedListQuickSort::sortSmallestOrder(this,0,howfar-1);
|
150 |
}
|
151 |
|
152 |
////////////////////////////////////////////////////////////////////////////////////
|
153 |
//remove a element from the linkedlist
|
154 |
////////////////////////////////////////////////////////////////////////////////////
|
155 |
|
156 |
bool StringLinkedList::removeElement(int index)
|
157 |
{
|
158 |
if ( index < howfar && index >= 0)
|
159 |
{
|
160 |
int temphowfar=howfar;
|
161 |
--howfar;
|
162 |
StringElement *lista = first;
|
163 |
|
164 |
for ( int w=0; w<index ; w++ )
|
165 |
{
|
166 |
lista=lista->next;
|
167 |
}
|
168 |
|
169 |
if ( lista == first && temphowfar == 1 )
|
170 |
{
|
171 |
delete first;
|
172 |
return true;
|
173 |
}
|
174 |
|
175 |
|
176 |
if ( lista == first && temphowfar != 1 )
|
177 |
{
|
178 |
bool s=false;
|
179 |
|
180 |
if ( lista->next == last )
|
181 |
{
|
182 |
s=true;
|
183 |
}
|
184 |
|
185 |
StringElement *temp=first->next;
|
186 |
delete first;
|
187 |
first=temp;
|
188 |
|
189 |
if (s == true)
|
190 |
{
|
191 |
last=first;
|
192 |
}
|
193 |
|
194 |
return true;
|
195 |
}
|
196 |
|
197 |
if ( lista == last )
|
198 |
{
|
199 |
StringElement *temp=last->previos;
|
200 |
delete last;
|
201 |
last=temp;
|
202 |
return true;
|
203 |
}
|
204 |
|
205 |
int t=0;
|
206 |
|
207 |
if ( lista->next == last && lista->previos == first )
|
208 |
{
|
209 |
t=1;
|
210 |
}
|
211 |
|
212 |
|
213 |
StringElement *tempnext=lista->previos;
|
214 |
StringElement *tempprevios=lista->next;
|
215 |
delete lista;
|
216 |
tempnext->next=tempprevios;
|
217 |
tempprevios->previos=tempnext;
|
218 |
|
219 |
if ( t == 1 )
|
220 |
{
|
221 |
first=tempnext;
|
222 |
last=tempprevios;
|
223 |
}
|
224 |
|
225 |
return true;
|
226 |
}
|
227 |
|
228 |
return false;
|
229 |
}
|