数据结构:广义表的实现(Java)
广义表的简单理解在这篇博文中: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)的更多相关文章
- 【C/C++】实现数据结构广义表
1. 广义表的定义 每个元素可以为Atom,原子,也可以为线性表. 线性表的推广.线性表元素有唯一的前驱和后继,为线性表,而广义表是多层次的线性表 表头:第一个元素,可能是 ...
- 数据结构(C语言第2版)-----数组,广义表,树,图
任何一个算法的设计取决于选定的数据结构,而算法的实现依赖于采用的存储结构. 之前线性表的数据元素都是非结构的原子类型,元素的值是不可再分的.下面学习的这两个线性表是很特殊的,其中数据元素本身也可能是一 ...
- 数据结构算法C语言实现(十九)--- 5.5&5.6&5.7广义表
一.简述 传说Lisp的基本数据结构就是广义表,广义表也是具有典型递归属性的数据结构,此外,由于建表要处理字符串,用C语言处理起来也是一脸懵逼.....最后自己还想写一个将广义表还原成字符串的函数,一 ...
- javascript实现数据结构:广义表
原文:javascript实现数据结构:广义表 广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...
- 数据结构(C语言版)-第4章 串、数组和广义表
补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...
- 数据结构28:广义表及M元多项式
广义表,又称为列表.记作: LS = (a1,a2,…,an) ;( LS 为广义表的名称, an 表示广义表中的数据). 广义表可以看作是线性表的推广.两者区别是:线性表中的数据元素只能表示单个数据 ...
- 数据结构之---C语言实现广义表头尾链表存储表示
//广义表的头尾链表存储表示 //杨鑫 #include <stdio.h> #include <malloc.h> #include <stdlib.h> #in ...
- 数据结构 c++ 广义表
// CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- 广义表操作 (ava实现)——广义表深度、广义表长度、打印广义表信息
广义表是对线性表的扩展——线性表存储的所有的数据都是原子的(一个数或者不可分割的结构),且所有的数据类型相同.而广义表是允许线性表容纳自身结构的数据结构. 广义表定义: 广义表是由n个元素组成的序列: ...
随机推荐
- 7-n!的位数(斯特灵公式)
http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big NumberTime Limit: 2000/1000 MS (Java/Others) Memo ...
- Java Thread系列(二)线程状态
Java Thread系列(二)线程状态 一.线程的五种状态 新建状态(New):新创建了一个线程对象,尚未启动. 就绪状态(Runnable):也叫可运行状态.线程对象创建后,其他线程调用了该对象的 ...
- Go基本数据类型
Go基本数据类型 第一部分:基本数据类型和操作符 1. 文件名&关键字&标识符 1. 所有go源码以.go结尾 2. 标识符以字母或下划线开头,大小写敏感,比如: a. boy b. ...
- linux网络编程模型
1.编程模型 Linux网络编程模型是基于socket的编程模型
- Oracle 错误集锦
1.plsql进行更新操作时卡死的解决办法 https://blog.csdn.net/laoyingat/article/details/79379270
- 利用NotePad++ 格式化代码(格式标准化) worldsing
在阅读别人的代码时往往会遇到格式很乱,阅读起来很费劲,如果手动改很容易出错,而且很费时间,这时可以借助一些专业的编辑器来格式化代码,NotePad++是一个轻量级的代码编辑器,占用内存少,运行速度快, ...
- Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks(使用循环一致的敌对网络进行不成对的图像到图像转换)
作者:朱俊彦,朱俊彦博士是计算机图形学领域现代机器学习应用的开拓者.他的论文可以说是第一篇用深度神经网络系统地解决自然图像合成问题的论文.因此,他的研究对这个领域产生了重大影响.他的一些科研成果,尤其 ...
- Windows 以及 Xcode下编译调试 libcurl 源码
curl 这个工具大家都很熟悉. 前几天因为要跟踪curl的实现细节, 不得不设法搭建curl的调试工程. 我们分别在windows visual studio 和 mac 上的 xcode 下搭建调 ...
- Redis配置参数汇总
==配置文件全解=== ==基本配置daemonize no 是否以后台进程启动databases 16 创建database的数量(默认选中的是database 0) save 900 1 #刷新快 ...
- eayui js动态加载Datagrid,自适应宽度,高度
HTML: <div class="easyui-layout" style="min-height:100%;min-width:100%;"> ...