日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/3/21 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();

}

}

總結

以上是生活随笔為你收集整理的java 广义表_数据结构:广义表的实现(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。