# 广义表

158人阅读 评论(0)

GeneralList.h

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

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

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

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

class GeneralList
{
public:
GeneralList(const char* str)
{
}
GeneralList( GeneralList& gl)
{
}
GeneralList& operator=( GeneralList& gl)
{
if (&gl != this)
{
}
return *this;
}
~GeneralList()
{
}
int Size() //求广义表节点数
{
}
int Depth() //求广义表的深度
{
}
void Print()
{
cout << endl;
}
protected:
void _CreatGeneralList(GeneralListNode*& link, const char*& str)
{
if (*str != '(' || *str == '\0')
{
cout << "Invaild GeneralList" << endl;
return;
}
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;
}
else if (*str == ')')
{
str++;
return;
}
else //处理 ，空格 等情况
{
str++;
}
}
}
{
int size = 0;
while (cur)
{
if (cur->_type == Value_Type)
{
size++;
}
else if (cur->_type == Sub_Type)
{
}
cur = cur->_next;
}
return size;
}
{
int depth = 1;
while (cur)
{
if (cur->_type == Sub_Type)
{
if (subDep + 1 > depth)
{
depth = subDep + 1;
}
}
cur = cur->_next;
}
return depth;
}
{
cout << "(";
while (cur)
{
if (cur->_type == Value_Type)
{
cout << cur->_value;
if (cur->_next != NULL)
{
cout << ",";
}
}
else if (cur->_type == Sub_Type)
{
if (cur->_next != NULL)
{
cout << ",";
}
}
cur = cur->_next;
}
cout <<")";
}
{
while (cur)
{
GeneralListNode* del = cur;
cur = cur->_next;
delete del;
if (cur == NULL)
{
return;
}
if (cur->_type == Sub_Type)
{
}
}
}
{
{
{
GeneralListNode* tmp = new GeneralListNode(Value_Type);
begin->_next = tmp;
begin = begin->_next;
}
{
GeneralListNode* SubNode = new GeneralListNode(Sub_Type);
begin->_next = SubNode;
begin = begin->_next;
}
}
}
private:
};


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;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：37918次
• 积分：1271
• 等级：
• 排名：千里之外
• 原创：91篇
• 转载：2篇
• 译文：0篇
• 评论：3条
阅读排行
最新评论