目录
预先要引用的头文件以及宏定义
#include<stdio.h>
#include<iostream>
//
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define UNSUCCESS 0
#define SUCCESS 1
typedef int ElemType;
typedef int Status;
typedef int KeyType;
typedef char AtomType;
所使用广义表的结构
typedef enum {
ATOM, LIST//枚举,ATOM=0,LIST=1.
}ElemTag;
typedef struct GLNode {
ElemTag tag;
union {
AtomType atom; //当tag == ATOM,本项有意义
struct {
struct GLNode* hp;
struct GLNode* tp;
}ptr; //当tag == LIST,本项有意义
}un;
}GLNode,*GList;
其基本操作接口
GLNode* MakeAtom(AtomType e); //创建一个原子结点
void InitGList(GList& L); //创建一个空的广义表
Status DesTroyGList(GList& L); //销毁一个广义表
GLNode* GetHead(GList L); //求广义表L的表头
GList GetTail(GList L); //求广义表L的表尾
Status InsertHead(GList& L, GLNode* p); //在广义表L的表头插入p元素
Status Append(GList& L, GLNode* p); //在广义表L的末尾添加p元素
Status DeleteHead(GList& L, GLNode* p); //删除一个广义表L的表头,并用p返回
int GListDepth(GList L); //求广义表的深度
int GListLength(GList L); //求广义表的长度
GList CopyGList(GList L); //复制一个广义表
Status GListEmpty(GList L); //判断广义表L是否为空
Status GListTraverse(GList L); //遍历广义表
创建一个原子结点
GLNode* MakeAtom(AtomType e)
{
GLNode* p;
p = (GLNode*)malloc(sizeof(GLNode));
if (p != NULL)
{
p->tag = ATOM;
p->un.atom = e;
//p->un.ptr.hp = NULL;
//p->un.ptr.hp = NULL;
return p;
}
}
创建一个空的广义表
void InitGList(GList& L)
{
L = NULL;
}
销毁一个广义表
Status DesTroyGList(GList& L)
{
if (L != NULL)
{
if (L->tag == LIST)//递归实现,简单粗暴
{
DesTroyGList(L->un.ptr.hp);
DesTroyGList(L->un.ptr.tp);
free(L);
return OK;
}
else
{
free(L);
return OK;
}
}
}
求广义表L的表头
GLNode* GetHead(GList L)
{
if (L->tag == LIST)
{
return L->un.ptr.hp;
}
else
{
return L;
}
}
求广义表L的表尾
GList GetTail(GList L)
{
if (L->tag == LIST)
{
return L->un.ptr.tp;
}
else
{
return NULL;
}
}
在广义表L的表头插入p元素
Status InsertHead(GList& L, GLNode* p)
{
GList L1;
L1 = (GList)malloc(sizeof(GLNode));
if (L1 != NULL)
{
L1->tag = LIST;
L1->un.ptr.hp = p;
L1->un.ptr.tp = L;
L = L1;
return OK;
}
else
{
return OVERFLOW;
}
}
在广义表L的末尾添加p元素
Status Append(GList& L, GLNode* p)
{
GList tail;
tail = (GList)malloc(sizeof(GLNode));
if (tail == NULL)
{
return OVERFLOW;
}
else
{
tail->tag = LIST;
tail->un.ptr.hp = p;
tail->un.ptr.tp = NULL;
if (L == NULL)
{
L = tail;
}
else
{
GLNode* pp;
for (pp = L; pp->un.ptr.tp != NULL; pp = pp->un.ptr.tp);
pp->un.ptr.tp = tail;
}
return OK;
}
}
删除一个广义表L的表头,并用p返回
Status DeleteHead(GList& L, GLNode* p)
{
if (L != NULL)
{
if (L->tag == LIST) //存放的是表结点
{
p = L->un.ptr.hp;
L = L->un.ptr.tp;
return OK;
}
else //存放的是原子结点(没有所谓的表头表尾)
{
p = NULL;
return ERROR;
}
}
else
{
p = NULL;
return OVERFLOW;
}
}
求广义表的深度
int GListDepth(GList L)
{
int h1, h2;
if (L == NULL)
{
return 1;
}
else if (L->tag == ATOM)
{
return 0;
}
else
{
h1 = GListDepth(L->un.ptr.hp) + 1; //表头深度加一
h2 = GListDepth(L->un.ptr.tp); //表尾深度与原表相同
return h1 >= h2 ? h1 : h2;
}
}
求广义表的长度
int GListLength(GList L)
{
int length = 0;
if (L == NULL)
{
return 0;
}
else
{
return 1 + GListLength(L->un.ptr.tp);
}
}
复制一个广义表
GList CopyGList(GList L)
{
if (L == NULL)
{
return NULL;
}
else
{
if (L->tag == ATOM)
{
GLNode* p = MakeAtom(L->un.atom);
return p;
}
else
{
GList L1;
L1 = (GList)malloc(sizeof(GLNode));
if (L1 != NULL)
{
L1->tag = LIST;
L1->un.ptr.hp = CopyGList(L->un.ptr.hp);
L1->un.ptr.tp = CopyGList(L->un.ptr.tp);
return L1;
}
}
}
}
判断广义表L是否为空
Status GListEmpty(GList L)
{
if (L->un.ptr.hp == NULL && L->un.ptr.tp == NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
遍历广义表
Status GListTraverse(GList L)
{
printf(" ");
if (L == NULL)
{
printf("#\n");
return OK;
}
if (L->tag == ATOM)
{
printf("%c\n", L->un.atom);
return OK;
}
else if(L->tag == LIST)
{
GListTraverse(L->un.ptr.hp);
GListTraverse(L->un.ptr.tp);
return OK;
}
}
一些接口的测试
所测试的广义表为
int main()
{
//广义表
GList L;
InitGList(L);//创建一个空表
cout << GListDepth(L);
cout << GListLength(L);
cout << "\n";
GLNode* p;
GList L1;
for (char a = 'a'; a <= 'c'; a++)
{
if (a == 'b')
{
p = MakeAtom('z');
L1 = (GList)malloc(sizeof(GLNode));
if (L1 != NULL)
{
L1->tag = LIST;
L1->un.ptr.hp = p;
L1->un.ptr.tp = NULL;
}
Append(L, L1);
}
p = MakeAtom(a);
Append(L, p);
}
p = MakeAtom('z');
L1 = (GList)malloc(sizeof(GLNode));
if (L1 != NULL)
{
L1->tag = LIST;
L1->un.ptr.hp = p;
L1->un.ptr.tp = NULL;
}
Append(L, L1);
//建立了一个广义表
GListTraverse(L);
cout << GListDepth(L);
cout << GListLength(L);
cout << "\n";
GLNode* g;
g = (GList)malloc(sizeof(GLNode));
DeleteHead(L, g);
GList L2;
L2 = CopyGList(L);
GListTraverse(L2);
cout << GListDepth(L2);
cout << GListLength(L2);
cout << "\n";
}
如若想要删除中间的某个结点,则将遍历改为查找,找到对应结点在删除表头即可。