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;
};
实现一个广义表
最新推荐文章于 2024-04-02 00:08:21 发布