广义表的简单理解在这篇博文中:https://blog.csdn.net/lishanleilixin/article/details/87364496,在此不做赘述。

Java实现广义表:

package 广义表;

import java.util.Stack;

public class Test {

	public final int TAG_TABLE = 1;
public final int TAG_ITEM = 0; private char mStartSymb = '(';
private char mEndSymb = ')';
private Node mGenTable; class Node {
int tag;
Object data;
Node mPh;
Node mPt; public Node(Node ph, Node pt, int tag, Object data) {
this.mPh = ph;
this.mPt = pt;
this.tag = tag;
this.data = data;
}
} public Test(String genTable) {
if(genTable == null)
throw new NullPointerException("genTable is null");
initTable(genTable);
} public Test() {
mGenTable = new Node(null, null, TAG_TABLE, null); } public Test(Test b) {
if(b != null) {
mGenTable = b.mGenTable;
}
} public void initTable(String genTable) {
String ts = genTable.replaceAll("\\s", "");
int len = ts.length();
Stack<Character> symStack = new Stack<Character>();
Stack<Node> nodeStack = new Stack<Node>();
initSymbolicCharactor(ts);
mGenTable = new Node(null, null, TAG_TABLE, null);
Node itemNode, tableNode = mGenTable, tmpNode;
for (int i = 0; i < len; i++) {
if(ts.charAt(i) == mStartSymb) {
tmpNode = new Node(null, null ,TAG_TABLE, null);
symStack.push(ts.charAt(i));
if(symStack.size() > 1) {
nodeStack.push(tableNode);
tableNode.mPh = tmpNode;
tableNode = tableNode.mPh;
}
else {
tableNode.mPt = tmpNode;
tableNode = tableNode.mPt;
}
}
else if(ts.charAt(i) == mEndSymb) {
if(symStack.isEmpty()) {
throw new NullPointerException(
"IllegalArgumentException in constructor GeneralizedTable!...");
}
if(symStack.size() > 1) {
tableNode = nodeStack.pop();
}
symStack.pop();
}
else if(ts.charAt(i) == ',') {
tableNode.mPt = new Node(null, null, TAG_TABLE, null);
tableNode = tableNode.mPt;
}
else {
itemNode = new Node(null, null, TAG_ITEM, ts.charAt(i));
tableNode.mPh = itemNode;
}
} if(!symStack.isEmpty()) {
throw new NullPointerException(
"IllegalArgumentException in constructor GeneralizedTable!...");
} } public void initSymbolicCharactor(String ts) {
mStartSymb = ts.charAt(0);
switch (mStartSymb) {
case '(':
mEndSymb = ')';
break;
case '{':
mEndSymb = '}';
break;
case '[':
mEndSymb = ']';
break;
default:
throw new IllegalArgumentException(
"IllegalArgumentException ---> initSymbolicCharactor");
} } public void print() {
print(mGenTable);
} private void print(Node node) {
if(node == null) return;
if(node.tag == 0) System.out.print(node.data.toString() + "\t");
print(node.mPh);
print(node.mPt);
} public int depth() {
if(mGenTable == null)
throw new NullPointerException("Generalized Table is null !.. ---> method depth");
return depth(mGenTable);
} private int depth(Node node) {
if(node == null || node.tag == 0) return 0;
int depHeader = 0, depTear = 0;
depHeader = 1 + depth(node.mPh);
depTear = depth(node.mPt);
return depHeader > depTear ? depHeader : depTear;
} public int length() {
if(mGenTable == null || mGenTable.mPt == null) return -1;
int len = 0;
Node node = mGenTable;
while(node.mPt != null) {
node = node.mPt;
if(node.mPh == null && node.mPt == null) break;
len++;
}
return len;
} public Test getHeader() {
if(isEmpty()) return null;
Node node = mGenTable.mPt;
Test test = new Test();
test.mGenTable.mPt = node.mPh;
return test;
} public Test getTear() {
if(mGenTable == null) return null;
Node node = mGenTable.mPt;
Test test = new Test();
test.mGenTable.mPt = node.mPt;
return test; } public boolean isEmpty() {
if(mGenTable == null) return true;
Node node = mGenTable.mPt;
return node.mPh == null || node.mPt == null;
} public static void main(String[] args) {
Test test = new Test("(c,a,b,(a,b,c),(a,(a,b),c))");
test.print();
System.out.println();
System.out.println("该广义表的深度为:" + test.depth());
System.out.println("该广义表的长度为:" + test.length()); Test t2 = test.getTear();
t2.print();
} }

