您的位置:首页 > 理论基础 > 数据结构算法

Java 数据结构与算法之链表实现

2017-04-15 17:17 351 查看

单链表

/*
* 功能:创建单项链表,提供对链表的增删改查操作
*/
package javalist;

/*
简介:定义节点类。
*/
class Node{
//数据域
public int a;
//指针域
public Node nextNode = null;

public Node(int d){

this.a =  d;
}

public int getDate(){
return a;
}

public void setDate(int newDate){
this.a = newDate;
}
public  void setNextNode(Node n){
this.nextNode = n;
}

}

/*
功能:创建链表,提供对链表的增删该查操作
*/
class LinkList {

//初始化头结点
public static  Node head = new Node(0);

public Node getHeadNode(){
return head;
}

/*
增:尾插法
@param node
*/
public void insetNode(Node n){
Node p = head.nextNode;
head.setNextNode(n);
n.nextNode = p;
}

/*
删除:删除最后一个
*/
public void deleteNode(){
Node p = head.nextNode;
System.out.println("删除Node");
while(p.nextNode.nextNode!=null){
p = p.nextNode;
}//while语句结束,接下来开开始进行删除操作
p.nextNode = null;
System.out.println("删除成功!");
}

/*
改:把2改成6
return L
*/
public void changNode(LinkList l,int date, int  newDate){
boolean flag = findNode( l, date);
if(flag){
Node p = l.getHeadNode();
while(p!=null){
if(p.getDate() == date){

p.setDate(newDate);
System.out.println("找到date:"+date+"   "+"改为:"+newDate);
}//if end
p = p.nextNode;
}//while  end
}//if true 执行
}
/*
查找; 根据值进行查找
return boolean
*/
public boolean findNode(LinkList l,int date){
Node p = l.getHeadNode();
while(p!=null){
if(p.getDate() == date){
System.out.println("找到date:"+date);
return true;
}//if end
p = p.nextNode;
}//while  end
System.out.println("No find");
return false;
}

}

/**
* 功能;链表测试类
* @author Administrator
*/
public class JavaList {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//创建单项链表
LinkList l = new LinkList();

//加入节点
System.out.println("尾插法开始");
Node n1 = new Node(1);
l.insetNode(n1);

Node n2 = new Node(2);
l.insetNode(n2);

Node n3 = new Node(3);
l.insetNode(n3);

//打印链表
printList(LinkList.head);

//删除最后一个节点
l.deleteNode();

printList(LinkList.head);

//查找
l.findNode(l, 2);

//把2换成666
l.changNode(l, 2, 666);

printList(LinkList.head);

}

/*
打印
*/
public static  void printList(Node h){
System.out.println("打印List");
Node p = h;
while(p.nextNode!=null){
System.out.println(p.getDate());
p = p.nextNode;
}
System.out.println(p.getDate());
System.out.println("打印完毕");
}
}


实现截图

run:
尾插法开始
打印List
0
3
2
1
打印完毕
删除Node
删除成功!
打印List
0
3
2
打印完毕
找到date:2
找到date:2
找到date:2   改为:666
打印List
0
3
666
打印完毕
成功构建 (总时间: 0 秒)


树的广度和深度优先算法(未完成)

/*
* 树的深度优先和广度优先算法实现
*/
package LearnTree;

/*
首先得定义节点类。
*/
class Node{
//数据域
public int a;
//指针域
public Node nextLNode = null;
public Node nextRNode = null;

public Node(int d){

this.a =  d;
}

public int getDate(){
return a;
}

public void setDate(int newDate){
this.a = newDate;
}

public  void setNextNode(Node lNode , Node rNode){
this.nextLNode = lNode;
this.nextRNode = rNode;
}

}

/*
然后得有棵树 树中有节点
*/
class Tree{
//生成一个根节点
public static Node Root = new Node(1);

//插入节点,
public void addNode(Node n){

}

}

/**
* Tree 测试类
* @author Administrator
*/
public class TreeTest {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息