数据结构 -- 链表的学习

数据结构 -- 链表的学习

 

    链表学习目录

      1、创建单链表(创建一个链表头)

      2、添加结点 (在表头insert、在表中insert、在表尾insert)

      3、删除结点(指定的,所有的),删除表头、删除表中、删除表尾

      4、修改结点(指定的,所有的)

      5、查看结点(指定的,所有的)

      6、对单向链表进行反转

      7、单向链表串接的算法

      8、链表与多项式

 

     Node结点类:

public class Node {
    String name;
    int no;
    int score;
    Node next;

    public Node(String name, int no, int score) {
        this.name = name;
        this.no = no;
        this.score = score;
        this.next = null;
    }
}

    

    1、创建单链表

    创建一个带有虚拟头结点的单链表。

/**
 * 创建单链表(创建的是带有虚拟头结点的链表,当然也是可以创建不带有虚拟头结点的链表)
 */

class LinkList {

    private Node first = new Node(null,0,0); // 虚头结点
    private Node last = new Node(null,0,0); //

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

    /**
     * 添加结点
     */
    public void addNode(String name,int no,int score) {
        Node newNode = new Node(name,no,score);
        if (isEmpty()) {
            first.next = newNode;
            last = newNode;
        } else {
            last.next = newNode;
            last = newNode;
        }
    }

    /**
     * 打印节点
     */
    public void printLinkList() {
        Node node = first.next;
        while (node != null) {
            System.out.print(node.name + "\t" + node.no + "\t" + node.score + "\t");
            node = node.next;
        }
        System.out.println();
    }

}

public class Demone1 {
    public static void main(String[] args) {
        LinkList linkList = new LinkList();
        System.out.println(linkList.isEmpty());
        linkList.addNode("huidou1",1,89);
        linkList.addNode("huidou2",2,99);
        linkList.addNode("huidou3",3,98);
        System.out.println(linkList.isEmpty());
        linkList.printLinkList();
    }
}

    结果:

 

    2、添加结点(头插法、中插法、尾插法)

     创建三种结点的插入方式: 

/**
 * 实现三种结点的插入方式(头插入、中插入、尾插入)
 */

class LinkList2 {

    private Node first = new Node(null,0,0); // 虚拟头结点

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

    /**
     * 头插入算法实现
     */
    public void headInsert(Node insertNode) {

        Node node = first.next;
        first.next = insertNode;
        insertNode.next = node;

    }


    /**
     * 中间插入算法实现
     * 中间结点的插入还可以分为:按照指定的值插入或者按照位置插入
     * 在这里我按照指定的值插入,插入到该值的后面
     */
    public void mindumInsert(Node insertNode,String name) {

        Node node = first.next;
        while (node != null) {
            if (node.name.equals(name)) {
                Node temp = node.next;
                node.next = insertNode;
                insertNode.next = temp;
            }
            node = node.next;
        }

    }


    /**
     * 尾插入算法实现
     */
    public void backInsert(Node insertNode) {

        Node node = first.next;
        if (isEmpty()) {
            first.next = insertNode;
        } else {
            while (node != null) {
                if (node.next == null) {
                    node.next = insertNode;
                    insertNode.next = null;
                }
                node = node.next;
            }
        }

    }


    /**
     * 打印链表节点
     */
    public void printLinkList() {
        Node node = first.next;
        while (node != null) {
            System.out.println(node.name);
            System.out.println(node.no);
            System.out.println(node.score);
            node = node.next;
            System.out.println("====================================");
        }
    }
}

