数据结构之广义表(扩展线性链表)的基本操作
来源:互联网 发布:lamb动作数据百度云 编辑:程序博客网 时间:2024/06/05 08:02
#include<iostream>using namespace std;typedef char AtomType;enum ElemTag{ATOM,LIST};typedef struct GLNode{ElemTag tag;union{AtomType atom;GLNode *pHead;};GLNode *pTail;}GLNode,*GList;void InitGList(GList &L){L = NULL;}void DestroyGList(GList &L){GList ph, pt;if (L){if (L->tag == LIST)ph = L->pHead;elseph = NULL;pt = L->pTail;delete L;L = NULL;DestroyGList(ph);DestroyGList(pt);}}void CopyGList(GList &T, GList L){if (!L)T = NULL;else{T = new GLNode;T->tag = L->tag;if (L->tag == ATOM)T->atom = L->atom;elseCopyGList(T->pHead, L->pHead);if (!(L->pTail))T->pTail = NULL;elseCopyGList(T->pTail, L->pTail);}}int GListLength(GList L){int len = 0;GList p;if (L->tag == LIST&&!(L->pHead))return 0;else if (L->tag == ATOM)return 1;else{p = L->pHead;do{++len;p = p->pTail;} while (p);return len;}}bool GListEmpty(GList L){if (!L || L->tag == LIST&&!(L->pHead))return true;elsereturn false;}int GListDepth(GList L){int max, dep;GList p;if (GListEmpty(L))return 1;else if (L->tag == ATOM)//递归之前是不会出现的return 0;else{for (max = 0, p = L->pHead; p; p = p->pTail){dep = GListDepth(p);if (dep > max)max = dep;}return max + 1;}}GList GetHead(GList L){GList h;InitGList(h);if (GListEmpty(L))return NULL;else{h = new GLNode;h->tag = L->pHead->tag;h->pTail = NULL;if (h->tag == ATOM)h->atom = L->pHead->atom;elseCopyGList(h->pHead, L->pHead->pHead);return h;}}GList GetTail(GList L){GList t;if (!L)return NULL;else{t = new GLNode;t->tag = LIST;t->pTail = NULL;CopyGList(t->pHead, L->pHead->pTail);return t;}}void InsertFirst(GList &L, GList pe){GList p = L->pHead;L->pHead = pe;pe->pTail = p;}void DeleteFirst(GList &L, GList &pe){if (L){pe = L->pHead;L->pHead = pe->pTail;pe->pTail = NULL;}elsepe = L;}void VisitAtom(AtomType a){cout << a << " ";}void GListPrint(GList L){GList ph;if (L){if (L->tag == ATOM){VisitAtom(L->atom);ph = NULL;}else{ph = L->pHead;GListPrint(ph);GListPrint(L->pTail);}}}
0 0
- 数据结构之广义表(扩展线性链表)的基本操作
- 数据结构之广义表(头尾链表存储)基本操作
- 看数据结构写代码(52) 广义表的扩展线性链表存储表示
- _DataStructure_C_Impl:广义表的扩展线性链表存储
- 数据结构之线性表中顺序表的基本操作
- 数据结构之线性表(顺序表)的基本操作
- C语言数据结构之线性表的基本操作
- 广义表的基本操作
- 广义表的基本操作
- 广义表的基本操作
- 线性表的基本操作-数据结构
- 【数据结构】线性表基本操作
- 数据结构之广义表
- C++数据结构之线性顺序表基本操作
- 数据结构之线性表的操作
- 数据结构之线性表的操作
- 广义表的建立与基本操作
- 广义表的基本操作函数
- ajaxfileupload文件上传返回值处理 ajaxfileupload.js + spring mvc文件上传
- hdu 4915 括号匹配+巧模拟
- hdu2203 KMP水题
- Android JNI编程学习笔记(二)——深入了解NDK
- Android中怎么使图片显示
- 数据结构之广义表(扩展线性链表)的基本操作
- Linux命令--ps
- MATLAB 基础
- js的语法
- 反转链表
- 【cocos2d-x游戏开发】坐标系详解
- VS调试技巧
- 测试JSON RPC远程调用(JSON客户端)
- android ndk 环境搭建 心得