数据结构单向链表的实现

数据结构单向链表的实现

理论基础

链表介绍

优点

链表 和 一维数组是 对比的产品。
链表可以理解为 牺牲掉一维数组可以直接通过指定下标访问这个功能。增加动态扩容这个功能
以及增删不用对后面的数据处理。
非常试用那些不知道数组长度该给多少的情况以及频繁在中间增删。

jdk原生的支持

在java.util.LinkedList 下面具有支持。可以直接使用。

代码实现

节点实体类

package linkTable;

public class Node<V> {

    private  Node<V> next;
    private V value;


    public Node(Node<V> next, V value) {
        this.next = next;
        this.value = value;
    }

    public Node( V value) {
        this.value = value;
    }

    public Node() {
    }

    public Node<V> getNext() {
        return next;
    }

    public void setNext(Node<V> next) {
        this.next = next;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }
}

链表实体类,其实也可以做个工具类封装方法

package linkTable;

public class LinkTable<V> {


    private Node<V> head;

    private Node<V> tail;

    private int size;

    public LinkTable(Node<V> head) {
        this.head = head;
        this.tail = head;
        this.size=1;
    }

    // ---------------------------    增量操作 ------------------------------------------//


    /**
     * 批量加入尾部
     * @param values
     */
    public void dataChange(V[] values){
        for (int i = 0; i <values.length ; i++) {
            this.addTail(values[i]);
        }
    }


    /**
     * 新增在尾节点
     * @param value
     */
    public void addTail(V value){
        Node<V> vNode=new Node<V>(null,value);
        this.tail=vNode;
        this.size++;
        getIndex(this.size-2).setNext(vNode);
    }

    /**
     * 新增在头节点
     * @param value
     */
    public void addHead(V value){
        Node<V> temp=new Node<V>(head,value);
        this.head=temp;
        this.size++;
    }

    /**
     * 在指定位置新增节点
     * @param i
     * @param value
     */
    public void addIndex(int i,V value){
        if (i==0){
            addHead(value);
        }else if (i==size){
            addTail(value);
        }else  if (i>size){
            try {
                throw new  Exception("数组下标越界");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Node<V> index = getIndex(i-1);
            Node<V> temp=new Node<V>(index.getNext(),value);
            index.setNext(temp);
            this.size++;
        }
    }

    // ---------------------------    删除操作 ------------------------------------------//


    /**
     * 删除指定节点
     */
    public void delete(int i){
        if(this.size==1){
            try {
                throw new  Exception("不允许删除最后一个元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (i==0){
           this.head=this.head.getNext();
            this.size--;
        }else if (i==size-1){
            Node<V> index = this.getIndex(size - 2);
            index.setNext(null);
            this.size--;
        }else  if (i>=size){
            try {
                throw new  Exception("数组下标越界");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Node<V> index = this.getIndex(i - 1);
            index.setNext(index.getNext().getNext());
            this.size--;
        }

    }


    // ---------------------------    修改操作操作 ------------------------------------------//

    public void editIndex(int i,V value){
        if (i>size || i<0){
            try {
                throw new  Exception("数组下标越界");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Node<V> index = getIndex(i);
            index.setValue(value);
        }
    }



    // ---------------------------    查询操作 ------------------------------------------//

    /**
     * 获取指定下标获取的值
     */
    public Node<V> getIndex(int i) {
       if (i==0){
            return head;
        }else if (i==size-1){
            return tail;
        }else  if (i>=size){
           try {
               throw new  Exception("数组下标越界");
           } catch (Exception e) {
               e.printStackTrace();
           }finally {
               return null;
           }
        } else {
           Node<V> temp = head.getNext();
           for (int j = 1; j < i; j++) {
               temp = temp.getNext();
           }
           return temp;
       }
    }


    /**
     * 展示所有细节
     */
    public void showAll(){
        StringBuffer buffer=new StringBuffer();
        if (this.size==1){
            buffer.append("【").append(head.getValue()).append("】");
        }else {
            buffer.append("【").append(head.getValue()).append(",");
            Node<V> temp = head.getNext();
            for (int j = 1; j < this.size; j++) {
                if (j == this.size - 1) {
                    buffer.append(temp.getValue()).append("】");
                } else {
                    buffer.append(temp.getValue()).append(",");
                }
                temp = temp.getNext();
            }
        }


        System.out.println(buffer);

    }


    /**
     * 查询连表长度
     * @return
     */
    public int getSize() {
        return size;
    }
}

测试代码

package linkTable;

import java.util.LinkedList;

public class TestMain {


    public static void main2(String[] args) {

        Node<String> head=new Node<String>("周瑜");
        LinkTable<String> link =new LinkTable<String>(head);

        // 测试新增
        link.addHead("亲密的人");
        link.addIndex(1,"黄盖");
        link.showAll();

        // 测试编辑
        link.editIndex(0,"小乔");
        link.showAll();

        //测试查询链表长度
        System.out.println(link.getSize());

        // 测试删除
        link.delete(1);
        link.showAll();
        link.delete(1);
        link.showAll();
    }

    public static void main3(String[] args) {
        Node<String> head2=new Node<String>("第一个元素");
        LinkTable<String> link2 =new LinkTable<String>(head2);

        link2.dataChange(new String[]{"第二个元素","第三个元素","第四个元素","第五个元素"});
        link2.showAll();
    }

    public static void main4(String[] args) {
        Node<String> head=new Node<String>("第一个元素");
        LinkTable<String> link =new LinkTable<String>(head);

        link.addTail("第二个元素");
        link.addTail("第三个元素");

        link.addHead("fukcer");
        link.showAll();

        link.addIndex(1,"覆盖原来的第一个元素");
        link.showAll();

        link.editIndex(1,"修改第一个元素");
        link.showAll();

        link.delete(1);
        link.showAll();

        link.delete(3);
        link.showAll();
    }

    public static void main(String[] args) {

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值