2016-01-20 java中的链表设计…
来源:互联网 发布:手机园林设计软件 编辑:程序博客网 时间:2024/05/31 19:13
java中链表的设计
class Node{
private String name; //存储节点名字
private Node next;//保存下一个节点
public Node(String name){
this.name=name;
}
public void setNext(Node next){
this.next=next;
}
public Node getNext(){
return next;
}
public String getName(){
return name;
}
}
public class LinkDemo01 {
public static void main(String args[]){
Node root=new Node("根节点");
Node n1=new Node("第一节车厢");
Node n2=new Node("第二节车厢");
Node n3=new Node("第三节车厢");
root.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
print(root);
}
public static void print(Node node){
if(node!=null){
System.out.print(node.getName()+" --> ");
}
if(node.getNext()!=null){
print(node.getNext());
}else{
System.out.print("null");
}
}
}
运行结果:
根节点 --> 第一节车厢 --> 第二节车厢 --> 第三节车厢 --> null
对于链表的查询,删除,增加节点
package com.mz;
class Link{
class Node{
private String name; //存储节点名字
private Node next;//保存下一个节点
public Node(String name){
this.name=name;
}
public String getName(){
return name;
}
public void addNode(Node newNode){//增加节点
if(this.next==null){//后面没有节点
this.next=newNode;
}else{
this.next.addNode(newNode);//向下继续查
}
}
public void printNode(){
System.out.print(this.name+"-->");
if(this.next!=null){
this.next.printNode();//向下继续列出
}
}
public boolean searchNode(String name){
if(this.name.equals(name)){
return true;
}else{
if(this.next!=null){
return this.next.searchNode(name);
}else{
return false;
}
}
}
public void deleteNode(Node preNode,String name){
if(this.name.equals(name)){
preNode.next=this.next;
}else{
this.next.deleteNode(this,name);
}
}
}
private Node root;
public void add(String name){
Node newNode=new Node(name);
if(this.root==null){//没有根节点
this.root=newNode;
}else{
this.root.addNode(newNode);
}
}
public void delete(String name){
if(this.search(name)){//判断节点是否存在
if(this.root.name.equals(name)){
if(this.root.next!=null){
this.root=this.root.next;//改变根节点
}else{
this.root=null;//取消
}
}else{
if(this.root.next!=null){
this.root.next.deleteNode(root, name);
}
}
}
}
public void print(){
if(this.root!=null){
this.root.printNode();
}
}
public boolean search(String name){//指定查找的名字
if(this.root!=null){
return this.root.searchNode(name);
}else{
return false;
}
}
}
public class LinkDemo02 {
public static void main(String args[]){
Link link=new Link();
link.add("根节点");
link.add("第一节点");
link.add("第二节点");
link.add("第三节点");
link.add("第四节点");
link.add("第五节点");
link.print();
link.delete("第四节点");
System.out.println();
link.print();
System.out.println();
link.delete("根节点");
link.add("第六节点");
link.print();
//System.out.println(link.search("根x节点"));
}
}
以上代码也说明了内部类使用的好处
0 0
- 2016-01-20 java中的链表设计…
- java中的LinkedList 和 …
- Android UI设计中的特效 界面左右…
- java 中的 加密
- Java JFrame 设计视图&n…
- Java中的Drag and Drop详解…
- JAVA设计模式 - 观察者模式
- Java 中的反射机制
- Android Bander设计与实现 - 设计…
- (转)关于JAVA中的Garbage Collecti…
- IBM Filenet java Flex FileRefere…
- java Struts2 jquery uploadify 多…
- hadoop HA----Quorum Journal 设计…
- oracle 中的INTERVAL 函…
- android中的 gravity 和…
- compizconfig 中的方框…
- MATLAB中的eye() 函数 …
- java单例设计模式 饿汉式 懒汉式
- poj 1182食物链
- 2016年1月18日 21:47:46关于this_2…
- 2016年1月19日 static声明属…
- 2016年1月20日 代码块的类别
- UVALive 4255 Guess【拓扑排序 】
- 2016-01-20 java中的链表设计…
- 2016年1月22日 关于继承(重点)
- 2016-01-23
- 2016-01-27 关于抽象类
- 2016-01-29 接口实现的方法
- 2016-02-01
- 2016-02-01_2 Object类
- 2016-02-01_3 关于匿名内部类
- 2016-02-01_4 包装类