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