广义表及其实现

时间:2020-11-29 21:19:51

一,定义

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

它是递归的,因为在表的描述中有得到表,允许表中有表。

二,实例

<1> A = ()
<2> B = (a,b)
<3> C = (a,b,(c,d))
<4> D = (a,b,(c,d),(e,(f),h))

<5> E = (((),()))

三,图解

广义表及其实现广义表及其实现


四,代码实现

#pragma once;
#include<iostream>
using namespace std;
#include<assert.h>


enum Type
{
HEAD,
VALUE,
SUB,
};


struct GeneralizedNode
{
Type _type;                        //结点类型
GeneralizedNode* _next;            //指向同一层的下一个节点
union                              //共用体
{
char _value;                   //若为值类型结点,则存储值
GeneralizedNode* _sublink;     //指向子表的指针
};


GeneralizedNode(Type type = HEAD, char value = 0)
:_type(type)
, _next(NULL)
{
if (_type == VALUE)
{
_value = value;
}
else if (_type == SUB)
{
_sublink = NULL;
}
}
};


class Generalized
{
public:
Generalized()                            //默认构造
:_head(NULL)
{}


Generalized(const char* str)
:_head(NULL)
{
_head = _CreateLized(str);           //创建
}


Generalized(const Generalized& g)
{
_head = _Copy(g._head);
}


Generalized& operator=(const Generalized& g)
{
if (_head != g._head)
{
GeneralizedNode* cur = _head;
_Destory(_head);
_head = _Copy(g._head);
return *this;
}
}


~Generalized()
{
_Destory(_head);
}
public:
void Print()
{
_Print(_head);
cout << endl;
}
size_t Size()
{
size_t count = _Size(_head);
cout << count << endl;
return count;
}


size_t Depth()
{
size_t dep = _Depth(_head);
cout << dep << endl;
return dep;
}


public:


//创建广义表表
GeneralizedNode* _CreateLized(const char*& str) //防止递归时不能传值
{                                              
assert(str&&*str == '(');     //str不为‘(’,则传参错误
str++;


GeneralizedNode* head = new GeneralizedNode(HEAD);
GeneralizedNode* cur = head;
while (*str != '\0')
{
if (_Isvalue(*str))
{
GeneralizedNode* tmp = new GeneralizedNode(VALUE, *str);
cur->_next = tmp;
cur = cur->_next;
str++;
continue; //继续判断
}
else if (*str == '(')   //遇到子表
{
GeneralizedNode* sub = new GeneralizedNode(SUB);
cur->_next = sub;
cur = cur->_next;
sub->_sublink = _CreateLized(str);  //进入递归创建子表
continue;
}
else if (*str == ')')  //一个表的结束
{
str++;
return head;
}
else
{
str++;
continue;
}
assert(false);  //强制判断程序是否出错
return head;
}
}


//判断表中的数据是否为有效数值
bool _Isvalue(const char c)
{
if ((c <= '9'&&c >= '0') || (c <= 'z'&&c >= 'a') || (c <= 'Z'&&c >= 'A'))
{
return true;
}
else
{
return false;
}
}


//拷贝一个广义表
GeneralizedNode* _Copy(GeneralizedNode* head)
{
GeneralizedNode* Head = new GeneralizedNode(HEAD);


GeneralizedNode* cur = head->_next;
GeneralizedNode* tmp = Head;
while (cur)
{
if (cur->_type == VALUE)
{
tmp->_next = new GeneralizedNode(VALUE, cur->_value);
cur = cur->_next;
tmp = tmp->_next;
}
else if (cur->_type == SUB)
{
tmp->_next = new GeneralizedNode(SUB);

tmp = tmp->_next;
tmp->_sublink = _Copy(cur->_sublink);  //进行拷贝表的递归
cur = cur->_next;
}
}
return Head;
}


//输出广义表
void _Print(GeneralizedNode* head)
{
GeneralizedNode* cur = head;
while (cur)
{
if (cur->_type == HEAD)
{
cout << "(" << " ";
cur = cur->_next;
continue;
}
else if ((cur->_type == VALUE) && (cur->_next != NULL))
{
cout << cur->_value << " " << ",";
cur = cur->_next;
continue;
}
else if ((cur->_type == VALUE) && (cur->_next == NULL))//最后一个结点
{
cout << cur->_value << " ";
cur = cur->_next;
continue;
}
else if (cur->_type == SUB)
{
_Print(cur->_sublink);                //惊醒输出广义表的递归
cur = cur->_next;
if (cur != NULL)      //属于子表
{
cout << ",";
}
continue;
}
}
if (cur == NULL)
{
cout << ")";
return;
}
}


//销毁广义表
void _Destory(GeneralizedNode* head)
{
GeneralizedNode* cur = head;
while (cur)
{
if (cur->_type == SUB)
{
_Destory(cur->_sublink); 
}
GeneralizedNode* del = cur;
cur = cur->_next;
delete del;
}
return;
}


//求广义表的大小
size_t _Size(GeneralizedNode* head)
{
size_t count = 0;
GeneralizedNode* cur = head;
while (cur)
{
if (cur->_type == VALUE)
{
count++;
cur = cur->_next;
continue;
}
if (cur->_type == SUB)
{
count += _Size(cur->_sublink); //进入递归
cur = cur->_next;
continue;
}
cur = cur->_next;
}
return count;
}


//求表的深度
size_t _Depth(GeneralizedNode* head)
{
assert(head);
size_t dep = 1;
size_t Dep = 0;
GeneralizedNode* cur = head;
while (cur)
{
if (cur->_type == SUB)
{
dep += _LenthSUB(cur->_sublink);
}
cur = cur->_next;
if (Dep < dep)  
{
Dep = dep;
dep = 1;
}
}


return Dep;
}
//求子表长度
size_t _LenthSUB(GeneralizedNode* sub)
{
GeneralizedNode* cur = sub;
size_t dep = 1;
while (cur)
{
if (cur->_type == SUB)
{
dep = dep + _LenthSUB(cur->_sublink);
}
cur = cur->_next;
}
return dep;
}


protected:
GeneralizedNode* _head;
};


#define _CRT_SECURE_NO_WARNINGS 1
#include"generalized.h"
#include<stdio.h>
int main()
{
char a[20] = {'(','a',',', 'b',',', 'c',',', '(',',', 'd',', ','e',',', ')',')'};
Generalized A(a);
A.Print();
A.Size();
A.Depth();
system("pause");
return 0;
}

五,结果

广义表及其实现广义表及其实现广义表及其实现广义表及其实现