简单链表练习
来源:互联网 发布:火星时代有网络班 编辑:程序博客网 时间:2024/06/06 08:50
class A{ private class B{ private B next;//**节点** private Object data; public B(Object data){ this.data=data; } public void addB(B b){ if(this.next==null){ this.next=b; }else{ this.next.addB(b); } } public void toArrayB(){//输出数组 A.this.Redata[A.this.foot++]=this.data; if(this.next!=null){ this.next.toArrayB(); } } public boolean Contains(Object search){//检验是否存在某数据 if(search.equals(this.data)){ return true; }else{ if(this.next!=null){ return this.next.Contains(search); }else{ return false; } } } public Object getB(int index){//按索引查找数据 if(A.this.foot++==index){ return this.data; }else{ if(this.next!=null){ return this.next.getB(index); }else{ return false; } } } public void setB(int index,Object newData){//按索引更改数据 if(index==A.this.foot++){ this.data=newData; }else{ if(this.next!=null){ this.next.setB(index, newData); } } } public void removeB(Object data,B previous){//按数据删除数组 if(this.data.equals(data)){ previous.next=this.next; }else{ this.next.removeB(data,this); } } } //========================= private B root;//根目录 private int count ; private int foot; private Object []Redata; public void add(Object data){//检验输入数据正确性 if(data==null){ return; } B b=new B(data); if(this.root==null){//如果没有根目录建立一个 this.root=b; }else{ this.root.addB(b); } count++; } public Object []toArray(){//根据根目录输出数组 if(this.count==0){ return null; } this.Redata=new Object[this.count]; this.foot=0; this.root.toArrayB(); return this.Redata; } public boolean Contain(Object search){//检验 if(this.root==null){ return false; }else{ return this.root.Contains(search); } } public void set(int index,Object newData){ if(index>=this.count){ return; }else{ this.foot=0; this.root.setB(index, newData); } } public Object get(int index){ if(index>=this.count&&index<0){ return null; }else{ this.foot=0; return this.root.getB(index); } } public int getCount(){ return this.count; } public void remove(Object data){ if(this.Contain(data)){ if(this.root.data.equals(data)){ this.root=this.root.next; }else{ this.root.next.removeB(data,this.root); }this.count--; } }}public class test3 { public static void main(String args[]){ A a=new A(); a.add(3); a.add(4); a.set(0, 1); a.set(1, 2); a.add(5); a.set(2, 3); a.remove(2); Object []data=a.toArray(); for(int x=0;x<data.length;x++){ System.out.println(data[x]); } System.out.println("==========="); System.out.println(a.Contain(2)); System.out.println(a.get(0)); }}
阅读全文
0 0
- 简单链表练习
- 简单的链表相关练习
- 简单链栈练习
- 一道简单的有关链表的练习
- 写个内核链表,练习练习
- 综合练习:简单日历
- 综合练习:简单日历
- C++简单练习
- C++简单练习0
- C++简单练习题目
- 简单linux命令练习
- 一个简单的练习
- JSP简单练习
- MSQL的简单练习
- MYSQL的简单练习
- 简单选择排序练习
- js 简单练习节点
- SWT练习:简单计算器
- 表单提交的 三种方式
- 快速幂
- 关于const 限定符总结
- 最小生成树的两种算法图解(Kruskal与prim)
- hive 函数
- 简单链表练习
- Android Widgets 之 Space 使用详解
- 811grep
- 4 设计与声明
- HDU 3944 DP?(lucas定理 组合数)
- .NET漫游指南-003-事件
- 算法十八
- 奇偶校验通俗易懂
- 【FFT】BZOJ2194 快速傅立叶之二