java單向鏈表的實(shí)現(xiàn)實(shí)例
package ncu.com.app.chatpter_5;
import java.util.Random;
//結(jié)點(diǎn)類
class Node {
Object data;
Node next;
}
//操作類
class ListNode{
public Node first;
public int size;
public ListNode(){
first = null;
size = 0;
}
public void insertNode(Object node){
Node no = new Node();
no.data = node;
no.next = first;
first = no;
size++;
}
public void disPlay(){
if(size==0){
System.out.println("鏈表為空");
}
Node currnode = first;
while(currnode!=null){
System.out.print(currnode.data+",");
currnode = currnode.next;
}
System.out.println("");
}
//刪除i個(gè)結(jié)點(diǎn)
public void delect(int i){
if(i<=size){
for(int m=0;m<i;m++){
first = first.next;
size--;
disPlay();
}
}
}
//清空鏈表
public void delectAll(){
size = 0;
first = null;
disPlay();
}
//獲得從i-j中鏈表的數(shù)據(jù)
public void getNode(int i,int j){
for(int m=0;m<i-1;m++){
first = first.next;
}
Node currnode = first;
for(int m=0;m<j-i+1;m++){
System.out.print(currnode.data+",");
currnode = currnode.next;
}
}
}
public class NodeTree {
public static void main(String args[]){
ListNode listnode = new ListNode();
for(int i = 0;i<10;i++){
int k = new Random().nextInt(10);
listnode.insertNode(k);
System.out.print(k+",");
}
System.out.println("");
listnode.disPlay();
//listnode.delect(10);
//listnode.delectAll();
listnode.getNode(2,8);
}
}
相關(guān)文章
詳解Java?ThreadPoolExecutor的拒絕策略
這篇文章主要介紹了Java?ThreadPoolExecutor的拒絕策略,本文對(duì)于線程的池的幾種策略進(jìn)行詳細(xì)的講解,在實(shí)際的生產(chǎn)中需要集合相關(guān)的場(chǎng)景來(lái)選擇合適的拒絕策略,需要的朋友可以參考下2022-08-08SpringBoot中配置多數(shù)據(jù)源的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot中配置多數(shù)據(jù)源的方法的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02使用maven一步一步構(gòu)建spring mvc項(xiàng)目(圖文詳解)
這篇文章主要介紹了詳解使用maven一步一步構(gòu)建spring mvc項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09在springboot文件中如何創(chuàng)建mapper.xml文件
這篇文章主要介紹了在springboot文件中如何創(chuàng)建mapper.xml文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01