ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LinkList怎么手动创建、手动写一个

2021-08-04 23:01:39  阅读:274  来源: 互联网

标签:int 创建 手动 next LinkList Link data public cu


public class Link {

    /**
     * 存放数据
     */
    public int data;

    /**
     * 存放下一个节点
     */
    public Link next;

    public Link(int data) {
        this.data = data;
    }

    public Link(int data, Link next) {
        this.data = data;
        this.next = next;
    }

    public Link() {
    }

    public void display() {
        System.out.println(data + " ");
    }

    @Override
    public String toString() {
        return "Link{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}
public class LinkedList {

    public Link first;// 定义一个头节点
    public Link last;//尾指针永远指向头节点
    public int size = 0;// 节点的位置

    public LinkedList() {
        this.first = null;//
    }

    /**
     * 判断链表是否为空
     *
     * @return
     */
    public boolean isis() {
        return size == 0;
    }

    /**
     * 头插法
     *
     * @param data
     */
    public void addFirst(int data) {
        Link L = new Link(data);
        L.next = first;
        first = L;
        size++;
    }

    /**
     * 插入
     *
     * @param data
     */
    public void add(int data) {

        if (first == null) {
            first = new Link(data);
            last = first;
        } else {
            Link newL = new Link(data);
            last.next = newL;
            last = newL;
        }
        size++;
    }

    /**
     * 从头删除
     *
     * @return
     */
    public Link removeFirst() {
        Link d = first;
        first = d.next;
        size--;
        return d;
    }

    /**
     * 删除最后一个
     */
    public void delLast() {
        dell(size - 1);
    }

    /**
     * 获取链表长度
     */
    public void displayAllLink() {
        Link cure = first;
        while (cure != null) {
            cure.display();
            cure = cure.next;
        }
        System.out.println("长度" + size);
    }

    /**
     * 获取指定位置的节点元素
     *
     * @param index
     * @return
     */
    public Link getData(int index) {
        if (index < 0 && index > size - 1) {
            throw new IndexOutOfBoundsException("An array");
        }
        Link count = first;
        for (int i = 0; i < size && count != null; i++, count = count.next) {
            if (i == index) {
                return count;
            }
        }
        return null;
    }

    /**
     * 按值查找指定位置
     *
     * @param element
     * @return
     */
    public int selectIndex(int element) {
        Link current = first;
        for (int i = 0; i < size && current != null; i++, current = current.next) {
            if (current.data == element) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 删除链表中数值最大的元素
     */
    public void delMax() {
        // 要遍历的链表
        Link cu = first;
        // 初始化一个节点,当中间变量
        Link cc = new Link(0);
        // 遍历
        for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
            if (cu.data > cc.data) {
                cc.data = cu.data;
            }
        }
        int data = cc.data;
        int number = selectIndex(data);
        dell(number);
    }

    /**
     * 删除链表中数值最小的元素
     */
    public void delMin() {
        // 要遍历的链表
        Link cu = first;
        // 初始化一个节点,当中间变量
        Link cc = new Link(0);
        // 遍历
        for (int i = 0; i < size && cu != null; i++, cu = cu.next) {
            if (cu.data < cc.data) {
                cc.data = cu.data;
            }
        }
        int data = cc.data;
        int number = selectIndex(data);
        dell(number);
    }

    /**
     * 从指定位置处插入数据
     *
     * @param t
     * @param index
     */
    public void insert(int t, int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引超出线性表范围");
        }
        if (first == null) {
            addFirst(t);
        } else {
            if (index == 0) {
                addFirst(t);
            } else {
                Link k = getData(index - 1);
                k.next = new Link(t, k.next);
                size++;
            }

        }
    }

    /**
     * 从指定位置处删除
     *
     * @param index
     */
    public void dell(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("索引超出线性表范围");
        }
        Link del;
        if (index == 0) {
            first = first.next;
        } else {
            Link neL = getData(index - 1);
            del = neL.next;
            neL.next = del.next;
            del.next = null;
        }
        size--;
    }

    /**
     * 清空链表
     */
    public void clear() {
        first = null;
        last = null;
        size = 0;
    }

    /**
     * 按从小到大排序
     */
    public void Min_to_Max() {
        // 要遍历的链表
        Link cu = first;
        // 记录最小值
        int min;
        while (cu != null) {
            // 内重循环从当前节点的下一个节点循环到尾节点,找到和外重循环的值比较最小的那个,然后与外重循环进行交换
            Link nextLink = cu.next;
            while (nextLink != null) {
                // 比外循环小的值放在前面
                if (nextLink.data < cu.data) {
                    min = nextLink.data;
                    nextLink.data = cu.data;
                    cu.data = min;
                }
                nextLink = nextLink.next;
            }
            cu = cu.next;
        }

    }

    /**
     * 按从大到大排序
     */
    public void Max_to_Min() {
        // 要遍历的链表
        Link cu = first;
        // 记录最小值
        int min;
        while (cu != null) {
            // 内重循环从当前节点的下一个节点循环到尾节点
            //找到和外重循环的值比较最小的那个,然后与外重循环进行交换
            Link nextLink = cu.next;
            while (nextLink != null) {
                // 比外循环小的值放在前面
                if (nextLink.data > cu.data) {
                    min = nextLink.data;
                    nextLink.data = cu.data;
                    cu.data = min;
                }
                nextLink = nextLink.next;
            }
            cu = cu.next;
        }

    }

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add(13);
        linkedList.add(14);
        linkedList.add(15);


        for (int i = 0; i < linkedList.size; i++) {
            System.out.println(linkedList.getData(i));
        }

        linkedList.dell(0);

        for (int i = 0; i < linkedList.size; i++) {
            System.out.println(linkedList.getData(i));
        }
    }
}

标签:int,创建,手动,next,LinkList,Link,data,public,cu
来源: https://blog.csdn.net/qq_42208194/article/details/119395171

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有