第9周项目4广义表算法库及应用2
来源:互联网 发布:face nginx 编辑:程序博客网 时间:2024/05/21 05:44
算法及代码:
#ifndef GLIST_H_INCLUDED#define GLIST_H_INCLUDEDtypedef char ElemType;typedef struct lnode{ int tag; //节点类型标识 union { ElemType data; //原子值 struct lnode *sublist; //指向子表的指针 } val; struct lnode *link; //指向下一个元素} GLNode; //广义表节点类型定义int GLLength(GLNode *g); //求广义表g的长度int GLDepth(GLNode *g); //求广义表g的深度GLNode *CreateGL(char *&s); //返回由括号表示法表示s的广义表链式存储结构void DispGL(GLNode *g); //输出广义表g#endif // GLIST_H_INCLUDED#include <stdio.h>#include "glist.h"int atomnum(GLNode *g) //求广义表g中的原子个数{ if (g!=NULL) { if (g->tag==0) return 1+atomnum(g->link); else return atomnum(g->val.sublist)+atomnum(g->link); } else return 0;}ElemType maxatom(GLNode *g) //求广义表g中最大原子{ ElemType max1,max2; if (g!=NULL) { if (g->tag==0) { max1=maxatom(g->link); return(g->val.data>max1?g->val.data:max1); } else { max1=maxatom(g->val.sublist); max2=maxatom(g->link); return(max1>max2?max1:max2); } } else return 0;}int main(){ GLNode *g; char *s="(b,(b,a,(#),d),((a,b),c((#))))"; g = CreateGL(s); DispGL(g); printf("\n"); printf("原子个数 :%d\n", atomnum(g)); printf("最大原子 :%c\n", maxatom(g)); return 0;}
运行结果:
0 0
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4广义表算法库及应用2
- 第9周SHH数据结构-【项目4-广义表算法库及应用(2)】
- 第9周项目4-广义表算法库及应用(2)
- 【第9周项目4-广义表算法库及应用(2)】
- 第9周项目4 -广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4 广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周项目4 广义表算法库及应用(2)
- 第9周项目4-广义表算法库及应用(2)
- 第9周 项目4—广义表算法库及应用
- 第9周项目4-广义表算法库及应用
- 第9周项目4-广义表算法库及应用(1)
- 解决adb not responding if youd like to retry...错误
- Java之继承
- Linux自虐之路(一):装机后的那些事
- 第十三周项目3 Dijkstra算法
- 找论文资料方式
- 第9周项目4广义表算法库及应用2
- 24 Given the following steps, which would be the correct order to create a backup of an Oracle datab
- boost中unordered_map的用法
- 第十三周项目四:Floyd算法的验证
- 第十三周--数据结构-Kruskal算法的验证
- LeetCode 224 Basic Calculator
- 打开蓝牙设备
- 动态内存分配
- ORACLE 事务属性详解