ループチェーンテーブルとそのインタフェース関数のC++実装
転載は出典を明記してください.http://blog.csdn.net/hongkangwl/article/details/22284249
まずノードの構造体を作成する
次に、ループチェーンテーブルのクラスで必要なメンバーとメソッドを宣言します.
また挿入操作があって、囧~~毎回挿入するのはいつも最も面倒です
削除操作時も同様に0ノードに注意
以下に、完全なインプリメンテーションとテストプログラムを示します.
テスト正解~~~
まずノードの構造体を作成する
struct node
{
int date;
node* ptrnext;
};
次に、ループチェーンテーブルのクラスで必要なメンバーとメソッドを宣言します.
class circlelinklist
{
private:
int length;
// int capacity;
public:
node* root;
// node* nodeptr;
circlelinklist()
{
length = 0;
// capacity = 0;
root = new node;
root->ptrnext = NULL;
// nodeptr = NULL;
}
int circlelinklist_insertnode(circlelinklist* ptr, int position, int member);
int circlelinklist_delectnode(circlelinklist* ptr, int position);
int circlelinklist_getnode(circlelinklist* ptr, int position);
bool circlelinklist_empty(circlelinklist* ptr);
int circlelinklist_getlength(circlelinklist* ptr);
void circlelinklist_display(circlelinklist* ptr, int num);
};
また挿入操作があって、囧~~毎回挿入するのはいつも最も面倒です
int circlelinklist::circlelinklist_insertnode(circlelinklist *ptr, int position, int member)
{
node* nodeinsert = new node;
nodeinsert->date = member;
node* current = root->ptrnext;
// node* back;
if (root->ptrnext == NULL) //
{
root->ptrnext = nodeinsert;
nodeinsert->ptrnext = nodeinsert;
ptr->length++;
return 0;
}
else
{
if (position == 0)//0 , 0 ,
{
while ((current->ptrnext) != (root->ptrnext))
{
current = current->ptrnext;
}
nodeinsert->ptrnext = root->ptrnext;
root->ptrnext = nodeinsert;
current->ptrnext = nodeinsert;
/*for (int i = 0; i < ptr->circlelinklist_getlength(ptr) ; i++ )
{
current = current ->ptrnext;
}
current->ptrnext = nodeinsert;
nodeinsert->ptrnext = root->ptrnext;
root->ptrnext = nodeinsert;*/
ptr->length++;
return 0;
}
else//
{
for (int i = 0; i < position - 1 ; i++)
{
current =current->ptrnext;
}
nodeinsert->ptrnext = current->ptrnext;
current->ptrnext = nodeinsert;
ptr->length++;
return 0;
}
}
}
削除操作時も同様に0ノードに注意
int circlelinklist::circlelinklist_delectnode(circlelinklist* ptr, int position)
{
if (ptr->circlelinklist_getlength(ptr) == 0)
{
cout<<" oop!!"<<endl;
return 0;
}
else if (ptr->circlelinklist_getlength( ptr) == 1)//
{
root->ptrnext =NULL;
ptr->length--;
}
else
{
if (position == 0)//0
{
node* current = root->ptrnext;
while (current->ptrnext != root->ptrnext)
{
current = current->ptrnext;
}
root->ptrnext = root->ptrnext->ptrnext;
current->ptrnext = root->ptrnext;
ptr->length--;
}
else
{
node* current = root->ptrnext;
for (int i = 0; i < position - 1; i++)
{
current =current ->ptrnext;
}
current->ptrnext = current->ptrnext->ptrnext;
ptr->length--;
}
}
}
以下に、完全なインプリメンテーションとテストプログラムを示します.
#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
int date;
node* ptrnext;
};
class circlelinklist
{
private:
int length;
// int capacity;
public:
node* root;
// node* nodeptr;
circlelinklist()
{
length = 0;
// capacity = 0;
root = new node;
root->ptrnext = NULL;
// nodeptr = NULL;
}
int circlelinklist_insertnode(circlelinklist* ptr, int position, int member);
int circlelinklist_delectnode(circlelinklist* ptr, int position);
int circlelinklist_getnode(circlelinklist* ptr, int position);
bool circlelinklist_empty(circlelinklist* ptr);
int circlelinklist_getlength(circlelinklist* ptr);
void circlelinklist_display(circlelinklist* ptr, int num);
};
int circlelinklist::circlelinklist_delectnode(circlelinklist* ptr, int position)
{
if (ptr->circlelinklist_getlength(ptr) == 0)
{
cout<<" oop!!"<<endl;
return 0;
}
else if (ptr->circlelinklist_getlength( ptr) == 1)//
{
root->ptrnext =NULL;
ptr->length--;
}
else
{
if (position == 0)//0
{
node* current = root->ptrnext;
while (current->ptrnext != root->ptrnext)
{
current = current->ptrnext;
}
root->ptrnext = root->ptrnext->ptrnext;
current->ptrnext = root->ptrnext;
ptr->length--;
}
else
{
node* current = root->ptrnext;
for (int i = 0; i < position - 1; i++)
{
current =current ->ptrnext;
}
current->ptrnext = current->ptrnext->ptrnext;
ptr->length--;
}
}
}
void circlelinklist::circlelinklist_display(circlelinklist *ptr, int num)
{
node* current = root->ptrnext;
for (int i = 0; i < num; i++)
{
cout<<current->date<<" ";
current = current->ptrnext;
}
cout<<endl;
}
int circlelinklist::circlelinklist_getlength(circlelinklist* ptr)
{
return ptr->length;
}
int circlelinklist::circlelinklist_insertnode(circlelinklist *ptr, int position, int member)
{
node* nodeinsert = new node;
nodeinsert->date = member;
node* current = root->ptrnext;
// node* back;
if (root->ptrnext == NULL) //
{
root->ptrnext = nodeinsert;
nodeinsert->ptrnext = nodeinsert;
ptr->length++;
return 0;
}
else
{
if (position == 0)//0 , 0 ,
{
while ((current->ptrnext) != (root->ptrnext))
{
current = current->ptrnext;
}
nodeinsert->ptrnext = root->ptrnext;
root->ptrnext = nodeinsert;
current->ptrnext = nodeinsert;
/*for (int i = 0; i < ptr->circlelinklist_getlength(ptr) ; i++ )
{
current = current ->ptrnext;
}
current->ptrnext = nodeinsert;
nodeinsert->ptrnext = root->ptrnext;
root->ptrnext = nodeinsert;*/
ptr->length++;
return 0;
}
else//
{
for (int i = 0; i < position - 1 ; i++)
{
current =current->ptrnext;
}
nodeinsert->ptrnext = current->ptrnext;
current->ptrnext = nodeinsert;
ptr->length++;
return 0;
}
}
}
int main()
{
circlelinklist* CircleListObject = new circlelinklist;
for (int i = 0; i < 8; i++)
{
CircleListObject->circlelinklist_insertnode(CircleListObject,i,i);
}
CircleListObject->circlelinklist_display(CircleListObject,20);
CircleListObject->circlelinklist_delectnode(CircleListObject,2);
CircleListObject->circlelinklist_display(CircleListObject,20);
CircleListObject->circlelinklist_delectnode(CircleListObject,0);
CircleListObject->circlelinklist_display(CircleListObject,20);
return 0;
}
テスト正解~~~