内 部 类、链表

   内部类

    1、成员内部类:直接在类中定义的类
    2、方法内部类:在一个类中的方法内定义一个类
        (1)方法内部类只能在定义该内部类的方法内实例化,不可以在此方法外对其实例化
        (2)方法内部类对象不能使用该内部类所在方法的非final局部变量。
    3、静态内部类,在类中定义一个静态修饰的内部类,
        静态的含义是该内部类可以像其他静态成员一样,没有外部类对象时,也能够访问它。
        静态嵌套类仅能访问外部类的静态成员和方法。
    4、匿名内部类就是没有名字的内部类。
        匿名内部类的三种情况:
        (1)继承式的匿名内部类
        (2)接口式的匿名内部类
        (3)参数式的匿名内部类

public class Test{
	public static void main(String[] args){
		
		Outer outer = new Outer();
		//在外部创建成员内部类的实例,因为成员内部类需要依赖外部类的对象,
		//通常情况下,我们不建议这样来实例化内部类的对象
		//Outer.Inner inner = outer.new Inner();
		//inner.print();
		
		outer.innerPrint();
		outer.show(5);
		
		Outer.Inner3 inner3 = new Outer.Inner3();
		inner3.print();
		
		outer.print1();
		outer.print2();
		
		//参数式匿名内部类
		outer.print3(new Eat(){
			public void eat(){
				System.out.println("参数式匿名内部类");
			}
		});
	}
}

递归算法

在链表数据结构中,我们需要使用到递归算法。递归算法是一种直接或者间接地调用自身算法的过程。在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于理解。
 递归算法。方法本身调用自己
    1、递归必须要有出口
    2、递归内存消耗大,容易发生内存溢出
    3、层次调用越多,越危险

public class Test14{
    public static void main(String[] args){
        int result = jiecheng2(100);
        System.out.println(result);
    }
    
    //递归算法。方法本身调用自己
    //1、递归必须要有出口
    //2、递归内存消耗大,容易发生内存溢出
    //3、层次调用越多,越危险
    public static int jiecheng2(int num){
        if(num==1)return 1;
        return num*jiecheng2(num-1);
    }
    
    public static int jiecheng1(int num){
        int result = num;
        int i = num-1;
        do{
            result = result * i;
            i--;
        }while(i>1);
        return result;
    }
}

链表


/**
	链表
	一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,
	而是在每一个节点里存到是下一个节点的指针(Pointer)。
	
	链表与数组:线性数据结构
	
	数组适合查找,遍历,固定长度
	链表适合插入,删除,不宜过长,否则会导致遍历性能下降
	

*/
public class Test{
	public static void main(String[] args){
		
		NodeManager nm = new NodeManager();
		System.out.println("------add----------");
		nm.add(5);
		nm.add(4);
		nm.add(3);
		nm.add(2);
		nm.add(1);
		nm.print();
		System.out.println("-------del---------");
		nm.del(3);
		nm.print();
		System.out.println("-------find---------");
		System.out.println(nm.find(1));
		System.out.println("-------update---------");
		nm.update(1,10);
		nm.print();
		System.out.println("-------insert---------");
		nm.insert(1,20);
		nm.print();
		
	}
}

class NodeManager{
	private Node root;//根节点
	private int currentIndex = 0;//节点的序号,每次操作从0开始
	
	//添加
	public void add(int data){
		if(root==null){
			root = new Node(data);
		}else{
			root.addNode(data);
		}
	}
	//删除
	public void del(int data){
		if(root==null)return;
		if(root.getData()==data){
			root = root.next;
		}else{
			root.delNode(data);
		}
	}
	//打印所有
	public void print(){
		if(root!=null){
			System.out.print(root.getData()+"->");
			root.printNode();
			System.out.println();
		}
	}
	//查找是否存在节点
	public boolean find(int data){
		if(root==null)return false;
		if(root.getData()==data){
			return true;
		}else{
			return root.findNode(data);
		}
	}
	//更新
	public boolean update(int oldData,int newData){
		if(root==null)return false;
		if(root.getData()==oldData){
			root.setData(newData);
			return true;
		}else{
			return root.updateNode(oldData,newData);
		}
	}
	//向索引之前插入
	public void insert(int index,int data){
		if(index<0)return;
		currentIndex = 0;
		if(index==currentIndex){
			Node newNode = new Node(data);
			newNode.next = root;
			root = newNode;
		}else{
			root.insertNode(index,data);
		}
	}
	
	
	private class Node{
		private int data;
		private Node next; //把当前类型作为属性
		public Node(int data){
			this.data = data;
		}
		public void setData(int data){
			this.data = data;
		}
		public int getData(){
			return data;
		}
		//添加节点
		public void addNode(int data){
			if(this.next==null){
				this.next = new Node(data);
			}else{
				this.next.addNode(data);
			}
		}
		//删除节点
		public void delNode(int data){
			if(this.next!=null){
				if(this.next.data==data){
					this.next = this.next.next;
				}else{
					this.next.delNode(data);
				}
			}
		}
		//输出所有节点
		public void printNode(){
			if(this.next!=null){
				System.out.print(this.next.data+"->");
				this.next.printNode();
			}
		}
		//查找节点是否存在
		public boolean findNode(int data){
			if(this.next!=null){
				if(this.next.data==data){
					return true;
				}else{
					return this.next.findNode(data);
				}
			}
			return false;
		}
		//修改节点
		public boolean updateNode(int oldData,int newData){
			if(this.next!=null){
				if(this.next.data==oldData){
					this.next.data = newData;
					return true;
				}else{
					return this.next.updateNode(oldData,newData);
				}
			}
			return false;
		}
		//插入节点
		public void insertNode(int index,int data){
			currentIndex++;
			if(index==currentIndex){
				Node newNode = new Node(data);
				newNode.next = this.next;
				this.next = newNode;
			}else{
				this.next.insertNode(index,data);
			}
		}
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值