public class Demone2 {
    public static void main(String[] args) {

        LinkList2 linkList2 = new LinkList2();
        System.out.println(linkList2.isEmpty());
        System.out.println("头插入算法实现:");
        linkList2.headInsert(new Node("huidou1",12,23));
        linkList2.headInsert(new Node("huidou2",12,23));
        linkList2.headInsert(new Node("huidou3",12,23));
        linkList2.headInsert(new Node("huidou4",12,23));
        System.out.println("打印结果:");
        linkList2.printLinkList();
        System.out.println("中间插入算法实现:");
        linkList2.mindumInsert(new Node("mindumInsert",222,67),"huidou2");
        System.out.println("打印结果:");
        linkList2.printLinkList();
        System.out.println("尾插入算法实现:");
        linkList2.backInsert(new Node("huidou5",20,23));
        linkList2.backInsert(new Node("huidou6",89,45));
        System.out.println("打印结果:");
        linkList2.printLinkList();
    }
}

    实现结果:

true
头插入算法实现:
打印结果:
huidou4
12
23
====================================
huidou3
12
23
====================================
huidou2
12
23
====================================
huidou1
12
23
====================================
中间插入算法实现:
打印结果:
huidou4
12
23
====================================
huidou3
12
23
====================================
huidou2
12
23
====================================
mindumInsert
222
67
====================================
huidou1
12
23
====================================
尾插入算法实现:
打印结果:
huidou4
12
23
====================================
huidou3
12
23
====================================
huidou2
12
23
====================================
mindumInsert
222
67
====================================
huidou1
12
23
====================================
huidou5
20
23
====================================
huidou6
89
45
====================================

Process finished with exit code 0

 

    3、删除结点(头删发、中删法、尾删法)

/**
 * 删除结点(头删除、中间指定删除、尾删除)
 */

class LinkList3 {

    private Node first = new Node(null,0,0); // 虚头结点

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

    /**
     * 使用尾插法先插入几个结点
     */
    public void backInsert(Node inserNode) {

        Node node = first;
        while (node.next != null) {
            node = node.next;
        }
        node.next = inserNode;

    }

    /**
     * 删除第一个非虚拟头结点
     */
    public void deleteFornt() {

        if (isEmpty()) {
            System.out.println("表为空不能删除第一个节点");
            return;
        } else {
            Node node = first.next;
            first.next = node.next;
        }

    }

    /**
     * 删除中间指定的结点
     * 根据指定的名字进行删除
     */
    public void deleteByName(String str) {
        if (isEmpty()) {
            System.out.println("表为空不能删除第指定的节点");
            return;
        } else {
            Node node = first;
            while (node.next != null) {
                if (node.next.name.equals(str)) {
                    Node temp = node.next;
                    node.next = temp.next;
                }
                node = node.next;
            }
        }
    }


    /**
     * 删除最后的结点
     */
    public void deleteBack() {
        Node node = first;
        while (node.next.next != null) {
            node = node.next;
        }
        node.next = null;
    }


    /**
     * 打印链表节点
     */
    public void printLinkList() {
        Node node = first.next;
        while (node != null) {
            System.out.println(node.name);
            System.out.println(node.no);
            System.out.println(node.score);
            node = node.next;
            System.out.println("================================");
        }
    }


}

public class Demone3 {
    public static void main(String[] args) {
        LinkList3 linkList3 = new LinkList3();
        System.out.println(linkList3.isEmpty());
        linkList3.backInsert(new Node("huidou1",23,15));
        linkList3.backInsert(new Node("huidou2",33,235));
        linkList3.backInsert(new Node("huidou3",45,345));
        linkList3.backInsert(new Node("huidou4",79,25));
        linkList3.backInsert(new Node("huidou5",79,25));
        linkList3.backInsert(new Node("huidou6",79,25));
        linkList3.printLinkList();
        System.out.println("删除最后一个结点的结果为:");
        linkList3.deleteBack();
        linkList3.printLinkList();
        System.out.println("删除第一个结点的结果为:");
        linkList3.deleteFornt();
        linkList3.printLinkList();
        System.out.println("删除指定的结点的结果为:");
        linkList3.deleteByName("huidou4");
        linkList3.printLinkList();
    }
}

    算法结果:

