java基础之旅-链表的设计
来源:互联网 发布:videocompressor mac 编辑:程序博客网 时间:2024/05/16 06:50
贴个练习的代码:
package com.liberty;public class A {public static void main(String[] args) {Link link = new Link();link.add("头");link.add("身");link.add("尾");System.out.println(link.size());}}class Link{class Node{private String data;//要保存的对象数据private Node next;//要引用的下一个节点public Node(String data){this.data = data;}public void printNode(){System.out.println(this.data);if(this.next != null){this.next.printNode();}}public void addNode(Node newNode) {if(this.next == null){this.next = newNode;}else{this.next.addNode(newNode);}}public boolean containsNode(String data) {if(data.equals(this.data)){return true;}else{ if(this.next != null){return this.next.containsNode(data); }}return false;}public String getNode(int index) {if(Link.this.foot++ == index){return this.data;}else{//我们之所以可能省略判断this.next部位null//在于我们在前面设定了两个判断:1.index小于链表的count,而且index不为负数return this.next.getNode(index);}}public void setNode(int index,String data) {if(index == Link.this.foot++){this.data = data;}else{this.next.setNode(index,data);}}public void removeNode(Node previous, String data) {if(data.equals(this.data)){previous.next = this.next;}else {this.next.removeNode(this, data);}}}//**********以上为内部类************private Node head;//设置一个根节点private int count = 0;//添加一个计数的int变量public int foot = 0;//在Link类中创建一个索引的初始值public void add(String data){Node newNode = new Node(data);//创建一个Node对象if(this.head == null){//注:data可以为空数据this.head = newNode;}else{this.head.addNode(newNode);}this.count++;//每一次添加一个对象,就执行一次}public void print(){if(this.head == null){return;}else{this.head.printNode();}}public int size(){return this.count;//返回count的值}public boolean isEmpty(){return this.count == 0;}public boolean contains(String data){if(this.head == null || data == null){return false;}else{return this.head.containsNode(data);}}public String get(int index){//通过索引获取指定的内容if(index > this.count || this.head == null || index < 0){return null;}else{this.foot = 0;return this.head.getNode(index);}}public void set(int index,String data){if(index > this.count || this.head == null || index < 0){return;}else{this.foot = 0;this.head.setNode(index,data);}}public void remove(String data){if(this.contains(data)){if(data.equals(this.head.data)){this.head = this.head.next;}else{this.head.next.removeNode(this.head,data);}this.count--;}}}
0 0
- java基础之旅-链表的设计
- Java基础之图形用户界面的设计
- JAVA基础之设计模式
- java设计模式基础之设计原则
- java最基础类的设计之判断一个类是不是一个设计良好的类
- <<黑马程序员>>java基础之设计模式
- JAVA基础之单例设计
- java基础学习之设计模式 十七
- java基础之----设计模式单例设计模式
- 我的java之旅--一些基础
- java基础之设计模式之单例模式
- JAVA基础_Java的23种设计模式之单例模式
- 【Java基础之二】JDK中使用到的23个经典设计模式
- JAVA基础之——为什么String要设计成不可变的?
- java各种框架的基础设计(转)
- java基础设计的知识点图示
- Java链表之基础应用
- Java基础菜鸟之单例设计模式
- 我的助理辞职了!——看完后大家对这篇文章应该很有感触
- Sqlyog之excel数据导入方法
- C++ I/O库流状态标志位
- baby-step算法
- 安卓bitmap图片保存到系统图库
- java基础之旅-链表的设计
- 用java定义三维空间的点
- 你有被stringstream坑过吗?
- c++中transform
- 在公司有用的资源
- Unix系统编程(2) - 文件系统
- 89-----hibernate的hql查询,原生SQL查询,命名查询,投影查询
- Android微信自动回复及锁屏控制界面
- 微软研究院图像识别挑战赛 MSR Image Recognition Challenge (IRC)