码迷,mamicode.com
首页 > 编程语言 > 详细

Java 双向链表 Mylinkedlist Mylinkedlisttest

时间:2019-11-09 15:36:55      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:存储   i++   app   删除节点   this   equals   else   需要   rev   

package list;

public class Mylinkedlist {
private Node first; //第一个节点
private Node last; //最后一个节点
private int size = 0; // 节点的数量

public void addFrist(Object object){
    Node node = new Node(object);
    if (size == 0){
        this.first = node;
        this.last  = node;
    }else {
        //把新增节点的下一个节点为第一个
       node.next =  this.first;
       //吧新增节点作为第一个节点的上一个
        this.first.prev = node;
        //吧新增节点作为第一个节点
        this.first = node;

    }
    size ++;
}


public  void addLast(Object object){
    //需要保存的节点对象
    Node node = new Node(object);
    if (size == 0){
        this.first = node;
        this.last = node;
    }else {
       //把新增节点作为之前最后一个节点的下一个节点
        this.last.next = node;
        //把之前最后一个节点作为新增节点的上一个节点
        node.prev = this.last;
        //把新增节点作为最后一个节点
        this.last = node;
    }
    size ++;
}
//根据位置索引删除节点
public void removeindex(int index){
    if (index>=size){
        System.out.println("位置大于链表长度");
    }
    Node current = this.first;
    if (index==0){
        this.first = first.next;
    }else {
        int pos = 0;
        while (pos != index){
            current = current.next;
            pos++;
        }
         current.prev.next= current.next;
         current =current.prev;

    }
    size--;
}
//根据节点内容查找节点位置
public void findindex(Object object){
    Node current = this.first;
    if (current.next == null){

    }
}
//根据节点内容删除节点
public void remove(Object object){
    //找到被删除的节点
    Node current = this.first;
    for(int i =0;i<size;i++){
        if (!current.ele.equals(object)){
            if (current.next ==null){
                return;
            }
            current = current.next;
        }
    }

    //删除节点;
    if (current ==first){
        this.first = current.next;
        this.first.prev =null;
    }else if( current == last) {
        this.last = current.prev;
        this.last.next = null;
    }else {
        //把删除当前节点的下一个节点作为删除节点的上衣节点的next
        current.prev.next = current.next;
        //把删除节点的上一个节点作为删除节点的下一个节点的prev
        current.next.prev = current.prev;

    }
    size--;
}

@Override
public String toString(){
    if (size == 0){
        return "[]";
    }
    StringBuilder stringBuilder = new StringBuilder(size*2+1);
    Node current = this.first;
    stringBuilder.append("[");
    for (int i = 0;i<size;i++){
        stringBuilder.append(current.ele);
        if (i !=size-1){
            stringBuilder.append(",");
        }else {
            stringBuilder.append("]");
        }
        current = current.next;
    }
    return stringBuilder.toString();
}

//链表中的每一个节点 使用内部类
class Node{
    Node prev; //上一个节点
    Node next; //下一个节点
    Object ele; //当前节点中存储的数据

    public Node(Object ele){
        this.ele = ele;
    }
}

}

+=============================

package list;

public class Mylinkedlisttest {
public static void main(String[] args) {
Mylinkedlist mylinkedlist = new Mylinkedlist();
mylinkedlist.addLast("B");
mylinkedlist.addLast("D");
mylinkedlist.addFrist("CC");
mylinkedlist.addFrist("AA");
System.out.println(mylinkedlist.toString());
//mylinkedlist.remove("D");
//System.out.println(mylinkedlist.toString());
mylinkedlist.removeindex(2);
System.out.println(mylinkedlist.toString());

}

}

Java 双向链表 Mylinkedlist Mylinkedlisttest

标签:存储   i++   app   删除节点   this   equals   else   需要   rev   

原文地址:https://www.cnblogs.com/thttt/p/11826005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!