true
huidou1
23
15
================================
huidou2
33
235
================================
huidou3
45
345
================================
huidou4
79
25
================================
huidou5
79
25
================================
huidou6
79
25
================================
删除最后一个结点的结果为:
huidou1
23
15
================================
huidou2
33
235
================================
huidou3
45
345
================================
huidou4
79
25
================================
huidou5
79
25
================================
删除第一个结点的结果为:
huidou2
33
235
================================
huidou3
45
345
================================
huidou4
79
25
================================
huidou5
79
25
================================
删除指定的结点的结果为:
huidou2
33
235
================================
huidou3
45
345
================================
huidou5
79
25
================================

Process finished with exit code 0

 

    4、修改结点(修改指定的名字进行修改)

 

/**
 * 修改结点的算法(根据指定的名字修改)
 */

class LinkList5 {

    private Node first = new Node(null,0,0);//虚拟头结点

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

    /**
     * 尾插法先插入几个结点
     */
    public void backInsert(Node insertNode) {

        Node node = first;
        while (node.next != null) {
            node = node.next;
        }
        node.next = insertNode;

    }

    /**
     * 修改结点信息
     */

    public void updateNode(String name) {

        Node node = first.next;
        while (node != null) {
            if (node.name.equals(name)) {
                node.name = "修改了";
            }
            node = node.next;
        }

    }

    /**
     * 打印所有节点
     */
    public void print() {

        Node node = first.next;
        while (node != null) {
            System.out.println(node.name);
            System.out.println(node.no);
            System.out.println(node.score);
            node = node.next;
            System.out.println("=============================");
        }
    }

}
public class Demone4 {
    public static void main(String[] args) {
        LinkList5 linkList5 = new LinkList5();
        System.out.println(linkList5.isEmpty());
        linkList5.backInsert(new Node("huidou1",23,23));
        linkList5.backInsert(new Node("huidou2",23,23));
        linkList5.backInsert(new Node("huidou3",23,23));
        linkList5.print();
        System.out.println("输出修改后的信息:");
        linkList5.updateNode("huidou2");
        linkList5.print();
    }
}

    算法结果:

true
huidou1
23
23
=============================
huidou2
23
23
=============================
huidou3
23
23
=============================
输出修改后的信息:
huidou1
23
23
=============================
修改了
23
23
=============================
huidou3
23
23
=============================

Process finished with exit code 0

 

    5、查看结点 (查看所有的结点,这个在前面的例子已经体现了,就光了解一下,输出函数)

    /**
     * 打印链表节点
     */
    public void printLinkList() {
        Node node = first.next;
        while (node != null) {
            System.out.println(node.name);
            System.out.println(node.no);
            System.out.println(node.score);
            node = node.next;
            System.out.println("================================");
        }
    }

 

    6、对单向链表进行反转

 

 

 

    7、单向链表串接的算法

      链表的拼接,就是将两个单向链表合并成一个单向链表。

      

    /**
     * 合并单链表
     */
    public void merge(LinkList5 linkList5) {

        Node node = first;
        while (node.next != null) {
            node = node.next;
        }
        node.next = linkList5.getFirst().next;

    }
public class Demone5 {
    public static void main(String[] args) {

        LinkList2 linkList2 = new LinkList2();
        linkList2.backInsert(new Node("huidou1",23,45));
        linkList2.backInsert(new Node("huidou2",23,45));
        linkList2.backInsert(new Node("huidou3",23,45));

        LinkList5 linkList5 = new LinkList5();
        linkList5.backInsert(new Node("huidou4",234,56));
        linkList5.backInsert(new Node("huidou5",234,56));
        linkList5.backInsert(new Node("huidou6",234,56));

        System.out.println("合并:");
        linkList2.merge(linkList5);
        linkList2.printLinkList();
    }
}

    算法结果:

合并:
huidou1
23
45
====================================
huidou2
23
45
====================================
huidou3
23
45
====================================
huidou4
234
56
====================================
huidou5
234
56
====================================
huidou6
234
56
====================================

Process finished with exit code 0

 

    8、链表与多项式

 

posted @ 2021-10-27 15:38  心向未来  Views(51)  Comments(0)    收藏  举报