zoukankan      html  css  js  c++  java
  • java学习笔记——可用链表

    NO 链表方法名称 描述
    1 public void add(数据类型 对象) 向链表中增加数据
    2

    public int size()

    查看链表中数据个数
    3 public boolean isEmpty() 查看链表是否为空
    4 public void clean() 清空链表
    5 public 数据类型 get(int index) 返回指定索引的数据对象,需要使用自定义类中的Compare()函数方法
    6 public boolean contains(数据类型  对象) 查看链表中是否包含数据对象,需要使用自定义类中的Compare()函数方法
    7 public  void remove(数据类型 对象) 删除链表中数据对象,需要使用自定义类中的Compare()函数方法
    8 public 数据类型[] toArray() 转换为数据对象数组
    class Person{
    String name;
    int age; public Person(String name,int age) { // TODO Auto-generated constructor stub this.name=name; this.age=age; } public boolean Compare(Person person){ if(person==null) return false; if(this==person) return true; if(this.name==person.name&&this.age==person.age) return true; return false; } public void getInfo(){ System.out.println("name:"+name+",age:"+age); } } class Link{ private class Node{ private Person data; private Node next; public Node(Person data) { // TODO Auto-generated constructor stub this.data=data; } /**********************************************/ public void addNode(Node newNode){ if(this.next==null) this.next=newNode; else { this.next.addNode(newNode); } } /*********************************************/ public Person getNode(int index){ if(index==Link.this.foot++)//注意:内部类能直接访问外部类的变量 return this.data; return this.next.getNode(index); } /*********************************************/ public boolean containsNode(Person data){ if(this.data.Compare(data)) return true; else if(this.next==null)/*重点*/ return false; return this.next.containsNode(data); } public void removeNode(Node previous,Person data){ if(this.data.Compare(data)) previous.next=this.next; else this.next.removeNode(this, data); } public void toarrayNode(){ Link.this.retArray[Link.this.foot++]=this.data; if(this.next!=null) this.next.toarrayNode(); } /*********************************************/ } private Node root; private int count=0;//结点个数 private int foot;//为了查找相关位置节点 private Person [] retArray; public void add(Person data){ Node newNode=new Node(data); if(root==null) root=newNode; else { root.addNode(newNode); } count++; } public int size(){ return count; } public boolean isEmpty(){ return this.count==0; } public void clean(){//java的虚拟机会自动回收那些分配的空间 root=null; count=0; } public Person get(int index){ if(index>count) return null; this.foot=0; return this.root.getNode(index); } public boolean contains(Person data){ if(data==null) return false; return this.root.containsNode(data); } public void remove(Person data){ if(this.contains(data)){ if(this.root.data==data) this.root=this.root.next; else this.root.next.removeNode(root, data);//重点 this.count--; } } public Person [] toArray(){ if(this.count==0) return null; retArray=new Person[count]; this.foot=0; this.root.toarrayNode(); return retArray; } } public class LinkDemo { public static void main(String[] args) { // TODO Auto-generated method stub Link link=new Link(); link.add(new Person("张三", 20)); link.add(new Person("李四", 21)); link.add(new Person("王五", 22)); System.out.println("size:"+link.size()); Person[] per1=link.toArray(); for(int i=0;i<per1.length;i++) per1[i].getInfo(); Person tmp=new Person("李四", 21); link.remove(tmp); Person[] per2=link.toArray(); for(int i=0;i<per2.length;i++) per1[i].getInfo(); } }
  • 相关阅读:
    WP&Win10仿微信消息框代码分享
    Windows编程中回调函数的使用心得(MFC篇)
    进程间通信之WM_COPYDATA方式反思,回顾和总结
    华为的最新的两道算法设计题
    Python中3元运算符的实现
    Python Logging 模块研究
    LINUX下CPU Load Average的一点研究
    64位系统下,一个32位的程序究竟可以申请到多少内存,4GB还是更多?(一)
    Pyscripter 不能正确调用另一文件中模块的问题的解析(Internal Engine 和 Remote Engine)
    Django 最佳实践
  • 原文地址:https://www.cnblogs.com/lukexwang/p/4591834.html
Copyright © 2011-2022 走看看