java 广义表_数据结构:广义表的实现(Java)

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 symStack = new Stack();

Stack nodeStack = new Stack();

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值