用泛型写单链表

package study;
/**
 * 单链表
 * @author lzq
 *
 * @param <T>
 */
class Node<T> {//链表类

    class Entry {//节点类
        T data;
        Entry next;
        /**
         * 无参构造函数
         */
        public Entry() {
            this.data = null;
            this.next = null;
        }
        /**
         * 构造函数
         */
        public Entry(T val) {
            this.data = val;
            this.next = null;
        }
    }

    private Entry head = new Entry();//创建头节点
    /**
     * 判空
     * @return  true
     *          false
     */
    public boolean isEmpty() {
        if(head.next == null) {
            return true;
        }
        return false;
    }
    /**
     * 头插法
     * @param val
     */
    public void headinsert(T val) {
        Entry entry  = new Entry(val);
        entry.next = head.next;
        head.next = entry;
    }
    /**
     * 尾插法
     * @param val
     */
    public void lastinsert(T val) {
        Entry entry = new Entry(val);
        Entry cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = entry;
    }
    /**
     * 求链表长度
     * @return  i
     */
    public int getlength() {
        Entry cur = head;
        int i = 0;
        while(cur.next != null) {
            cur = cur.next;
            i++;
        }
        return i;
    }
    /**
     * 指定位置插入元素
     * @param pos
     * @param val
     * @return  true  false
     */
    public boolean insert(int pos,T val) {
        if(pos < 0 || pos > getlength()) {
            return false;
        }
        Entry cur = head;
        int i = 0;
        while(cur.next != null) {
            if(i == pos) {
                break;
            }
            cur = cur.next;
            i++;
        }
        Entry entry = new Entry(val);
        entry.next = cur.next;
        cur.next = entry;
        return true;
    }
    /**
     * 查找元素下标
     * @param val
     * @return  i
     */
    public int find(T val) {
        Entry cur = head.next;
        int i = 0;
        while(cur != null) {
            if(val == cur.data) {
                return i;
            }
            cur = cur.next;
            i++;
        }
        return -1;
    }
    /**
     * 删除元素
     * @param val
     * @return  true false
     */
    public boolean delete(T val) {
        if(isEmpty()) {
            return false;
        }
        Entry cur = head;
        Entry pwd = head.next;
        while(pwd != null) {
            if(pwd.data == val) {
                break;
            }
            cur = cur.next;
            pwd = pwd.next;
        }
        cur.next = pwd.next;
        return true;
    }
    /**
     * 查找倒数第k个元素
     * @param k
     * @return  cur.data
     */
    public T key(int k) {
        if(k < 0 || k > getlength()) {
            return null;
        }
        int i = getlength()-k;
        int j = 0;
        Entry cur = head.next;
        while(cur != null) {
            if(j == i) {
                break;
            }
            cur = cur.next;
            j++;
        }
        return cur.data;
    }
    /*
     * 输出链表
     */
    public void show() {
        Entry cur = head;
        while(cur.next != null) {
            cur = cur.next;
            System.out.print(cur.data+" ");
        }
        System.out.println();
    }
}
public class LianXi {

    public static void main(String[] args) {
        Node<Integer> s = new Node<Integer>();
        s.headinsert(97);
        s.headinsert(56);
        s.headinsert(27);
        s.headinsert(14);
        s.show();
        s.lastinsert(98);
        s.lastinsert(99);
        s.show();
        s.insert(3,85);
        s.show();
        System.out.println(s.getlength());
        System.out.println(s.key(5));
        System.out.println(s.find(56));
        s.delete(27);
        s.show();


        System.out.println("~~~~~~~~~~~~~~~~~~~~");


        Node<String> d = new Node<String>();
        d.headinsert("李正全");
        d.headinsert("代云鹏");
        d.headinsert("郭栋");
        d.headinsert("吴海波");
        d.show();
        d.lastinsert("黄苗杰");
        d.lastinsert("万琳琨");
        d.show();
        d.insert(3,"张航");
        d.show();
        System.out.println(d.getlength());
        System.out.println(d.key(5));
        System.out.println(d.find("黄苗杰"));
        d.delete("吴海波");
        d.show();
    }
}

运行结果:

14 27 56 97 
14 27 56 97 98 99 
14 27 56 85 97 98 99 
7
56
2
14 56 85 97 98 99 
~~~~~~~~~~~~~~~~~~~~
吴海波 郭栋 代云鹏 李正全 
吴海波 郭栋 代云鹏 李正全 黄苗杰 万琳琨 
吴海波 郭栋 代云鹏 张航 李正全 黄苗杰 万琳琨 
7
代云鹏
5
郭栋 代云鹏 张航 李正全 黄苗杰 万琳琨 

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq2899349953/article/details/80536613