java單向鏈表的實現(xiàn)實例
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個結(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)文章
一文帶你掌握SpringBoot中常見定時任務(wù)的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Spring?Boot中定時任務(wù)的基本用法、高級特性以及最佳實踐,幫助開發(fā)人員更好地理解和應(yīng)用定時任務(wù),提高系統(tǒng)的穩(wěn)定性和可靠性,需要的可以參考下2024-03-03
SpringBoot結(jié)果封裝和異常攔截的實現(xiàn)示例
SpringBoot 項目中,我們通常需要將結(jié)果數(shù)據(jù)封裝成特定的格式,以方便客戶端進(jìn)行處理,本文主要介紹了SpringBoot?優(yōu)雅的結(jié)果封裝和異常攔截,感興趣的可以了解一下2023-08-08
IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程
這篇文章主要介紹了IDEA設(shè)置JVM可分配內(nèi)存大小和其他參數(shù)的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
解決?IDEA?Maven?項目中"Could?not?find?artifact"?
這篇文章主要介紹了解決IDEA Maven項目中Could not?find?artifact問題的常見情況和解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL
這篇文章主要為大家介紹了Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

