关于java:技能树基础之章数据结构链表操作

38次阅读

共计 696 个字符,预计需要花费 2 分钟才能阅读完成。

单链表

遍历

public static void forLinked(Linked linked){
    Linked temp = linked;
    while(temp != null){temp = temp.nextNode;}
}

减少

public static void addLinked(Linked linked,int data) {Linked newNode = new Linked(data);
    Linked temp = linked;
    while(temp.nextNode!=null){temp =temp.nextNode;}
    temp.nextNode = newNode;
}

插入

public static void insertLinked(Linked linked,int value,int index){Linked newNode = new Linked(value);
    Linked temp = linked;
    while(temp !=null){
        index--;
        if(index == 1){
            newNode.nextNode = temp.nextNode;
            temp.nextNode = newNode;
        }else{temp = temp.nextNode;}
    }

}

删除

public static void deleteLinked(Linked linked,int index){
    Linked temp = linked;
    while(temp !=null){
        index--;
        if(index == 1){temp.nextNode = temp.nextNode.nextNode;}else{temp = temp.nextNode;}
    }
}

重点在于应用指针指向头结点,遍历指针

正文完
 0