根据单链表的定义,用语句实现两个有序链表的合并操作

1,结点类为学生

2,两个链表代表两个班级的同学,假设每个班级有5名同学

1:

package lianxi4;

public class PersonNode {
   public int num;//编号
    public String name;//名称
    public String aliasName;//别名
    public PersonNode next;//指向下一个节点

    //构造器
    public PersonNode(int num, String name, String aliasName) {
        this.num = num;
        this.name = name;
        this.aliasName = aliasName;
    }
    public PersonNode() {

    }

    //重写toString方法
    @Override
    public String toString() {
        return "PersonNode{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", aliasName='" + aliasName + '\'' +
                '}';
    }
}

2:方法实现:

package lianxi4;

public class SingleLinkedList {
   private PersonNode head;

    public SingleLinkedList(PersonNode head) {
        this.head = head;
    }
    public SingleLinkedList() {
        head = new PersonNode();
    }

    //在链表的指定位置添加节点数据
    public void addByNum(PersonNode personNode) {
        PersonNode temp = head;
        boolean flag = false;//插入的编号是否存在,默认不存在
        while (true) {
            if (temp.next == null) {//已经在链表的尾部
                break;
            }
            if (temp.next.num > personNode.num) {//找到位置,在temp后添加
                break;
            }
            if (temp.next.num == personNode.num) {//编号已经存在,不能添加
                flag = true;
                break;
            }
            temp = temp.next;//后移,遍历
        }
        if (flag) {
            System.out.printf("添加的人员编号%d已经存在,不能添加\n", personNode.num);
        } else {
            //添加数据到链表中,temp后的位置
            personNode.next = temp.next;
            temp.next = personNode;
        }

    }

    //显示链表
    public void show() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为头节点不能动,所以需要一个临时变量来遍历
        PersonNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp向后移动
            temp = temp.next;
        }
    }

    /**
     * @Description: 合并两个单链表,合并之后的链表依然有序
     * @Param: head1  head2
     * @Author: xz
     * @return: PersonNode
     * @Date: 2020/7/21 15:50
     */
    public static SingleLinkedList mergeLinkedList(SingleLinkedList list1, SingleLinkedList list2) {
        // 判断需要合并的链表是否为空
        if (list1.head.next == null && list2.head.next == null) {
            throw new RuntimeException("需要合并的两条链表都为空!");
        }
        // 一条链表为空,直接返回另一条链表
        if (list1.head.next == null) {
            System.out.println(list2);
        }
        if (list2.head.next == null) {
            System.out.println(list1);
        }
        //定义合并之后的新链表的头节点
        PersonNode newHead = new PersonNode();
        PersonNode current = newHead; // 定义一个current结点指向新链表
        //定义临时变量
        PersonNode temp1 = list1.head.next;
        PersonNode temp2 = list2.head.next;
        while (temp1 != null && temp2 != null) {
            if (temp1.num < temp2.num) {
                current.next = temp1;
                // 新链表中,current指针的下一个结点对应较小的那个数据
                temp1 = temp1.next;//temp1指针后移
                current = current.next; // current指针后移
            } else {
                current.next = temp2;
                temp2 = temp2.next;
                current = current.next;
            }
        }
        // 合并剩余的元素
        if (temp1 == null) {
            // 说明链表1遍历完了,是空的
            current.next = temp2;
        }
        if (temp2 == null) {
            // 说明链表2遍历完了,是空的
            current.next = temp1;
        }
       return new SingleLinkedList(newHead);
    }
}

3:测试类:

package lianxi4;

public class SingleLinkedListTest {
   public static void main(String[] args) {
      // 创建节点
      PersonNode personNode1 = new PersonNode(1, "小王", "小立");
      PersonNode personNode2 = new PersonNode(2, "小俊", "小蓝");
      PersonNode personNode3 = new PersonNode(3, "小柳", "小英");
      PersonNode personNode4 = new PersonNode(4, "小猪", "小猫");
      
      // 添加链表节点
      SingleLinkedList list1 = new SingleLinkedList();
      list1.addByNum(personNode1);
      list1.addByNum(personNode4);
      System.out.println("添加的第一个链表节点数据如下:============");
      // 查询链表所有节点
      list1.show();

      // 添加链表节点
      SingleLinkedList list2 = new SingleLinkedList();
      list2.addByNum(personNode2);
      list2.addByNum(personNode3);
      System.out.println("添加的第二个链表节点数据如下:============");
      // 查询链表所有节点
      list2.show();

      System.out.println("合并后的新链表的节点数据如下:============");
      SingleLinkedList list = SingleLinkedList.mergeLinkedList(list1, list2);
      // 查询合并后的链表所有节点
      list.show();
   }
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值