数据结构是数据在系统中的物理存储结构,自己编写数据结构可以对某种数据结构的物理和逻辑结构有更深的了解。
一、广义表作为一种数据结构有以下的特点:
1. 广义表是递归的;
2. 广义表的元素可以使广义表,也可以是值;
3. 广义表是有序的;
4. 广义表是有长度有深度的。
二、广义表的建立:
广义表节点的存储结构表示为:
图 2-1
utype:
typedef enum{HEAD,VALUE,CHILDLIST}UTYPE;
info:
union
{
//HADE 引用计数,默认0,被引用为1
int ref;
//VALUE 存放值
int value;
;//CHILDLIST 存放子表地址
GenListNode *hlink
}info;
三、广义表完整代码:
GenList.h
#ifndef _GLIST_H
#define _GLIST_H
#include <iostream>
#include <assert.h>
using namespace std;
typedef enum{HEAD,VALUE,CHILDLIST}UTYPE;
class GenList;
class GenListNode
{
friend GenList;
public:
GenListNode();
GenListNode(UTYPE ut, int val);
~GenListNode()
{}
private:
UTYPE utype; //HEAD VALUE CHILDLIST
union
{
int ref; //HADE 引用计数,默认0,被引用为1
int value; //VALUE 存放值
GenListNode *hlink; //CHILDLIST 存放子表地址
}info;
GenListNode *tlink; //尾指针
};
class GenList
{
public:
GenList();
GenList(const GenList &GL);
~GenList();
public:
void CreateGenList(char *str); //创建
void Head(GenListNode &GN)const; //求第一个元素
GenListNode* First()const; //返回表头指针
GenListNode* Tail()const; //返回表尾指针
GenListNode* Next(GenListNode *p)const;//求直接后继
int Length()const; //求广义表长度
int Depth()const; //求广义表深度
private:
void free(GenListNode *p);
GenListNode* Copy(GenListNode *p);
int Length(GenListNode *s)const;
int Depth(GenListNode *s)const;
void CreateList(GenListNode *&ls, char *str);
void server(char *sub, char *hsub); //拆分字串为头和尾
GenListNode *first;
};
#endif
GenList.cpp
#include"GList.h"
GenListNode::GenListNode() :utype(HEAD), tlink(NULL)
{
info.ref = 0;
}
GenListNode::GenListNode(UTYPE ut, int val) :tlink(NULL)
{
utype = ut;
if (utype == HEAD)
{
info.ref = val;
}
else if (utype == VALUE)
{
info.value = val;
}
}
GenList::GenList()
{
first = new GenListNode;
}
void GenList:: CreateGenList(char *str)
{
CreateList(first, str);
}
void GenList::CreateList(GenListNode *&ls, char *str)
{
//对字符串去前后括号
int n = strlen(str);
str[n - 1] = '\0';
strcpy_s(str, n - 1, str + 1);
if ((strlen(str) == 0) || (strcmp(str, "()") == 0))
{
ls->tlink = NULL;
}
char *sub = str;
char *hsub = new char[n - 2 + 1];
GenListNode *p = ls;
if (p == NULL)
{
ls = new GenListNode(HEAD, 1);
p = ls;
}
while (strlen(sub) != 0)
{
p = p->tlink = new GenListNode; //横向
server(sub, hsub); //将字串拆分为表头和表尾
if (hsub[0] == '(') //CHILDLIST
{
p->utype = CHILDLIST;
CreateList(p->info.hlink, hsub); //纵向
}
else
{
p->utype = VALUE;
p->info.value = atoi(hsub);
}
}
}
void GenList::server(char *sub, char *hsub)
{
int n = strlen(sub);
int i = 0;
char ch = sub[0];
int k = 0; //括号匹配计数
while (i < n && (ch != ',' || k != 0))
{
if (ch == '(')k++;
else if (ch == ')')k--;
i++;
ch = sub[i]; //获取下一个字符
}
if (i < n)
{
sub[i] = '\0';
strcpy_s(hsub, i + 1, sub);
strcpy_s(sub, n - i, sub + i + 1);
}
else if (k != 0)
{
return;
}
else
{
strcpy_s(hsub, n + 1, sub);
sub[0] = '\0';
}
}
void GenList::Head(GenListNode &GN)const
{
if (first->tlink != NULL)
{
GN.utype = first->tlink->utype;
GN.info = first->tlink->info;
}
}
GenListNode* GenList::First()const
{
return first->tlink;
}
GenListNode* GenList::Tail()const
{
return first->tlink->tlink;
}
GenListNode* GenList::Next(GenListNode *p)const
{
return p->tlink;
}
int GenList::Length()const
{
return Length(first->tlink);
}
int GenList::Depth()const
{
return Depth(first->tlink);
}
int GenList::Length(GenListNode *s)const
{
if (s == NULL)
return 0;
else
return 1 + Length(s->tlink);
}
int GenList::Depth(GenListNode *p)const
{
int max = 0;
int de;
while ((p != NULL) && (p->utype != CHILDLIST))
{
p = p->tlink;
}
if (p == NULL)
{
return 1;
}
else
{
max = Depth(p->info.hlink) + 1;
de = Depth(p->tlink);
return max > de ? max : max = de;
}
}
GenList::GenList(const GenList &GL)
{
first = Copy(GL.first);
}
GenListNode* GenList::Copy(GenListNode *p)
{
GenListNode *q = NULL;
if (p != NULL)
{
q = new GenListNode;
q->utype = p->utype;
switch (p->utype)
{
case HEAD:
q->info.ref = p->info.ref;
break;
case VALUE:
q->info.value = p->info.value;
break;
case CHILDLIST:
q->info.hlink = Copy(p->info.hlink);
break;
default:
break;
}
q->tlink = Copy(p->tlink);
}
return q;
}
GenList::~GenList()
{
free(first);
}
void GenList::free(GenListNode *p)
{
if (p == NULL)
{
return;
}
switch (p->utype)
{
case HEAD:
free(p->tlink);
break;
case VALUE:
free(p->tlink);
break;
case CHILDLIST:
free(p->tlink);
free(p->info.hlink);
break;
default:
break;
}
delete p;
}
Test.cpp
#include "GList.h"
int main(int argc, char *argv[])
{
char ge[] = "((3,4),(5),(6),((7)))";
GenList gen;
gen.CreateGenList(ge);
int x=gen.Depth();
int y=gen.Length();
GenList gen1(gen);
return 0;
}
图 4-1 调试结果