数据结构:广义表的实现(Java)的更多相关文章

  1. 【C/C++】实现数据结构广义表

    1. 广义表的定义     每个元素可以为Atom,原子,也可以为线性表.      线性表的推广.线性表元素有唯一的前驱和后继,为线性表,而广义表是多层次的线性表      表头:第一个元素,可能是 ...

  2. 数据结构(C语言第2版)-----数组,广义表,树,图

    任何一个算法的设计取决于选定的数据结构,而算法的实现依赖于采用的存储结构. 之前线性表的数据元素都是非结构的原子类型,元素的值是不可再分的.下面学习的这两个线性表是很特殊的,其中数据元素本身也可能是一 ...

  3. 数据结构算法C语言实现(十九)--- 5.5&5.6&5.7广义表

    一.简述 传说Lisp的基本数据结构就是广义表,广义表也是具有典型递归属性的数据结构,此外,由于建表要处理字符串,用C语言处理起来也是一脸懵逼.....最后自己还想写一个将广义表还原成字符串的函数,一 ...

  4. javascript实现数据结构:广义表

    原文:javascript实现数据结构:广义表  广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...

  5. 数据结构(C语言版)-第4章 串、数组和广义表

    补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...

  6. 数据结构28:广义表及M元多项式

    广义表,又称为列表.记作: LS = (a1,a2,…,an) ;( LS 为广义表的名称, an 表示广义表中的数据). 广义表可以看作是线性表的推广.两者区别是:线性表中的数据元素只能表示单个数据 ...

  7. 数据结构之---C语言实现广义表头尾链表存储表示

    //广义表的头尾链表存储表示 //杨鑫 #include <stdio.h> #include <malloc.h> #include <stdlib.h> #in ...

  8. 数据结构 c++ 广义表

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  9. 广义表操作 (ava实现)——广义表深度、广义表长度、打印广义表信息

    广义表是对线性表的扩展——线性表存储的所有的数据都是原子的(一个数或者不可分割的结构),且所有的数据类型相同.而广义表是允许线性表容纳自身结构的数据结构. 广义表定义: 广义表是由n个元素组成的序列: ...

随机推荐

  1. ThinkPHP出现General error: 2006 MySQL server has gone away的解决方法

    错误: #13 {main}SQLSTATE[HY000]: General error: 2006 MySQL server has gone awayFILE: \ThinkPHP\Library ...

  2. ultraiso制作ubuntu u盘启动

    http://blog.csdn.net/yaoyut/article/details/78003061

  3. linq join一些忘记的操作

  4. ScrollView中嵌套ListView的问题

    网上关于怎样在ScrollView中嵌套ListView的讨论有很多,我大概是搜索了一下,简单总结如下: 1.不要在ScrollView中嵌套ListView a.用一个LinearLayout来代替 ...

  5. Linux执行YUM命令报错解决方案

    Loaded plugins: rhnplugin, security This system is not registered with RHN. RHN support will be disa ...

  6. CI框架下的PHP增删改查总结

    controllers下的 cquery.php文件 <?php class CQuery extends Controller { //构造函数 function CQuery() { par ...

  7. 通过HttpClient请求webService

    通过HttpClient请求webService 由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求, ...

  8. 基于jCOM搭建Java-微软信息桥梁(下)

    第一部分析了BEA提供的Java/COM互操作解决方案—jCOM的实现原理:本文是第二部分,比较全面地分析了Weblogic Server的jCOM实现技术之后,通过一个具体实例来说明了jCOM的具体 ...

  9. 测试嵌入GeoGebra网页

    使用 http://ggbstudy.top/tools/ggb2html/ 将GGB文件免费托管,然后在博客内容中点击“HTML”按钮插入GGB网页地址: <iframe src=" ...

  10. Oracle EBS客户化程序中格式化金额

    在Oracle EBS系统中,随处可见金额的显示格式,通常情况下都具有千分位符,同时有一定位数的精度,让我们先来看看一些现成的例子    上面这些列子中的金额都显示了千分位符,同时具备以2位小数,难道 ...