单链表的基本操作(一)(排序、删除重复元素、反转、求倒数第k个元素等)

一、单链表的基本操作增、删、改、查

import java.util.ArrayList;

/**
 * @ClassName LinkList
 * @Description 单链表基本操作增、删、改、查
 * @Author lzq
 * @Date 2018/11/21 20:25
 * @Version 1.0
 **/
public class LinkList<T> {
    public class Entry {
        T data;
        public Entry next;

        //头节点
        public Entry() {
            this.data = null;
            this.next = null;
        }
        //数据节点
        public Entry(T val) {
            this.data = val;
            this.next = null;
        }
    }


    public Entry head;  //头节点的引用
    private Entry entry; //声明一个数据节点
    private Entry count; //一个移动的标记指针

    public LinkList() {
        this.head = new Entry();
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty() {
        return head.next == null;
    }

    /**
     * 头插法,从链表头部进行插入
     * @param val
     */
    public void head_insert(T val) {
        this.entry = new Entry(val);
        entry.next = head.next;
        head.next = entry;
    }

    /**
     * 尾插法,从链表尾部插入
     * @param val
     */
    public void tail_insert(T val) {
        count = this.head;
        while(count.next != null) {
            count = count.next;
        }
        entry = new Entry(val);
        count.next = entry;
    }

    /**
     * 指定位置插入元素
     * @param pos
     * @param val
     * @return
     */
    public boolean insert(int pos,T val) {
        if(pos < 0 || pos >= get_length()) {
            return false;
        }
        entry = new Entry(val);
        count = this.head.next;
        Entry cur = this.head;
        int i = 0;
        while(i != pos) {
            i++;
            count = count.next;
            cur = cur.next;
        }
        cur.next = entry;
        entry.next = count;
        return true;
    }

    /**
     * 删除指定元素(默认删除第一次出现的)
     * @param val
     * @return
     */
    public boolean delete(T val) {
        if(isEmpty()) {
            return false;
        }
        count = this.head;
        while(count.next != null) {
            if (count.next.data == val) {
                count.next = count.next.next;
                return true;
            }
            count = count.next;
        }
        return false;
    }

    /**
     * 删除所有的指定元素
     * @param val
     * @return
     */
    public boolean delete1(T val) {
        if(isEmpty()) {
            return false;
        }
        count = this.head;
        while(count.next != null) {
            if (count.next.data == val) {
                count.next = count.next.next;
                continue;
            }
            count = count.next;
        }
        return true;
    }

    /**
     * 删除所有的指定元素
     * @param val
     * @return
     */
    public boolean remove1(T val) {
        if(isEmpty()) {
            return false;
        }
        Entry pre = this.head;
        Entry cur = this.head.next;
        while(cur != null) {
            if(cur.data == val) {
                pre.next = cur.next;
                cur = pre.next;
                continue;
            }
            pre = cur;
            cur = cur.next;
        }
        return true;
    }

    /**
     * 删除指定位置元素
     * @param pos
     * @return
     */
    public boolean remove(int pos) {
        if(pos < 0 || pos >= get_length() || isEmpty()) {
            return false;
        }
        count = this.head;
        int i = 0;
        while(i != pos) {
            i++;
            count = count.next;
        }
        count.next = count.next.next;
        return true;
    }

    /**
     * 得到链表长度
     * @return
     */
    public int get_length() {
        count = head;
        int length = 0;
        while(count.next != null) {
            count = count.next;
            length++;
        }
        return length;
    }

    /**
     * 修改指定位置元素
     * @param pos
     * @param val
     * @return
     */
    public boolean setIndex(int pos,T val){
        if(pos < 0 || pos >= get_length()) {
            return false;
        }
        count = this.head;
        int i = 0;
        while(i != pos) {
            i++;
            count = count.next;
        }
        count.next.data = val;
        return true;
    }

    /**
     * 替换所有指定元素
     * @param x
     * @param val
     * @return
     */
    public boolean setAll(T x,T val) {
        count = this.head;
        while(count.next != null) {
            if (count.next.data == x) {
                count.next.data = val;
            }
            count = count.next;
        }
        return true;
    }

    /**
     * 返回指定元素的下标(默认找到的是第一个)
     * @param val
     * @return
     */
    public int getIndex(T val) {
        count = this.head;
        int i = 0;
        while(count.next != null) {
            if (count.next.data == val) {
                return i;
            }
            i++;
            count = count.next;
        }
        return -1;  //没找到
    }

    /**
     * 返回元素的所有下标
     * @param val
     * @return
     */
    public Integer[] getAll(T val) {
        count = this.head;
        int i = 0;
        boolean cur = false;  //一个标记,判断该链表是否有该元素
        ArrayList<Integer> list = new ArrayList<>();
        while(count.next != null) {
            if (count.next.data == val) {
                cur = true;
                list.add(i);
            }
            i++;
            count = count.next;
        }
        if(cur == true) {
            Integer[] array = (Integer[])list.toArray(new Integer[list.size()]);
            return array;
        }
        return new Integer[] {-1};
    }

    /**
     * 打印链表
     */
    public void show() {
        count = this.head;
        while(count.next != null) {
            count = count.next;
            System.out.print(count.data+"\t");
        }
        System.out.println();
    }


    /**
     * 逆序打印链表
     * @param head
     */
    public void inverseOrderShow(Entry head) {
        if(isEmpty()) {
            return ;
        }
        Entry cur = head.next;
        if(cur != null) {
            inverseOrderShow(cur);
            System.out.print(cur.data+"\t");
        }
    }

    /**
     * 获得链表头节点
     * @return
     */
    public Entry getHead() {
        return this.head;
    }
}

二、单链表的排序、删除重复元素、反转、求倒数第k个元素等

/**
 * @ClassName LinkList1
 * @Description 排序、删除重复元素、反转、求倒数第k个元素等
 * @Author lzq
 * @Date 2018/11/24 14:46
 * @Version 1.0
 **/
public class LinkList1<T> extends LinkList {
    /**
     * 对链表进行排序
     * @return
     */
    public Entry orderList() {
            Entry new_Entry = null;
            double tmp ;
            Entry cur = head.next;
            while(cur != null) {
                new_Entry = cur.next;
                while(new_Entry != null) {
                    if((double)cur.data > (double)new_Entry.data) {
                        tmp = (double)cur.data;
                        cur.data = new_Entry.data;
                        new_Entry.data = tmp;
                    }
                    new_Entry = new_Entry.next;
                }
                cur = cur.next;
            }
            return head;
        }

