目录
1.java 单向链表
package javalearn;
public class SingleLinkedList {
private int size;
private Node head;
public SingleLinkedList(){
size=0;
head=null;
}
private class Node{
private Object data;
private Node nextNode;
public Node(Object obj){
this.data=obj;
}
}
public Object addData(Object obj){
Node node = new Node(obj);
if(size==0){
head=node;
}else{
node.nextNode=head;
head=node;
node.data=obj;
}
size++;
return obj;
}
public static void main(String[] args){
SingleLinkedList list = new SingleLinkedList();
list.addData("aa");
list.addData("bb");
System.out.print(list.head.data);
}
}
2.java 双向链表
package javalearn;
public class TowWayLinkedList {
private int size;
private Node head;
private Node tail;
public TowWayLinkedList(){
size=0;
head=null;
tail=null;
}
private class Node{
private Object data;
private Node nextNode;
private Node prevNode;
public Node(Object obj){
this.data=obj;
}
}
public Object addHead(Object obj){
Node newNode = new Node(obj);
if(size==0){
head=newNode;
tail=newNode;
}else{
newNode.nextNode=head;
//newNode.nextNode.prevNode=newNode;
head.prevNode=newNode;
head=newNode;
newNode.data=obj;
}
size++;
return obj;
}
public Object addTail(Object obj){
Node newNode = new Node(obj);
if(size==0){
head=newNode;
tail=newNode;
}else{
newNode.prevNode=tail;
tail.nextNode=newNode;
//newNode.prevNode.nextNode=newNode;
tail=newNode;
newNode.data=obj;
}
size++;
return obj;
}
public static void main(String[] args){
TowWayLinkedList list = new TowWayLinkedList();
list.addHead("aa");
list.addHead("bb");
list.addTail("cc");
System.out.print(list.tail.prevNode.prevNode.data);
}
}
3.二叉树
package javalearn;
public class BinaryTreeTest {
// 根节点
private TreeNode root;
public TreeNode getRoot() {
return root;
}
private class TreeNode {
// 左节点
private TreeNode lefTreeNode;
// 右节点
private TreeNode rightNode;
// 数据
private Object value;
private boolean isDelete;
public TreeNode getLefTreeNode() {
return lefTreeNode;
}
public void setLefTreeNode(TreeNode lefTreeNode) {
this.lefTreeNode = lefTreeNode;
}
public TreeNode getRightNode() {
return rightNode;
}
public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
public Object getValue() {
return value;
}
public int getValueHashCode(){
return value.hashCode();
}
public void setValue(Object value) {
this.value = value;
}
public boolean isDelete() {
return isDelete;
}
public void setDelete(boolean isDelete) {
this.isDelete = isDelete;
}
public TreeNode() {
super();
}
public TreeNode(Object value) {
this(null, null, value, false);
}
public TreeNode(TreeNode lefTreeNode, TreeNode rightNode, Object value,
boolean isDelete) {
super();
this.lefTreeNode = lefTreeNode;
this.rightNode = rightNode;
this.value = value;
this.isDelete = isDelete;
}
@Override
public String toString() {
return "TreeNode [lefTreeNode=" + lefTreeNode + ", rightNode="
+ rightNode + ", value=" + value + ", isDelete=" + isDelete
+ "]";
}
}
public void insert(Object obj){
TreeNode newNode = new TreeNode(obj);
TreeNode currentNode=root;
if(root == null) {
root=newNode;
}else{
while(true){
if(obj.hashCode() > currentNode.getValue().hashCode()){
if(currentNode.getRightNode() == null){
currentNode.setRightNode(newNode);
return;
}else{
currentNode = currentNode.getRightNode();
}
} else{
if(currentNode.getLefTreeNode() == null) {
currentNode.setLefTreeNode(newNode);
return;
}else{
currentNode = currentNode.getLefTreeNode();
}
}
}
}
}
public TreeNode find(Object obj){
TreeNode currentNode = root;
if(root == null){
return null;
}
while(currentNode.getValue()!=obj){
if(obj.hashCode()>currentNode.getValueHashCode()){
currentNode=currentNode.getRightNode();
}else{
currentNode=currentNode.getLefTreeNode();
}
if(currentNode==null){
return null;
}
}
return currentNode;
}
public static void main(String[] args){
BinaryTreeTest tree = new BinaryTreeTest();
tree.insert("11");
tree.insert("12");
tree.insert("9");
System.out.println("11".hashCode());
System.out.println("12".hashCode());
System.out.println("9".hashCode());
System.out.println(tree.find("11"));
}
}
4.递归
递归的经典例子 1。汉诺塔 这是递归的超经典的例子,几乎每本程序设计书上谈到递归都会介绍。
汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。
(1)递归的出口在于disk数为一的时候
(2)向出口逼近:如果不是一,是n ,则我们先挪动上面n-1块disk,等上面挪完,即递归返回的时候,我们挪动最底下的disk。 仅仅如此,一个貌似十分复杂的问题就解决了,因为挪动那n-1块disk的时候,会继续向上减少,直到disk的数量为一为止。
package javalearn;
import javax.swing.JOptionPane;
public class Hanio {
private static final String DISK_B = "diskB";
private static final String DISK_C = "diskC";
private static final String DISK_A = "diskA";
static String from=DISK_A;
static String to=DISK_C;
static String mid=DISK_B;
private static void move(int num, String from2, String mid2, String to2) {
if(num==1){
System.out.println("move disk 1 from "+from2+" to "+to2);
} else {
move(num-1,from2,to2,mid2);
System.out.println("move disk "+num+" from "+from2+" to "+to2);
move(num-1,mid2,from2,to2);
}
}
public static void main(String[] args) {
String input=JOptionPane.showInputDialog("please input the number of the disks you want me move.");
int num=Integer.parseInt(input);
move(num,from,mid,to);
}
}