0
点赞
收藏
分享

微信扫一扫

针对单链表的重要操作

RIOChing 2022-04-04 阅读 74
数据结构


import org.junit.Test;

import java.util.*;

import static java.lang.System.*;

/**
 * @author ztyOperation
 * @create 2022-04-03-21:33
 */
public class SingleLinkedList {
    public static void main(String[] args) {
        StuList stuList1 = new StuList();
        StuList stuList2 = new StuList();

        stuList1.addNodes();
        stuList2.addNodes();

        stuList1.showList();
        stuList2.showList();

        StuList stuList = StuList.mergeLists(stuList1, stuList2);
        stuList.showList();

    }

    /**
     * 测试方法传递的地址
     */
    @Test
    public void test() {
        StuNode node1 = new StuNode("zty");

        StuNode node2 = node1;

        out.println(node2 == node1);
    }
}

class StuList {
    private Scanner in = new Scanner(System.in);
    private StuNode head = new StuNode("");

    public StuNode getHead() {
        return head;
    }

    /**
     * 将newNode添加入链表(尾插)
     *
     * @param newNode 将newNode添加到链表中
     */
    public void addNode(StuNode newNode) {
        StuNode pMove = head;
        while (pMove.next != null) {
            pMove = pMove.next;
        }

        pMove.next = newNode;
    }

    /**
     * 打印单链表
     */
    public void showList() {
        StuNode pMove;
        for (pMove = head.next; pMove != null; pMove = pMove.next) {
            if (pMove.next != null) out.print(pMove.name + "->");
            else out.println(pMove.name);
        }

    }

    /**
     * 不断添加结点,直到输入"e"
     */
    public void addNodes() {
        String str;
        out.print("请输入姓名: ");
        StuNode temp = new StuNode(str = in.next());
        StuNode pMove = head;
        while (!"e".equals(str)) {//字符串不能用!= ,需要用equals
            pMove.next = temp;
            pMove = temp;
            out.print("请输入姓名: ");
            str = in.next();

            if (str != "exit") temp = new StuNode(str);

        }
    }

    /**
     * 删除结点
     *
     * @param name 删除姓名为name的结点
     */
    public void deleteNode(String name) {
        StuNode pMove;
        if (head.next != null) {
            for (pMove = head; pMove.next != null; pMove = pMove.next) {
                if (pMove.next.name.equals(name)) {
                    if (pMove.next.next != null) pMove.next = pMove.next.next;
                    else pMove.next = null;
                    break;
                }
            }
        } else {
            out.println("链表为空");
        }
    }

    /**
     * 修改结点
     *
     * @param name    查询要修改的节点的姓名
     * @param newNode 将newNode替换原节点
     */
    public void modifyNode(String name, StuNode newNode) {
        StuNode pMove;
        if (head.next != null) {
            for (pMove = head; pMove.next != null; pMove = pMove.next) {
                if (pMove.next.name.equals(name)) {
                    if (pMove.next.next != null) {
                        newNode.next = pMove.next.next;
                        pMove.next = newNode;
                    } else {
                        pMove.next = newNode;
                    }
                    break;
                }
            }
        } else {
            out.println("链表为空");
        }
    }

    /**
     * @param name 要查询结点的姓名
     * @return null:查询不到 StuNode : 返回查询到的结点
     */
    public StuNode findNode(String name) {
        StuNode pMove;
        if (head.next != null) {
            for (pMove = head; pMove != null; pMove = pMove.next) {
                if (pMove.name.equals(name)) {
                    return pMove;
                }
            }
        } else {
            out.println("链表为空");
        }
        return null;
    }


    public void sortByName() {
        ArrayList<String> names = new ArrayList<>();
        StuNode pMove;
        for (pMove = head.next; pMove != null; pMove = pMove.next) {
            names.add(pMove.name);
        }
        String[] namesArr = new String[names.size()];
        names.toArray(namesArr);
        Arrays.sort(namesArr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof String && o2 instanceof String) {
                    return ((String) o1).compareTo((String) o2);
                } else {
                    throw new RuntimeException("输入的数据类型错误");
                }
            }
        });

        addNodes(namesArr, namesArr.length);

    }

    /**
     * @param names  名字们
     * @param number 添加number个结点
     */
    public void addNodes(String[] names, int number) {
        int cnt = 0;
        StuNode pMove = head;
        while (cnt < number) {
            pMove.next = new StuNode(names[cnt++]);
            pMove = pMove.next;
        }

    }

    /**
     * 反转链表(迭代法)
     */
    public void reverseList() {
        if (head.next == null || head.next.next == null) return;
        StuNode pMove = head.next;
        StuNode next;
        StuNode reverseHead = new StuNode("");
        while (pMove != null) {
            next = pMove.next;
            pMove.next = reverseHead.next;
            reverseHead.next = pMove;
            pMove = next;
        }

        head.next = reverseHead.next;

    }

    /**
     * 反转链表(递归法)
     * @param head 该链表的头结点的地址(该方法必须有参数(干脆用链表自己的头结点地址))
     */
    public StuNode reverseList(StuNode head) {
        if (head.next == null) return head;
        StuNode pMove = head.next;
        this.head = reverseList(pMove);
        pMove.next = head;
        head.next = null;
        return this.head;
    }

    /**
     * 合并两个有序链表
     * @param list1 将有序的list1与有序的list2合并
     * @param list2 将有序的list1与有序的list2合并
     * @return 返回有序的StuList
     */
    public static StuList mergeLists(StuList list1, StuList list2){
        StuNode head1 = list1.getHead();
        StuNode head2 = list2.getHead();

        if(head1.next == null && head2.next == null) return list1;
        else if(head1.next != null && head2.next == null ) return list1;
        else if(head1.next == null && head2.next != null) return list2;

        StuNode pMove1 = head1.next;
        StuNode pMove2 = head2.next;
       while(pMove1 != null && pMove2 != null){
            if(pMove1.name.compareTo(pMove2.name)<0){
                pMove1 = pMove1.next;
            }else if(pMove1.name.compareTo(pMove2.name)==0){
                StuNode temp = pMove1.next;
                pMove1.next = pMove2;
                pMove1 = temp;
            }else{
                StuNode temp = pMove2.next;
                pMove2.next = pMove1;
                pMove2 = temp;
            }
        }
        return list1;
    }

}

class StuNode {
    public String name;
    StuNode next;

    public StuNode(String name) {
        this.name = name;

    }

    @Override
    public String toString() {
        return "name : " + name;
    }
}

~~~~

举报

相关推荐

0 条评论