2.单链表

该部分包含以下内容
-单链表的增删改查
-计算链表长度
-逆序链表
-寻找(删除)链表倒数第K个元素
-逆序打印链表(使用栈)


import java.util.Stack;

import javax.swing.text.AbstractDocument.BranchElement;

public class SingleLinkedListDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 先创建节点
        HeroNode hero1 = new HeroNode(1, "song", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");

        // 加入节点
        // 创建链表
        Singlelinkedlist singlelinkedlist = new Singlelinkedlist();
        singlelinkedlist.addByOrder(hero1);
        singlelinkedlist.addByOrder(hero4);
        singlelinkedlist.addByOrder(hero3);
        singlelinkedlist.addByOrder(hero2);
        // singlelinkedlist.addByOrder(hero3);
        singlelinkedlist.showList();
        HeroNode newheroNode = new HeroNode(2, "小路", "玉麒麟");
        singlelinkedlist.update(newheroNode);
        // singlelinkedlist.showList();

        // singlelinkedlist.deletenode(4);
        System.out.println();
        // singlelinkedlist.showList();

//      System.out.println(getLength(singlelinkedlist.getHead()));
//      System.out.println(findLastIndexNode(singlelinkedlist.getHead(), 1));
        // 逆序链表
        // reverseLinkList(singlelinkedlist.getHead());
        // 逆序打印
        System.out.println("********************");
        reverseprintList(singlelinkedlist.getHead());
        System.out.println();
        singlelinkedlist.showList();

    }

    // 获取单链表的个数
    public static int getLength(HeroNode head) {
        if (head.next == null) {
            return 0;
        }
        int length = 0;
        HeroNode curHeroNode = head.next;
        while (curHeroNode != null) {
            length++;
            curHeroNode = curHeroNode.next;
        }
        return length;

    }

    // 查找单链表的倒数第k个节点
    // 接收head节点,接收k 的index
    // 先把链表从头到尾遍历一下,得到链表的长度
    // 再遍历size-index个节点
    public static HeroNode findLastIndexNode(HeroNode head, int index) {
        if (head.next == null) {
            return null;
        }
        // 获得链表长度
        int size = getLength(head);
        // 检验index是否合法

        if (index <= 0 || index > size) {
            System.out.println("index不合法");
            return null;
        }

        HeroNode curHeroNode = head.next;
        for (int i = 1; i <= size - index; i++) {
            // 走几步到倒数第K个节点
            curHeroNode = curHeroNode.next;
        }
        return curHeroNode;

    }

    // 单链表反转
    // 第一种方法:建立一个新链表,然后使用头插法建立链表
    public static void reverseLinkList(HeroNode head) {
        if (head.next == null) {
            return;
        }
        HeroNode reverseHead = new HeroNode(0, "", "");
        HeroNode curHeroNode = head.next;
        HeroNode nextHeroNode = null;
        while (curHeroNode != null) {
            // HeroNode tmpHeroNode=curHeroNode;
            nextHeroNode = curHeroNode.next;
            curHeroNode.next = reverseHead.next;
            reverseHead.next = curHeroNode;
            curHeroNode = nextHeroNode;
        }
        head.next = reverseHead.next;
    }

    public static void showreverseList(HeroNode head) {
        // 判断链表是否为空
        if (head.next == null) {
            return;
        }

        HeroNode tempHeroNode = head.next;
        while (true) {
            // 判断是否已经到达最后一个节点
            if (tempHeroNode == null) {
                break;
            }
            // 打印
            System.out.println(tempHeroNode);
            //
            tempHeroNode = tempHeroNode.next;

        }

    }
    // 逆序打印

    public static void reverseprintList(HeroNode head) {
        if (head.next == null) {
            return;
        }

        // 创建一个栈,不需要自己写
        Stack stack = new Stack();
        HeroNode curHeroNode = head.next;
        while (curHeroNode != null) {
            stack.push(curHeroNode);
            curHeroNode = curHeroNode.next;
        }

        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }

    }

}

// 定义linklist
class Singlelinkedlist {
    // 初始化头节点
    private HeroNode head = new HeroNode(0, "", "");

    // 添加节点到单向链表
    // 1.找到当前链表的最后节点
    // 2.将最后的next域指向最后节点
    public void addNode(HeroNode heronode) {
        HeroNode tempHeroNode = head;
        while (true) {
            // 获取最后的节点
            if (tempHeroNode.next == null) {
                break;
            }
            tempHeroNode = tempHeroNode.next;

        }
        // 将最后节点的next
        tempHeroNode.next = heronode;
    }

    // 按顺序添加节点
    public void addByOrder(HeroNode heronode) {
        HeroNode tmpHeroNode = head;
        boolean flag = false;
        while (true) {
            // 如果是最后一个节点,证明需要插入到最后,直接退出
            if (tmpHeroNode.next == null) {
                break;
            }
            // 找到插入位置
            if (tmpHeroNode.next.no > heronode.no) {
                break;
            } else if (tmpHeroNode.next.no == heronode.no) {
                flag = true;
                break;
            }
            tmpHeroNode = tmpHeroNode.next;
        }
        // 判断是否能够插入
        if (flag) {
            System.out.println("待插入节点已存在");

        } else {
            heronode.next = tmpHeroNode.next;
            tmpHeroNode.next = heronode;
        }

    }

    // 修改节点信息,编号不能变
    // 根据节点编号修改
    public void update(HeroNode newheroNode) {
        // 判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode tmpHeroNode = head.next;
        boolean flag = false;
        while (true) {
            if (tmpHeroNode == null) {
                break;
            }
            if (tmpHeroNode.no == newheroNode.no) {
                flag = true;
                break;
            }
            tmpHeroNode = tmpHeroNode.next;
        }

        if (flag) {
            tmpHeroNode.name = newheroNode.name;
            tmpHeroNode.nickname = newheroNode.nickname;

        } else {
            System.out.printf("编号为%d的节点不存在\n", newheroNode.no);
        }

    }

    public void showList() {
        // 判断链表是否为空
        if (head.next == null) {
            return;
        }

        HeroNode tempHeroNode = head.next;
        while (true) {
            // 判断是否已经到达最后一个节点
            if (tempHeroNode == null) {
                break;
            }
            // 打印
            System.out.println(tempHeroNode);
            //
            tempHeroNode = tempHeroNode.next;

        }

    }

    // 单链表的删除
    public void deletenode(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        // 找到需要删除节点的前一个节点。
        HeroNode tmpHeroNode = head;
        boolean flag = false;
        while (true) {
            if (tmpHeroNode.next == null) {
                break;
            }

            if (tmpHeroNode.next.no == no) {
                flag = true;
                break;
            }
            tmpHeroNode = tmpHeroNode.next;

        }

        if (flag) {
            tmpHeroNode.next = tmpHeroNode.next.next;

        } else {
            System.out.printf("编号为%d的节点不存在,无法删除\n", no);
        }

    }

    // 获取单链表的个数,不包括头结点
    /**
     * 
     * @return 返回有效节点个数
     */

    // 返回头结点
    public HeroNode getHead() {
        return head;
    }

}

//定义单个节点
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        super();
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
    }

}

你可能感兴趣的:(2.单链表)