广义表是线性表的扩展,是有n个元素组成有限序列。

广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表。
<1> A = ()
<2> B = (a,b)
<3> C = (a,b,(c,d))
<4> D = (a,b,(c,d),(e,(f),h))
#include<iostream>
using namespace std;
enum Type //结点类型
{
HEAD_TYPE, //头结点
VALUE_TYPE,// 值结点
SUB_TYPE, //子表结点
};
struct GeneralListNode
{
Type _type;
GeneralListNode *_next;
union
{
char _value;
GeneralListNode *_sublink;
};
GeneralListNode(Type type, char ch = '\0')
:_type(type)
, _next(NULL)
{
if (type == VALUE_TYPE)
{
_value = ch;
}
if (type == SUB_TYPE)
{
_sublink = NULL;
}
}
};
class GeneralList
{
public:
GeneralList(const char *str)
{
if (StrIsMatch(str))
{
_CreatGeneralList(_link, str);
}
}
GeneralList(const GeneralList &gl)
{
_CopyGeneralList(_link, gl._link);
}
GeneralList &operator=(const GeneralList &gl)
{
if (this != &gl)
{
this->Destroy();
_CopyGeneralList(_link, gl._link);
}
return *this;
}
void Destroy()
{
_Destroy(_link);
}
~GeneralList()
{
Destroy();
}
void Display()
{
_Display(_link);
cout << endl;
}
int ValueSize()//值结点个数
{
return _ValueSize(_link);
}
int Depth() //广义表深度
{
return _Depth(_link);
}
protected:
bool StrIsMatch(const char *str)
{
int len = strlen(str);
if (*str != '(' || str[len - 1] != ')')
{
return false;
}
int i = 1;
int count = 0;
while (i < len-1)
{
if (str[i] == '(')
{
count++;
}
if (str[i] == ')'&&count==0)
{
return false;;
}
if (str[i] == ')'&&count != 0)
{
count--;
}
i++;
}
if (count == 0)
{
return true;
}
return false;
}
bool CheckIsValue(const char *str)
{
if (*str >= '0'&&*str <= '9'
|| *str >= 'a'&&*str <= 'z'
|| *str >= 'A'&&*str <= 'Z')
{
return true;
}
return false;
}
void _CreatGeneralList(GeneralListNode * &link,const char * &str)
{
GeneralListNode *head = new GeneralListNode(HEAD_TYPE);
link = head;
GeneralListNode *begin = head;
str++;
while (*str != '\0')
{
if (CheckIsValue(str))
{
GeneralListNode *valueNode = new GeneralListNode(VALUE_TYPE, *str);
begin->_next = valueNode;
begin = begin->_next;
str++;
}
else if (*str == '(')
{
GeneralListNode *subNode = new GeneralListNode(SUB_TYPE);
begin->_next = subNode;
begin = begin->_next;
_CreatGeneralList(begin->_sublink,str);
str++;
}
else if (*str == ')')
{
++str;
return;
}
else
{
str++;
}
}
}
void _CopyGeneralList(GeneralListNode *&link, GeneralListNode *clink)
{
GeneralListNode *begin = clink;
GeneralListNode *cur = NULL;
while (begin)
{
if (begin->_type == HEAD_TYPE)
{
GeneralListNode *head = new GeneralListNode(HEAD_TYPE);
link = head;
cur = head;
}
else if (begin->_type == VALUE_TYPE)
{
cur->_next = new GeneralListNode(VALUE_TYPE,begin->_value);
cur = cur->_next;
}
else
{
cur->_next = new GeneralListNode(SUB_TYPE);
cur = cur->_next;
_CopyGeneralList(cur->_sublink, begin->_sublink);
}
begin = begin->_next;
}
}
void _Destroy(GeneralListNode *&link)
{
GeneralListNode *&begin = link;
while (begin)
{
if (begin->_type == SUB_TYPE)
{
_Destroy(begin->_sublink);
}
GeneralListNode *tmp = begin;
begin = begin->_next;
delete tmp;
}
}
int _ValueSize(GeneralListNode *link)
{
int size = 0;
GeneralListNode *begin = link;
while (begin)
{
if (begin->_type == VALUE_TYPE)
{
size++;
}
if (begin->_type == SUB_TYPE)
{
size += _ValueSize(begin->_sublink);
}
begin = begin->_next;
}
return size;
}
int _Depth(GeneralListNode *link)
{
GeneralListNode *cur = link;
int depth = 1;
while (cur)
{
if (cur->_type == SUB_TYPE)
{
int subdepth = _Depth(cur->_sublink);
if (subdepth + 1 > depth)
{
depth = subdepth + 1;
}
}
cur = cur->_next;
}
return depth;
}
void _Display(GeneralListNode *&link)
{
GeneralListNode *begin = link;
if (begin)
{
while (begin)
{
if (begin->_type == HEAD_TYPE)
{
cout << "(";
}
else if (begin->_type == VALUE_TYPE)
{
cout << begin->_value;
if (begin->_next)
{
cout << ",";
}
}
else if (begin->_type == SUB_TYPE)
{
_Display(begin->_sublink);
if (begin->_next)
{
cout << ",";
}
}
begin = begin->_next;
}
cout << ")";
}
}
private:
GeneralListNode *_link;
};
void Test()
{
const char *str1 = "()";
GeneralList pl1(str1);
pl1.Display();
const char *str2 = "(a,b))";
GeneralList pl2(str2);
pl2.Display();
const char *str3 = "(a,b,(c,d))";
GeneralList pl3(str3);
pl3.Display();
const char *str4 = "(a,b,(c,d),(e,(f),g))";
GeneralList pl4(str4);
pl4.Display();
cout << pl4.ValueSize() << endl;
cout << pl4.Depth()<< endl;
GeneralList pl5(pl4);
pl5.Display();
pl5.Destroy();
pl5.Display();
pl5 = pl3;
pl5.Display();
}
int main()
{
Test();
system("pause");
return 0;
}