
正文
广义表类的代码java java 广义表
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
声明以 双链表示的广义表类,实现广义表的遍历,插入、删除等操作 。用JAVA语言,急用。。。。
package core;
/*
* 广义表类的代码java我们可以用类ListLinked来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。
* 存储当前结点广义表类的代码java的指针时有一定广义表类的代码java的技巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,
* 当其值为null时表示当前结点是第一个结点。那么为什么要这样做呢?
* 这是因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。
* 那么如何得到当前结点呢,我们定义广义表类的代码java了一个方法cursor(),返回值是指向当前结点的指针。
* 类ListLinked还定义广义表类的代码java了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种操作。
* 例如reset()方法使第一个结点成为当前结点。
* insert(Object d)方法在当前结点前插入一个结点,并使其成为当前结点。
* remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,
* 如果删除的是最后一个结点,则第一个结点变为当前结点。
*/
public class ListLinked {
/* 用变量来实现表头 */
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;
public void deleteAll()
/* 清空整个链表 */
{
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
public void reset()
/*
* 链表复位,使第一个结点 成为当前结点
*/
{
Pointer = null;
}
public boolean isEmpty()
/* 判断链表是否为空 */
{
return (Length == 0);
}
public boolean isEnd()
/*
* 判断当前结点是否 为最后一个结点
*/
{
if (Length == 0)
throw new java.lang.NullPointerException();
else if (Length == 1)
return true;
else
return (cursor() == Tail);
}
public Object nextNode()
/*
* 返回当前结点的下一个结点的值, 并使其成为当前结点
*/
{
if (Length == 1)
throw new java.util.NoSuchElementException();
else if (Length == 0)
throw new java.lang.NullPointerException();
else {
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data);
else
throw new java.util.NoSuchElementException();
}
}
public Object currentNode()
/* 返回当前结点的值 */
{
Node temp = cursor();
return temp.data;
}
public void insert(Object d)
/*
* 在当前结点前插入一个结点, 并使其成为当前结点
*/
{
Node e = new Node(d);
if (Length == 0) {
Tail = e;
Head = e;
} else {
Node temp = cursor();
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}
public int size()
/* 返回链表的大小 */
{
return (Length);
}
public Object remove()
/* 将当前结点移出链表,下一个结点成为当前结点,如果移出的结点是最后一个结点,则第一个结点成为当前结点 */
{
Object temp;
if (Length == 0)
throw new java.util.NoSuchElementException();
else if (Length == 1) {
temp = Head.data;
deleteAll();
} else {
Node cur = cursor();
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail) {
Pointer.next = null;
Tail = Pointer;
reset();
} else
Pointer.next = cur.next;
Length--;
}
return temp;
}
private Node cursor()
/* 返回当前结点的指针 */
{
if (Head == null)
throw new java.lang.NullPointerException();
else if (Pointer == null)
return Head;
else
return Pointer.next;
}
public static void main(String[] args)
/* 链表的简单应用举例 */
{
ListLinked a = new ListLinked();
for (int i = 1; i = 10; i++)
a.insert(new Integer(i));
System.out.println(a.currentNode());
while (!a.isEnd())
System.out.println(a.nextNode());
a.reset();
while (!a.isEnd()) {
a.remove();
}
a.remove();
a.reset();
if (a.isEmpty())
System.out.println("There is no Node in List \n");
}
}
class Node {
/* 构成链表的结点定义 */
Object data;
Node next;
Node(Object d) {
data = d;
next = null;
}
}
相关问答
Q1: java中怎样用hashmap(或treemap)实现广义表
import java.util.*;
import static java.lang.System.out;
public class UseHashMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
test1();
test2();
test3();
}
static void test1(){
HashMap h1=new HashMap();
HashMap h2=new HashMap();
HashMap h3=new HashMap();
h2.put("A","B");
h1.put("", h2);
h3.put("C","D");
h1.put("A",h3);
out.println(h1.values());
}
static void test2(){
HashMap h1=new HashMap();
HashMap h2=new HashMap();
HashMap h3=new HashMap();
h2.put("", "B");
h1.put("",h2.values());
h3.put("C","D");
h1.put("A", h3);
out.println(h1.values());
}
static void test3(){
HashMap h1=new HashMap();
HashMap h2=new HashMap();
HashMap h3=new HashMap();
h2.put("B","C");
h3.put("A",h2);
h1.put("", h3);
out.println(h1.values());
}
}
哈哈。。。
Q2: 求Java中比较两个广义表是否相等的代码模版
试编写判别两个广义表是否相等广义表类的代码java的递归算法。
广义表类型GList的定义广义表类的代码java:
[cpp]
view
plain
copy
typedef
enum
{ATOM,LIST}
ElemTag;
typedef
struct
GLNode{
ElemTag
tag;
union
{
char
atom;
struct
{
GLNode
*hp,
*tp;
}
ptr;
}un;
}
*GList;
实现的函数如下广义表类的代码java:
[cpp]
view
plain
copy
Status
Equal(GList
A,
GList
B)
/*
判断广义表A和B是否相等,是则返回TRUE,否则返回FALSE
*/
{
if(A
-
tag
==
ATOM
B
-
tag
==
ATOM){
//当都为原子节点ATOM时
if(A
-
un.atom
==
B
-
un.atom)
return
TRUE;
else
return
FALSE;
}else
if(A
-
tag
==
LIST
B
-
tag
==
LIST){
//当都为广义表节点LIST时
if(Equal(A
-
un.ptr.hp,B
-
un.ptr.hp)
Equal(A
-
un.ptr.tp,B
-
un.ptr.tp))
//递归判断表头节点是否相等广义表类的代码java,表尾节点是否相等
return
TRUE;
else
return
FALSE;
}
}
Q3: java中的单向链表,栈,串,有哪些对象使用的这些数据结构,还有树,图,广义表这些在JAVA有哪些对象
JAVA把数据结构简化了,提供了不少集合类(collection),用的最多的就是LIST和MAP这个两个接口。LIST和MAP各自对应了多个实现它们的类,比如ArrayList,HashMap等等。其中List就很像C里的链表,它有顺序存放和无序存放的对象。好像没有几个类能严格符合你说的几种数据结构,你可以自己写类来实现相同的功能。没有这么多复杂的数据结构,JAVA才体现出简单易学的特点啊。
Q4: 求代码:在空白广义表中插入子链A,复制子链A并作为子链B插入广义表(java
广义表第一个元素为表头,其余元素组成的表为表尾,如果只有一个元素,则表尾为空即()。A=((a,b,c),(d,e,f))tail(A)=((d,e,f))tail(tail(A))=()head(tail(tail(A)))=()
广义表类的代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 广义表、广义表类的代码java的信息别忘了在本站进行查找喔。

![【电子书】[人文传记] 《简而美的哲学小史》[李妍][epub+mobi+azw3] 【电子书】[人文传记] 《简而美的哲学小史》[李妍][epub+mobi+azw3]](https://www.04ip.com/template/qe/style/noimg/2.jpg)

![【电子书】[历史军事] 《中国哲学小史》[冯友兰][epub+mobi+azw3] 【电子书】[历史军事] 《中国哲学小史》[冯友兰][epub+mobi+azw3]](https://www.04ip.com/template/qe/style/noimg/7.jpg)