    /**
     * 删除链表重复元素
     */
    public void deleteDuplecate() {
        if(isEmpty()) {
            return ;
        }
        Hashtable<T,Integer> hashtable = new Hashtable<>();
        Entry cur1 = head.next;
        Entry cur2 = head;
        while(cur1 != null) {
            if(hashtable.containsKey(cur1.data)) {
                cur1 = cur1.next;
            }else {
                hashtable.put((T)cur1.data,1);
                cur2.next = cur1;
                cur2 = cur2.next;
                cur1 = cur1.next;
            }

        }
        cur2.next = cur1;
    }

    /**
     * 反转链表,全部反转
     */
    public void ReverseIterativelyAll() {
        if(isEmpty()) {
            return;
        }
        Entry cur1 = head.next;
        Entry cur2 = head;
        Entry x = null;
        while(cur1 != null) {
            cur2 = cur1;
            cur1 = cur1.next;
            cur2.next = x;
            x = cur2;
        }
        head.next = x;
    }


    /**
     * 反转链表,部分反转  传入参数是下标
     * @param start
     * @param end
     */
    public void ReverseIterativelyPart(int start,int end) {
        if(isEmpty() || start < 0 || end > get_length() || start >= end) {
            return;
        }
        Entry cur1 = head.next;
        Entry cur2 = head;
        Entry x = null;
        Entry m1 = null;
        Entry m2 = null;

        int i = 0;
        while(cur1 != null) {
            if(i == start) {
                break;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
            i++;
        }
        m1 = cur2;
        m2 = cur1;

        while(cur1 != null && i <= end) {
            cur2 = cur1;
            cur1 = cur1.next;
            cur2.next = x;
            x = cur2;
            i++;
        }

        m1.next = cur2;
        m2.next = cur1;
    }

    /**
     * 找到链表的中间节点
     * @return
     */
    public T serachMid() {
        Entry cur1 = head;
        Entry cur2 = head;
        while(cur1 != null && cur1.next != null && cur1.next.next != null) {
            cur1 = cur1.next.next;
            cur2 = cur2.next;
        }
        return (T)cur2.data;
    }

    /**
     * 求链表倒数第k个元素
     * @param k
     * @return
     */
    public T findReciprocalKey(int k) {
        if(k < 0 || k > get_length() || isEmpty()) {
            System.out.print("该下标不合法!");
            return null;
        }
        Entry cur1 = head;
        Entry cur2 = head;
        int i = k-1;
        while(i != 0) {
            i--;
            cur1 = cur1.next;
        }
        while(cur1.next != null) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return (T)cur2.data;
    }

    /**
     * 删除链表倒数第k个节点
     * @param k
     * @return
     */
    public boolean removeLastK(int k) {
        if(k < 0 || k > get_length() || isEmpty()) {
            System.out.print("该下标不合法!");
            return false;
        }
        Entry cur1 = head;
        Entry cur2 = head;
        int i = k;
        while(i != 0) {
            i--;
            cur1 = cur1.next;
        }
        while(cur1.next != null) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        cur2.next = cur2.next.next;
        return true;
    }

    /**
     * 在没有头指针的情况下删除指定结点
     * 1、若待删除结点为尾结点,则无法删除,因为删除后无法使其前驱结点的next指向置为null
     * 2、如若不是尾结点,则可以通过交换这个结点和其后面结点的值,然后删除后继结点
     * @param x
     * @return
     */
    public boolean deleteEntry(Entry x) {
        if(isEmpty() || x == null || x.next == null) {
            return false;
        }
        T tmp = (T)x.data;
        x.data = x.next.data;
        x.next.data = tmp;
        x.next = x.next.next;
        return true;
    }

    /**
     * 逆置单链表
     */
    public Entry inversion() {
        if(isEmpty()) {
            return null;
        }
        Entry p1 = this.head.next;
        Entry p2 ;
        Entry p = new Entry();
        while(p1 != null) {
            p2 = p1.next;
            p1.next = p.next;
            p.next = p1;
            p1 = p2;
        }
        return p;
    }

    /**
     * 带参数的输出函数
     * @param head
     */
    public void show(Entry head) {
        if(isEmpty()) {
            return;
        }
        Entry cur = head;
        while(cur.next != null) {
            cur = cur.next;
            System.out.print(cur.data+"\t");
        }
         System.out.println();
    }

    /**
     * 删除链表的奇数节点
     * @return
     */
    public Entry delete_odd_number() {
        if(isEmpty()) {
            return null;
        }
        Entry p = this.head;
        while(p.next != null){
            if(p.next.next == null) {
                p.next = null;
                break;
            }
            p.next = p.next.next;
            p = p.next;
        }
        return this.head;
    }
}

测试代码:

public class TestDemo3 {
    public static void main(String[] args) {
        LinkList1<Double> list1 = new LinkList1<>();
        list1.tail_insert(5.0);
        list1.tail_insert(15.0);
        list1.tail_insert(25.0);
        list1.tail_insert(25.0);
        list1.tail_insert(45.0);
        list1.tail_insert(15.0);
        list1.show();
        System.out.println("反转链表,全部反转:");
        list1.ReverseIterativelyAll();
        list1.show();
        System.out.println("反转链表,部分反转:1~4号");
        list1.ReverseIterativelyPart(1,4);
        list1.show();
        System.out.println("对链表进行排序:");
        list1.orderList();
        list1.show();
        System.out.println("删除链表重复元素:");
        list1.deleteDuplecate();
        list1.show();
        System.out.println("链表的中间节点:\n"+list1.serachMid());
        System.out.println("链表的倒数第2个元素:\n"+list1.findReciprocalKey(2));
        linkList2.show(linkList2.delete_odd_number());
        linkList2.show(linkList2.inversion());
        
    }
}
5.0	15.0	25.0	25.0	45.0	15.0	
反转链表,全部反转:
15.0	45.0	25.0	25.0	15.0	5.0	
反转链表,部分反转:1~4号
15.0	15.0	25.0	25.0	45.0	5.0	
对链表进行排序:
5.0	15.0	15.0	25.0	25.0	45.0	
删除链表重复元素:
5.0	15.0	25.0	45.0	
链表的中间节点:
15.0
链表的倒数第2个元素:
25.0
15.0     45.0	
45.0     15.0
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值