数据结构与算法--链表

链表

    • 1、单向链表
    • 2、双向链表
    • 3、环形链表

介绍

  • 链表是有序的列表,它在内存中的存储如下
    数据结构与算法--链表_第1张图片
  1. 链表是以节点的方式来存储的,是链式存储
  2. 每个节点包含data域,next域:指向下一个节点
  3. 链表的各个节点不一定是连续存储

1、单向链表

实际应用

  • 使用带head头节点的单向链表实现–水浒英雄排行榜管理。
  • 需要实现的方法
    • add:添加到英雄到链表尾
    • addHead:添加英雄到head之后
    • addByOrder:按照序号添加
    • del:删除指定序号的英雄
    • findLastIndexNode:查找链表倒数第k个节点
    • getHead:返回头节点
    • list:打印链表
    • reverseList:反转链表
    • reversePrint:逆序打印
    • size:得到链表的长度(不包括头节点)
    • update:根据序号找到要修改的英雄节点进行修改
    • mergeList:合并两个有序链表为一个有序的链表

代码

  • 英雄类设计
class HeroNode{
    public int no;//英雄编号
    public String name;//英雄名称
    public String nickname;//英雄称号
    public HeroNode next; //指向下一个英雄
    //构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
    //重写toString方法,方便输出
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
  • 单向链表类设计

    • 属性
    class SingleList{
        //这里直接初始化了头节点
        private HeroNode head = new HeroNode(0,"",""); 
    }
    
    • add:添加到英雄到链表尾
        //添加英雄到链表尾
        public void add(HeroNode node){
            HeroNode cur = head;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    
    • addHead:添加英雄到head之后
        //添加英雄到head后
        public void addHead(HeroNode node){
            //判断一下传入节点是否为空
            if(node == null){
                System.out.println("传入节点为空");
                return;
            }
            node.next = head.next;
            head.next = node;
        }
    
    • addByOrder:按照序号添加
        //按照序号添加
        public void addByOrder(HeroNode node){
            if(node == null){
                System.out.println("传入节点为空");
                return;
            }
            HeroNode cur = head;
            while(cur.next != null){
                if(cur.next.no > node.no){
                    node.next = cur.next;
                    cur.next = node;
                    return;
                }else if(cur.next.no == node.no){
                    System.out.println("编号已存在,不能添加");
                    return;
                }else {
                    cur = cur.next;
                }
            }
        }
    
    • del:删除指定序号的英雄
        //删除指定序号的节点
        public void del(int no){
            if(no < 1){
                System.out.println("输入编号不合理");
                return;
            }
            HeroNode cur = head;
            while(cur.next != null){
                if(cur.next.no == no){
                    cur.next = cur.next.next;
                    return;
                }else {
                    cur = cur.next;
                }
            }
            System.out.println("链表中没有这个编号");
        }
    
    • findLastIndexNode:查找链表倒数第k个节点
        //查找链表倒数第k个节点
        public HeroNode findLastIndexNode(HeroNode head,int k){
            HeroNode cur = head;
            int size = size(head);
            if(k <= 0 || k > size){ //校验k是否合理
                return null;
            }
            for (int i = 0; i < size - k; i++) {
                cur = cur.next;
            }
            return cur.next;
        }
    
    • getHead:返回头节点
        //返回头节点
        public HeroNode getHead() {
            return head;
        }
    
    • list:打印链表
        //打印链表
        public void list(){
            if(head.next == null){
                System.out.println("链表为空");
                return;
            }
            HeroNode temp = head.next;
            while (temp != null){
                System.out.println(temp);
                temp = temp.next;
            }
        }
    
    • reverseList:反转链表(方法多样)
        //反转链表
        public void reverseList(){
            HeroNode pre = null;
            HeroNode cur = head.next;
            HeroNode next = null;
            while (cur != null){
                next = cur.next;
                cur.next = pre;
                pre = cur;
                cur = next;
            }
            head.next = pre;
        }
    
    • reversePrint:逆序打印
        //逆序打印
        public void reversePrint(){
            Stack<HeroNode> stack = new Stack<>();
            HeroNode temp = head.next;
            while(temp != null){
                stack.push(temp);
                temp = temp.next;
            }
            while (!stack.isEmpty()){
                System.out.println(stack.pop());
            }
        }
    
    • size:得到链表的长度(不包括头节点)
        //得到链表的长度
        public int size(HeroNode head){
            HeroNode cur = head;
            int size = 0;
            while(cur.next != null){
                cur = cur.next;
                size++;
            }
            return size;
        }
    
    • update:根据序号找到要修改的英雄节点进行修改
        //根据序号找到要修改的英雄节点进行修改
        public void update(HeroNode node){
            HeroNode cur = head;
            while (cur.next != null){
                if(cur.next.no == node.no){
                    cur.next.name = node.name;
                    cur.next.nickname = node.nickname;
                    return;
                }
                cur = cur.next;
            }
            System.out.println("更新失败");
        }
    
    • mergeList:合并两个有序链表为一个有序的链表
    class Solution {
        public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
               ListNode newHead = new ListNode(-1);
               ListNode tmp = newHead;
               while(list1 != null && list2 != null){
                   if(list1.val < list2.val){
                        tmp.next = list1;
                        list1 = list1.next;
                        tmp = tmp.next;   
                    }else{
                        tmp.next = list2;
                        list2 = list2.next;
                        tmp = tmp.next;
                    }
               }
               if(list1 != null){
                   tmp.next = list1;
               }
     
               if(list2 != null){
                   tmp.next = list2;
               }
               return newHead.next;
        }
    }
    
