java 单链表 练习

练习一下java单链表的简单习题

package com.test1;

import java.util.Stack;

public class SingleListDemo {
    
    /**
     * 返回单链表的总长度
     * @param sl
     * @return
     */
    public static <T> int getListLength(SingleList<T> sl) {
        Node<T> temp = sl.headNode.next;
        int index = 0;
        while(temp != null) {
            temp = temp.next;
            index++;
        }
        return index;
    }
    
    /**
     * 查找单链表中倒数第k个元素
     * @param sl
     * @param k
     * @return
     */
    public static <T> Node<T> getLastK(SingleList<T> sl, int k){
        int length = getListLength(sl);
        if(length < k) {
            throw new RuntimeException("不存在倒数第" + k + "个节点");
        }
        int index = length - k;
        Node<T> temp = sl.headNode.next;
        while(index != 0) {
            temp = temp.next;
            index--;
        }
        return temp;
    }
    
    /**
     * 单链表的反转
     * @param <T>
     * @param args
     */
    public static <T> void reverseList(SingleList<T> sl){
        int length = getListLength(sl);
        if(length == 1) {
            return;
        }
        
        Node<T> headNode = sl.headNode;
        Node<T> last =  headNode.next;
        Node<T> cur = last.next;
        Node<T> next = cur.next;
        last.next = null;
        while(next != null) {
            cur.next = last;
            last = cur;
            cur = next;
            next = next.next;
        }
        cur.next = last;
        sl.headNode.next = cur;
    }
    
    /**
     * 从尾到头打印单链表 (利用栈)
     * @param <T>
     * @param args
     */
    public static <T> void printReverseList1(SingleList<T> sl) {
        Node<T> temp = sl.headNode.next;
        Stack<T> stack = new Stack<>();
        while(temp != null) {
            stack.push(temp.data);
            temp = temp.next;
        }
        while(!stack.empty()) {
            System.out.println(stack.pop());
        }
    }
    
    /**
     * 从尾到头打印单链表 (利用递归) 很是牛逼!
     * @param <T>
     * @param args
     */
    public static <T> void printReverseList2(Node<T> node) {
        if(node.next != null) {
            printReverseList2(node.next);
        }
        System.out.println(node.data);
    }
    
    /**
     * 合并两个有序的单链表,合并之后的单链表任然有序
     * @param <T>
     * @param args
     */
    public static <T> SingleList<T> mergeTwoList(SingleList<T> sl1, SingleList<T> sl2) {
        SingleList<T> result = new SingleList<>();
        Node<T> temp1 = sl1.headNode.next;
        Node<T> temp2 = sl2.headNode.next;
        while(temp1 != null && temp2 != null) {
            if(temp1.compareTo(temp2) < 0) {
                Node<T> temp = temp1;
                temp1 = temp1.next;
                temp.next = null;
                result.addNode(temp);
            } else {
                Node<T> temp = temp2;
                temp2 = temp2.next;
                temp.next = null;
                result.addNode(temp);
            }
        }
        Node<T> temp = temp1 == null ? temp2 : temp1;
        while(temp != null) {
            result.addNode(temp);
            temp = temp.next;
        }
        return result;
    }
    
    public static void main(String[] args) {
//      SingleList<String> sl = new SingleList<>();
//      sl.addNode(new Node<>("李四", null));
//      sl.addNode(new Node<>("张三", null));
//      sl.addNode(new Node<>("王五", null));
//      sl.addNode(new Node<>("赵六", null));
//      sl.showData();
//      System.out.println(getListLength(sl));
//      Node<String> node = getLastK(sl, 2);
//      System.out.println(node);
//      reverseList(sl);
//      System.out.println("\n反转之后为:");
//      sl.showData();
//      System.out.println("\n从尾到头打印单链表");
//      printReverseList2(sl.headNode.next);
//      Node<Integer> a = new Node<>(1, null);
//      Node<Integer> b = new Node<>(2, null);
//      System.out.println(a.compareTo(b));
        
        SingleList<Integer> sl = new SingleList<>();
        sl.addNodeOrder(new Node<>(1, null));
        sl.addNodeOrder(new Node<>(3, null));
        sl.addNodeOrder(new Node<>(70, null));
        sl.addNodeOrder(new Node<>(2, null));
        sl.showData();
        SingleList<Integer> sl1 = new SingleList<>();
        sl1.addNodeOrder(new Node<>(11, null));
        sl1.addNodeOrder(new Node<>(13, null));
        sl1.addNodeOrder(new Node<>(17, null));
        sl1.addNodeOrder(new Node<>(12, null));
        sl1.showData();
        System.out.println("\n合并之后为:");
        SingleList<Integer> result = mergeTwoList(sl, sl1);
        result.showData();
    }
}

class Node<T> implements Comparable<Node<T>>{
    public T data;
    public Node<T> next;
    public Node(T data, Node<T> next) {
        super();
        this.data = data;
        this.next = next;
    }
    @Override
    public String toString() {
        return this.data.toString();
    }
    
    
    @SuppressWarnings("unchecked")
    @Override
    public int compareTo(Node<T> o) {
        if(o.data instanceof Comparable) {
            return ((Comparable<Comparable<?>>) this.data).compareTo((Comparable<?>) o.data);
        }
        return 0;
    }
    
}

class SingleList<T>{
    public Node<T> headNode = new Node<>(null, null);
    
    /**
     * 往尾部添加节点
     * @param node
     */
    public void addNode(Node<T> node) {
        Node<T> temp = headNode;
        while(temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
    }
    
    /**
     * 按照节点的大小添加节点,使得链表有序
     */
    public void addNodeOrder(Node<T> node) {
        Node<T> last = headNode;
        Node<T> temp = last.next;
        while(temp != null && temp.compareTo(node) < 0) {
            last = temp;
            temp = temp.next;
        }
        if(temp != null && temp.compareTo(node) == 0) {
            throw new RuntimeException("已经存在相同的节点,不允许再次添加");
        }
        last.next = node;
        node.next = temp;
    }
    
    /**
     * 显示list中的数据
     */
    public void showData() {
        Node<T> temp = headNode.next;
        while(temp != null) {
            System.out.print(temp + " ");
            temp = temp.next;
        }
    }
    
    
}

转载于:https://www.cnblogs.com/theone67/p/11454178.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值