您的位置:首页 > 其它

广义表

2015-11-20 17:26 239 查看


GeneralList.h

#pragma once
#include<iostream>
using namespace std;

enum NodeType
{
Head_Type,  //头结点
Value_Type, //值节点
Sub_Type,  //子表节点
};

struct GeneralListNode  //广义表节点
{
NodeType _type;   //节点类型
GeneralListNode* _next;
union
{
char _value;    //节点的值
GeneralListNode* _subLink;
};

GeneralListNode(NodeType type = Head_Type, char value = '\0')
:_type(type)
, _next(NULL)
, _value(value)
, _subLink(NULL)
{}
};

class GeneralList
{
public:
GeneralList(const char* str)
:_link(NULL)
{
_CreatGeneralList(_link,str);
}
GeneralList( GeneralList& gl)
{
_link = _CopyGeneralList(gl._link);
}
GeneralList& operator=( GeneralList& gl)
{
if (&gl != this)
{
_Destory(_link);
_link = _CopyGeneralList(gl._link);
}
return *this;
}
~GeneralList()
{
_Destory(_link);
}
int Size() //求广义表节点数
{
return _Size(_link);
}
int Depth() //求广义表的深度
{
return _Depth(_link);
}
void Print()
{
_Print(_link);
cout << endl;
}
protected:
void _CreatGeneralList(GeneralListNode*& link, const char*& str)
{
if (*str != '(' || *str == '\0')
{
cout << "Invaild GeneralList" << endl;
return;
}
GeneralListNode* Head = new GeneralListNode(Head_Type);
link = Head;
GeneralListNode* begin = Head;
str++;
while (*str)
{
//值节点
if (*str >= 0 && *str <= 9 || *str >= 'a' && *str <= 'z' || *str >= 'A' && *str <= 'Z')
{
GeneralListNode* tmp = new GeneralListNode(Value_Type);
tmp->_value = *str;
begin->_next = tmp;
begin = begin->_next;
str++;
}
else if (*str == '(')
{
GeneralListNode* SubNode = new GeneralListNode(Sub_Type);
begin->_next = SubNode;
begin = begin->_next;
_CreatGeneralList(begin->_subLink, str);
}
else if (*str == ')')
{
str++;
return;
}
else //处理 ,空格 等情况
{
str++;
}
}
}
int _Size(GeneralListNode*& link)
{
int size = 0;
GeneralListNode* cur = link;
while (cur)
{
if (cur->_type == Value_Type)
{
size++;
}
else if (cur->_type == Sub_Type)
{
size += _Size(cur->_subLink);
}
cur = cur->_next;
}
return size;
}
int _Depth(GeneralListNode*& link)
{
int depth = 1;
GeneralListNode* cur = link;
while (cur)
{
if (cur->_type == Sub_Type)
{
int subDep = _Depth(cur->_subLink);
if (subDep + 1 > depth)
{
depth = subDep + 1;
}
}
cur = cur->_next;
}
return depth;
}
void _Print(GeneralListNode*& link)
{
GeneralListNode* cur = link;
cout << "(";
while (cur)
{
if (cur->_type == Value_Type)
{
cout << cur->_value;
if (cur->_next != NULL)
{
cout << ",";
}
}
else if (cur->_type == Sub_Type)
{
_Print(cur->_subLink);
if (cur->_next != NULL)
{
cout << ",";
}
}
cur = cur->_next;
}
cout <<")";
}
void _Destory(GeneralListNode*& link)
{
GeneralListNode* cur = link;
while (cur)
{
GeneralListNode* del = cur;
cur = cur->_next;
delete del;
if (cur == NULL)
{
return;
}
if (cur->_type == Sub_Type)
{
_Destory(cur->_subLink);
}
}
}
GeneralListNode*& _CopyGeneralList(GeneralListNode*& link)
{
GeneralListNode* Head = new GeneralListNode(Head_Type);
GeneralListNode* CopyHead = Head;
GeneralListNode* begin = CopyHead;
while (link)
{
if (link->_type == Value_Type)
{
GeneralListNode* tmp = new GeneralListNode(Value_Type);
tmp->_value = link->_value;
begin->_next = tmp;
begin = begin->_next;
}
else if (link->_type == Sub_Type)
{
GeneralListNode* SubNode = new GeneralListNode(Sub_Type);
begin->_next = SubNode;
begin = begin->_next;
begin->_subLink = _CopyGeneralList(link->_subLink);
}
link = link->_next;
}
return CopyHead;
}
private:
GeneralListNode* _link;
};


main.cpp

#include"GeneralList.h"

void Test()
{
char* str1 = "()";
GeneralList gl1(str1);
int size1 = gl1.Size();
cout << "节点个数:" << size1 << endl;

char* str2 = "(a, b)";
GeneralList gl2(str2);
gl2.Print();
int size2 = gl2.Size();
cout << "节点个数:" << size2 << endl;

char* str3 = "(a,(c,d))";
GeneralList gl3(str3);
gl3.Print();
int size3 = gl3.Size();
cout << "节点个数:" << size3 << endl;

char* str4 = "(a,(c,d),(e,(f),h))";
GeneralList gl4(str4);
gl4.Print();
int depth4 = gl4.Depth();
cout << "深度:" << depth4 << endl;
int size4 = gl4.Size();
cout << "节点个数:" << size4 << endl;

char* str5 = "(((), ()))";
GeneralList gl5(str5);
gl5.Print();
}

void Test1()
{
char* str = "(a,(c,d),(e,(f),h))";
GeneralList gl(str);
GeneralList gl1("(a, (c, d))");
gl = gl1;
gl1.Print();
}
int main()
{
Test1();
getchar();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: