C++で一方向循環チェーンテーブルを実現する解決方法
3221 ワード
C++で一方向循環チェーンテーブルを実現し、コンソールから整数数字を入力し、単項循環チェーンテーブルに格納し、チェーンテーブルの大きさを求めることを実現した.
足りないところは,まだ指摘してほしい.
足りないところは,まだ指摘してほしい.
// TestSound.cpp : 。
//
#include "stdafx.h"
#include
#include
using namespace std;
//
template
struct NODE
{
T data;//
NODE* next;//
};
// ( C++ )
template
class MyList
{
public:
// , ,data ,next
MyList()
{
phead = new NODE;
phead->data = NULL;
phead->next = phead;
}
// , ,
~MyList()
{
NODE* p = phead->next;
while (p != phead)
{
NODE* q = p;
p = p->next;
delete q;
}
delete phead;
}
//
MyList(MyList& mylist)
{
NODE* q = mylist.phead->next;
NODE* pb = new NODE;
this->phead = pb;
while (q != mylist.phead)
{
NODE* p = new NODE;
p->data = q->data;
p->next = phead;
pb->next = p;
pb = p;
q = q->next;
}
}
// list
int get_size();
// integer , list
void push_back();
// list
void get_elements();
private:
NODE* phead;
};
// list
template
int MyList::get_size()
{
int count(0);
NODE* p = phead->next;
while (p != phead)
{
count ++;
p = p->next;
}
return count;
}
// integer , list
template
void MyList::push_back()
{
int i;
cout << "Enter several integer number, enter ctrl+z for the end: "<< endl;
NODE* p = phead;
while (cin >> i)
{
NODE* q = new NODE;
p->next = q;
q->data = i;
q->next = phead;
p = q;
}
}
// list
template
void MyList::get_elements()
{
NODE* q = phead->next;
while (q != phead)
{
cout << q->data << " ";
q = q->next;
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
MyList mylist;
mylist.push_back();
MyList mylist2(mylist);
mylist.get_elements();
mylist2.get_elements();
cout << endl << mylist.get_size() << endl;
return 0;
}