  • 测试代码

    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode hero5 = new HeroNode(5, "a?", "buzhidao");

        SingleList singleLinkedList = new SingleList();
        singleLinkedList.add(hero1);
        singleLinkedList.add(hero5);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero3);
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.list();
        System.out.println("删除3后");
        singleLinkedList.del(3);
        singleLinkedList.list();
        System.out.println("反向输出");
        singleLinkedList.reversePrint();
        System.out.println("反转");
        singleLinkedList.reverseList();
        singleLinkedList.list();
        System.out.println("查找倒数第二个节点");
        System.out.println(singleLinkedList.findLastIndexNode(singleLinkedList.getHead(),2));
        System.out.println("更新1号节点");
        HeroNode hero6 = new HeroNode(1, "shua", "wocao");
        singleLinkedList.update(hero6);
        singleLinkedList.list();
    }

2、双向链表

和单向链表类似,只不过多一个前置节点

  • 代码
public class doubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode hero5 = new HeroNode(5, "a?", "buzhidao");
        doubleLinkedList doubleLinkedList = new doubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.add(hero5);
        doubleLinkedList.list();
        System.out.println("删除5");
        doubleLinkedList.del(5);
        doubleLinkedList.list();
        System.out.println("修改3");
        doubleLinkedList.update(new HeroNode(3,"wocao","ss"));
        doubleLinkedList.list();
    }
}
//双向链表类
class doubleLinkedList{
    private HeroNode head = new HeroNode(0,"","");
    //遍历
    public void list(){
        HeroNode temp = head;
        while(temp.next != null){
            System.out.println(temp.next);
            temp = temp.next;
        }
    }
    //添加
    public void add(HeroNode heroNode){
        HeroNode temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }
    //修改
    //更新节点
    public void update(HeroNode newHeroNode){
        HeroNode cur = head;
        while(cur.next != null){
            if(cur.next.no == newHeroNode.no){
                cur.next.name = newHeroNode.name;
                cur.next.nickname = newHeroNode.nickname;
                return;
            }
            cur = cur.next;
        }
        System.out.println("更新失败");
    }
    //删除
    public void del(int no){
        HeroNode temp = head.next;
        while (temp != null){
            if(temp.no == no){
                temp.pre.next = temp.next;
                if(temp.next != null){
                    temp.next.pre = temp.pre;
                }
                return;
            }
            temp = temp.next;
        }
        System.out.println("删除失败");
    }
}
//英雄类
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode pre;
    public HeroNode next;

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

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

3、环形链表

应用:约瑟夫问题

  • Josephu 问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
  • 用一个不带头结点的循环链表来处理Josephu 问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。

创建环形链表思路

  • 构建一个单向的环形链表
    • 先创建第一个节点,让first指向该节点,并形成队列
    • 后面每创建一个新的节点,就把该节点,加入到已有的环形链表中即可。
  • 遍历环形链表
    • 先让一个辅助变量curBoy,指向first节点
    • 然后通过while循环遍历该环形链表,当curBoy.next == first结束

出圈思路

  • 从第startNo开始数,数countNum次,最初有nums孩子在圈中
  • 创建辅助指针helper指向最后一个孩子
  • 报数前让first和helper移动 startNo-1次 让first指向数数的孩子
  • 报数后,让first和helper同时移动m-1次,first指向出圈孩子
    • first = first.getNext
    • helper.setNext(first);

代码实现

package linkedList.circleLinkedList;

public class circleLinkedListDemo{
    public static void main(String[] args) {
        CircleLinkedList circleLinkedList = new CircleLinkedList();
        circleLinkedList.addBoy(5);
        circleLinkedList.countBoy(1,2,5);
    }
}
//环形链表
class CircleLinkedList{
    private Boy first = null;

    public void addBoy(int nums){
        if(nums < 1){
            System.out.println("输入的值不对");
        }
        Boy curBoy = first;
        for (int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if(i == 1){
                first = boy;
                first.setNext(first);
                curBoy = first;
            }else {
                curBoy.setNext(boy);
                curBoy = curBoy.getNext();
                curBoy.setNext(first);
            }
        }
    }
    //遍历环形链表
    public void showBoy(){
        if(first == null){
            System.out.println("链表为空");
            return;
        }
        Boy curBoy = first;
        while(true){
            System.out.println("小孩编号:"+curBoy.getNo());
            if(curBoy.getNext() == first){
                break;
            }
            curBoy = curBoy.getNext();
        }
    }
    public void countBoy(int startNo,int countNum,int nums){
        Boy helper = first;
        while (helper.getNext() != first){
            helper = helper.getNext();
        }
        for (int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        while (true){
            if(helper == first){
                //圈中只剩下一个小孩
                break;
            }
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println("出圈小孩为:"+first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.println("剩下的小孩为:"+first.getNo());
    }
}
//孩子类
class Boy{
    private int no;
    private Boy next;

    public Boy(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}

你可能感兴趣的:(数据结构与算法,链表,数据结构)