日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java链表代码,java链表的基本使用 代码

發布時間:2024/2/28 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java链表代码,java链表的基本使用 代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

package test;

interface ILink{

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 implements ILink{

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 all = new LinkImpl();

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"));

}

}

標簽:index,return,int,代碼,next,鏈表,java,data,public

來源: https://www.cnblogs.com/yxj808/p/12644306.html

總結

以上是生活随笔為你收集整理的java链表代码,java链表的基本使用 代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。