ここでは節点や要素を格納するリスト用としてクラス NodeList とクラス ElementList を定義します。
また整数のソート用クラス IntSort の定義を行います。ファイル fem.h に以下のコードをかきこんでください。
class NodeList
{
Node* node;
NodeList* next;
public:
NodeList()
{
node = NULL;
next = NULL;
}
NodeList(Node* node)
{
this->node = node;
next = NULL;
}
virtual ~NodeList()
{
if(this->next != NULL)
{
NodeList* p = this->next;
NodeList* q = p->next;
this->next = NULL;
while(p->next != NULL)
{
p->next = NULL;
delete p;
p = q;
q = p->next;
}
delete p;
}
}
Node* getNode() { return node; }
NodeList* getNext() { return next; }
NodeList* copy()
{
NodeList* nodelist = new NodeList(node);
NodeList* list = next;
while (list != NULL)
{
nodelist->add(list->node);
list = list->next;
}
return nodelist;
}
void add(Node* node)
{
if(this->node == NULL)
{
this->node = node;
return;
}
NodeList* list = this;
while(list->next != NULL)
{
if(list->node == node) return;
list = list->next;
}
if(list->node == node) return;
NodeList* nodelist = new NodeList(node);
list->next = nodelist;
}
int getNumlist()
{
int numlist = 0;
NodeList* list = this;
while (list != NULL)
{
numlist++;
list = list->next;
}
return numlist;
}
};
class ElementList
{
//要素リスト用クラス
Element* element;
ElementList* next;
public:
ElementList(Element* element)
{
this->element = element;
next = NULL;
}
virtual ~ElementList()
{
if(this->next != NULL)
{
ElementList* p = this->next;
ElementList* q = p->next;
this->next = NULL;
while(p->next != NULL)
{
p->next = NULL;
delete p;
p = q;
q = p->next;
}
delete p;
}
}
Element* getElement() { return element; }
ElementList* getNext() { return next; }
ElementList* copy()
{
ElementList* elementlist = new ElementList(element);
ElementList* list = next;
while (list != NULL)
{
elementlist->add(list->element);
list = list->next;
}
return elementlist;
}
void add(Element* element)
{
ElementList* list = this;
while(list->next != NULL)
{
if(list->element == element) return;
list = list->next;
}
if(list->element == element) return;
ElementList* elementlist = new ElementList(element);
list->next = elementlist;
}
int getNumlist()
{
int numlist = 0;
ElementList* list = this;
while (list != NULL)
{
numlist++;
list = list->next;
}
return numlist;
}
};
class IntSort
{
//整数のソート用クラス
int num;
int* index;
int* data;
public:
IntSort(int num)
{
this->num = num;
index = new int[num];
data = new int[num];
}
virtual ~IntSort()
{
delete[] index;
delete[] data;
}
void setData(int* index, int* data)
{
for (int n=0; n<num; n++)
{
this->index[n] = index[n];
this->data[n] = data[n];
}
}
void getIndex(int* index)
{
for (int n=0; n<num; n++)
index[n] = this->index[n];
}
void getData(int* data)
{
for (int n=0; n<num; n++)
data[n] = this->data[n];
}
void quick_sort(int left, int right)
{
int left_index = left;
int right_index = right;
double pivot = data[(left + right)/2];
while (left_index <= right_index)
{
for (; data[left_index] < pivot; left_index++)
{
;
}
for (; data[right_index] > pivot; right_index--)
;
if (left_index <= right_index)
{
swap(index[left_index], index[right_index]);
swap(data[left_index], data[right_index]);
left_index++;
right_index--;
}
}
if (right_index > left)
{
quick_sort(left, right_index);
}
if (left_index < right)
{
quick_sort(left_index, right);
}
}
};
これらのクラスのメンバー関数は実装されているのでファイル fem.cpp へのかきこみは不要です。