• Java 双向链表 Mylinkedlist Mylinkedlisttest


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

    }
    

    }

  • 相关阅读:
    WCF bindings comparison z
    DevExpress打印功能 z
    使用Topshelf 5步创建Windows 服务 z
    Log4net中的RollingFileAppender z
    Log4Net在Windows服务中不能记录日志 z
    dev 注册方法 z
    async callback z
    多窗体之间方法调用 z
    [JS6] 通过用户事件事件执行脚本
    [JS5] 利用onload执行脚本
  • 原文地址:https://www.cnblogs.com/thttt/p/11826005.html
Copyright © 2020-2023  润新知