首页 / JAVA / java链表的基本使用 代码
java链表的基本使用 代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java链表的基本使用 代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3817字,纯文字阅读大概需要6分钟。
内容图文
![java链表的基本使用 代码](/upload/InfoBanner/zyjiaocheng/636/d2a96db4972f4d8581038ae150eb447d.jpg)
package test;
interface ILink<E>{
public void add(E e);//添加链表数据
public int size();//返回链表长度
public boolean isEmpty();//判断链表是否为空
public Object [] toArray();//返回集合数据
public E get(int index);//返回指定索引的数据
public void set(int index,E data);//修改值
public boolean contains(E data);//判断指定数据是否存在
}
class LinkImpl<E> implements ILink<E>{
private class Node{//保存节点的数据关系
private E data;
private Node next;//保存下一个引用
private Node(E data) {
this.data=data;
}
//第一次调用
public void addNode(Node newNode) {
//保存新的Node数据
if(this.next==null) {
this.next =newNode;
}else {
this.next.addNode(newNode);
}
}
public void toArrayNode() {
LinkImpl.this.returnData [LinkImpl.this.foot ++]=this.data;
if(this.next!=null) {
this.next.toArrayNode();
}
}
public E getNode(int index) {
if(LinkImpl.this.foot ++ ==index) {
return this.data;
}
else {
return this.next.getNode(index);
}
}
public void setNode(int index,E data) {
if(LinkImpl.this.foot ++ ==index) {
this.data=data;
}
else {
this.next.getNode(index);
}
}
public boolean containsNode(E data) {
if(this.data.equals(data)) {
return true;
}
else{
if(this.next==null) {
return false;
}else {
return this.next.containsNode(data);
}
}
}
}
private Node root;
//增加数据
private int count;
private int foot;
private Object[] returnData;
public void add(E e) {
if(e==null) {
return ;
}else {
Node newNode = new Node(e);//创建新节点
if(this.root==null) {
this.root=newNode;
}
else {
this.root.addNode(newNode);
}
this.count++;
}
}
@Override
public int size() {
// TODO Auto-generated method stub
return this.count;
}
@Override
public boolean isEmpty() {
//this.count==0
return this.root==null;
}
@Override
public Object[] toArray() {
if(this.isEmpty()) {
return null;
}
this.foot=0;
this.returnData=new Object[this.count];
this.root.toArrayNode();
return this.returnData;
}
@Override
public E get(int index) {
if(index>this.count) {
return null;
}
this.foot=0;
return this.root.getNode(index);
}
@Override
public void set(int index, E data) {
if(index>=this.count) {
return ;
}
this.foot=0;
this.root.setNode(index, data);
}
@Override
public boolean contains(E data) {
if(data==null) {
return false;
}
return this.root.containsNode(data);
}
}
public class Test1 {
public static void main(String[] args) throws Exception {
LinkImpl<String> all = new LinkImpl<String>();
System.out.println("size:"+all.size());
all.add("hello");
all.add("wo");
System.out.println("-------------");
System.out.println("size:"+all.size());
Object [] array = all.toArray();
for(Object obj:array) {
System.out.print(obj);
}
System.out.println("数据获取");
System.out.println(all.get(1));
System.out.println(all.contains("wo"));
System.out.println(all.contains("women"));
}
}
内容总结
以上是互联网集市为您收集整理的java链表的基本使用 代码全部内容,希望文章能够帮你解决java链表的基本使用 代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。