package glist;
import java.util.List;
import java.util.Scanner;
public class GList {
static Scanner sc = new Scanner(System.in);
public static ListNote createGList() {
Note head = new Note();
Note t = head;
while (true) {
int flag = sc.nextInt();
if (flag == 0) { //原子节点
int data = sc.nextInt();
IntNote p = new IntNote();
p.flag = 0;
p.data = data;
p.pre = t;
t.next = p;
t = p;
} else if (flag == 1) {
ListNote p = createGList();
p.pre = t;
t.next = p;
t = p;
} else {
t.next = null;
break;
}
}
head.next.pre = null;
ListNote gList = new ListNote();
gList.flag = 1;
gList.gList = head.next;
return gList;
}
public static void show(ListNote gList) {
System.out.print("(");
Note p = gList.gList;
while (p != null) {
if (p.flag == 0) {
IntNote t = (IntNote)p;
System.out.print(t.data);
} else {
ListNote t = (ListNote)p;
show(t);
}
if (p.next != null) {
System.out.print(",");
}
p = p.next;
}
System.out.print(")");
}
public static int depth(Note gList) {
if (gList == null) return 1;
if (gList.flag == 0) return 0;
int max = 0;
Note p = ((ListNote)gList).gList;
while (p != null) {
int tem = depth(p);
if (tem > max) {
max = tem;
}
p = p.next;
}
return max + 1;
}
public static void main(String[] args) {
ListNote gList = createGList();
show(gList);
System.out.print(depth(gList));
}
}
class Note {
Integer flag = null;
Note pre = null, next = null;
}
class IntNote extends Note{
Integer data;
}
class ListNote extends Note {
Note gList = null;
}
[Java]广义表
猜你喜欢
转载自blog.csdn.net/qq_40561126/article/details/104642581
今日推荐
周排行