enum NodeType //枚舉類型
{
HEAD_TYPE, // 頭結點
DATA_TYPE, // 數據結點
SUB_TYPE, // 岔路結點
};
struct GeneralListNode //結點結構體
{
NodeType _type; //類型(枚舉)
GeneralListNode *_next; //指向下一個結點的指針
union //聯合體 ,當類型爲DATA_TYPE時,爲_data,當類型爲SUB_TYPE,爲_subLink
{
char _data; //數據
GeneralListNode * _subLink; //指向岔路的指針
};
GeneralListNode(NodeType type = HEAD_TYPE, char data = '\0')//結點構造函數
:_type(type)
, _next(NULL)
{
if (type == DATA_TYPE)
{
_data = data;
}
if (type == SUB_TYPE)
{
_subLink = NULL;
}
}
~GeneralListNode()
{
_next = NULL;
_subLink = NULL;
}
};
class GeneralList //定義類(廣義表)
{
public:
GeneralList(const char * str) //構造函數
:_head(NULL)
{
_CreatGeneralList(_head, str); //調用創建廣義表函數
}
GeneralList(const GeneralList & g) //拷貝構造
:_head(NULL)
{
_RCreatGeneralList(_head,g._head);
}
GeneralList & operator=(GeneralList g) //賦值運算符的重載函數
{
swap(_head, g._head);//直接拿實參構造臨時對象,然後將this和臨時對象的內容交換
}
~GeneralList() //析構函數
{
Destory(_head); //調用銷燬函數
}
void Print() //打印
{
_print(_head); //調用打印函數
cout << endl;
}
int Size() //廣義表的數據個數
{
return _size(_head);
}
int Depth() //廣義表的深度
{
return _depth(_head);
}
protected:
void Destory(GeneralListNode * head) //銷燬函數
{
GeneralListNode * cur = head;
while (cur) //循環條件:cur不爲空
{
GeneralListNode * tmp = cur;
cur = cur->_next;
if (tmp->_type == SUB_TYPE) //當結點爲岔路結點時遞歸調用銷燬函數
{
Destory(tmp->_subLink);
}
else //否則刪除結點
{
delete tmp;
}
}
}
void _RCreatGeneralList(GeneralListNode*head, GeneralListNode* ghead)//拷貝構造創建函數
{
if (ghead) //參數入口檢測,當形參不爲空時
{
head = new GeneralListNode;
GeneralListNode* cur = head;
GeneralListNode* gcur = ghead->_next;
while (gcur) // 循環條件:gcur 不爲空
{
if (gcur->_type == HEAD_TYPE) //gcur是什麼類型,就給cur構造什麼類型的結點
{
cur->_next = new GeneralListNode;
cur = cur->_next;
}
if (gcur->_type == DATA_TYPE)
{
cur->_next = new GeneralListNode(DATA_TYPE, cur->_data);
cur = cur->_next;
}
if (gcur->_type == SUB_TYPE) //當gcur的類型爲岔路結點時,遞歸調用拷貝構造創建函數
{
cur->_next = new GeneralListNode(SUB_TYPE);
cur = cur->_next;
_RCreatGeneralList(cur->_subLink, gcur->_subLink);
}
gcur = gcur->_next;
}
}
}
int _depth(GeneralListNode * head)//計算廣義表深度函數
{
GeneralListNode *cur = head;
int depth = 1; //初始化depth爲1
while (cur)
{
if (cur->_type == SUB_TYPE) //當cur的類型爲岔路類型時,遞歸調用
{
int tmp = _depth(cur->_subLink); //tmp接收函數返回值
if (tmp >= depth) //當返回值大於depth時,就更新depth的值
{
depth = tmp+1;
}
}
cur = cur->_next;
}
return depth;
}
int _size(GeneralListNode * head) //計算廣義表大小函數
{
GeneralListNode * cur = head;
int size = 0;
while (cur)
{
if (cur->_type == DATA_TYPE) //當cur的類型爲數據是,size++
{
size++;
}
if (cur->_type == SUB_TYPE) //當cur的類型爲岔路類型時,遞歸調用求岔路的大小,然後將返回值加到size
{
size += _size(cur->_subLink);
}
cur = cur->_next;
}
return size;
}
void _print(GeneralListNode * head) //打印函數
{
GeneralListNode * cur = head;
while (cur)
{
if (cur->_type == HEAD_TYPE)
{
cout << '(';
}
if (cur->_type == DATA_TYPE)
{
cout << cur->_data;
if (cur->_next != NULL)
{
cout << ',';
}
}
if (cur->_type == SUB_TYPE)
{
_print(cur->_subLink);
if (cur->_next!= NULL)
cout << ',';
}
cur = cur->_next;
}
cout << ')';
}
void _CreatGeneralList(GeneralListNode * & link, const char *& str)//構造創建廣義表函數
{
link = new GeneralListNode;
GeneralListNode *cur = link;
str++;
while (str)
{
if (*str == '(') //當 *str爲‘(’時,構造岔路結點,然後遞歸調用構造岔路
{
cur->_next = new GeneralListNode(SUB_TYPE);
cur = cur->_next;
_CreatGeneralList(cur->_subLink, str);
}
if (*str == ')' || *str == '\0')
{
str++;
return;
}
if (*str > 'A'&&*str < 'z')
{
cur->_next = new GeneralListNode(DATA_TYPE, *str);
cur = cur->_next;
}
str++;
}
}
protected:
GeneralListNode *_head;
};
實現一個廣義表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.