#pragma once //头文件
#include<iostream>
using namespace std;
enum GeneralizedType
{
_HEAD,
_VALUE,
_SUB,
};
struct GeneralizedNode
{
public:
GeneralizedNode* _next;
GeneralizedType _type;
union
{
char _value;
GeneralizedNode* _subLink;
}Data;
public:
GeneralizedNode(GeneralizedType type,char ch);
~GeneralizedNode();
};
class GeneralizedList
{
public:
GeneralizedNode* _head;
char* _str;
protected:
GeneralizedNode* _CreatNode(char*& str);
int _Size(GeneralizedNode* tmp);
int _Depth(GeneralizedNode* tmp);
void _Print(GeneralizedNode* tmp);
void _Clear(GeneralizedNode* tmp);
public:
GeneralizedList(const GeneralizedList& tmp);
GeneralizedList( char* str);
~GeneralizedList();
int Size();
int Depth();
void Print();
void Clear();
};
#include"generalizedlist.h"//函数文件
#include<assert.h>
#include <ctype.h>
GeneralizedNode::GeneralizedNode(GeneralizedType type,char ch)
:_type(type)
, _next(NULL)
{
if (_type == _VALUE)
Data._value = ch;
else
Data._subLink = NULL;
}
GeneralizedNode::~GeneralizedNode()
{}
GeneralizedList::GeneralizedList(char* str)
:_str(str)
, _head(NULL)
{
_head = _CreatNode(_str);
}
GeneralizedList::~GeneralizedList()
{
Clear();
}
GeneralizedNode* GeneralizedList::_CreatNode(char*& str)
{
assert(*str == '(');
GeneralizedNode* head = new GeneralizedNode(_HEAD,*str);
GeneralizedNode* cur = head;
while (*str != '\0')
{
++str;
if (*str == '(')
{
cur->_next = new GeneralizedNode(_SUB,*str);
cur = cur->_next;
cur->Data._subLink=_CreatNode(str);
}
else if (*str == ')')
return head;
else if (isalnum(*str))
{
cur->_next = new GeneralizedNode(_VALUE,*str);
cur = cur->_next;
}
}
return head;
}
int GeneralizedList:: Size()
{
return _Size(_head);
}
int GeneralizedList::_Size(GeneralizedNode * tmp)
{
int i = 0;
GeneralizedNode* cur = tmp;
while (cur)
{
if (cur->_type == _VALUE)
i++;
if (cur->_type==_SUB)
i += _Size(cur->Data._subLink);
cur = cur->_next;
}
return i;
}
int GeneralizedList::Depth()
{
return _Depth(_head);
}
int GeneralizedList::_Depth(GeneralizedNode* tmp)
{
int count=0;
int i = 1;
GeneralizedNode* cur = tmp;
while (cur)
{
count = 1;//避免0层
if (cur->_type == _SUB)
{
i += _Depth(cur->Data._subLink);//递归
count = i > count ? i : count; //将count置成自己与i中大的
i = 1;//将count重新置1
}
cur = cur->_next;
}
return count;
}
void GeneralizedList::Print()
{
_Print(_head);
cout << endl;
}
void GeneralizedList::_Print(GeneralizedNode* tmp)
{
if (tmp == NULL)
return;
GeneralizedNode* cur = tmp;
while (cur)
{
if (cur->_type == _VALUE)
{
cout << cur->Data._value;
cout << ' ';
}
if (cur->_type == _HEAD)
cout << '(';
if (cur->_type == _SUB)
_Print(cur->Data._subLink);
cur = cur->_next;
}
cout << ')';
return;
}
void GeneralizedList::Clear()
{
_Clear(_head);
_head = NULL;
}
void GeneralizedList::_Clear(GeneralizedNode* tmp)
{
GeneralizedNode* cur = tmp;
while (cur)
{
if (cur->_type != _SUB)
{
GeneralizedNode* tmp = cur->_next;
delete cur;
cur = tmp;
}
else if (cur->_type == _SUB)
{
_Clear(cur->Data._subLink);
GeneralizedNode* tmp = cur->_next;
delete cur;
cur = tmp;
}
}
return ;
}
#include"generalizedlist.h" //主函数测试文件
void test()
{
//GeneralizedList s("(a)");
//GeneralizedList s("(a ,b,(c,d),e(f),(g,(h,i,(k(j(l))))))");
GeneralizedList s("(q(v)(g,(h,i,(k))))");
cout << s.Size() << endl;
cout << s.Depth() << endl;
s.Print() ;
s.Clear();
s.Print();
}
int main()
{
test();
return 0;
}
转载于:https://blog.51cto.com/shaungqiran/1747861