单链表的增删改查合并
#最近开始学习数据结构,尚学堂韩老师的,只是看还是不行的,得动手码代码才行,其他的就不多说了,直接上代码吧(代码是在韩老师的基础上加了一些自己的想法)
先创建模拟节点,就是简单的实体类
package com.example.demo.arrayList;
public class HeroNode {
private int no;
private String name;
private String nickName;
private 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 + '\'' +
'}';
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public HeroNode getNext() {
return next;
}
public void setNext(HeroNode next) {
this.next = next;
}
}
然后就是链表模拟和测试代码
package com.example.demo.arrayList;
public class ArrayListTest {
public static void main(String[] args) {
ArrayListDemo arrayListDemo = new ArrayListDemo();
HeroNode heroNode1 = new HeroNode(1,"小宋","及时雨");
HeroNode heroNode3 = new HeroNode(3,"小卢","玉麒麟");
HeroNode heroNode5 = new HeroNode(5,"小吴","智多星");
HeroNode heroNode7 = new HeroNode(7,"小林","豹子头");
HeroNode heroNode9 = new HeroNode(9,"小花","小李广");
ArrayListDemo demo = new ArrayListDemo();
HeroNode xiyou2 = new HeroNode(2,"唐僧","师父");
HeroNode xiyou4 = new HeroNode(4,"悟空","大师兄");
HeroNode xiyou6 = new HeroNode(6,"悟能","二师兄");
HeroNode xiyou8 = new HeroNode(8,"悟净","沙师弟");
HeroNode xiyou10 = new HeroNode(10,"三太子","白龙马");
HeroNode xiyou12 = new HeroNode(12,"女儿国王","美女");
arrayListDemo.add(heroNode1);
arrayListDemo.add(heroNode5);
arrayListDemo.add(heroNode3);
arrayListDemo.add(heroNode9);
arrayListDemo.add(heroNode7);
System.out.println("链表1");
ArrayListDemo.list(arrayListDemo.getHead());
demo.add(xiyou2);
demo.add(xiyou6);
demo.add(xiyou4);
demo.add(xiyou8);
demo.add(xiyou10);
demo.add(xiyou12);
System.out.println("链表2");
ArrayListDemo.list(demo.getHead());
ArrayListDemo.merge(arrayListDemo,demo);
System.out.println("合并后");
ArrayListDemo.list(arrayListDemo.getHead());
System.out.println("反转。。。。。。");
ArrayListDemo.reverse(arrayListDemo.getHead(),arrayListDemo);
ArrayListDemo.list(arrayListDemo.getHead());
System.out.println("取出节点,并从链表中删除该节点");
HeroNode heroNode = ArrayListDemo.getHeroNode(xiyou4,arrayListDemo.getHead());
System.out.println("获取到的节点"+heroNode);
ArrayListDemo.list(arrayListDemo.getHead());
}
}
class ArrayListDemo{
private HeroNode head;
public HeroNode getHead() {
return head;
}
public void setHead(HeroNode head) {
this.head = head;
}
//设置头结点
public ArrayListDemo(){
head = new HeroNode(0,"","");
}
//按顺序增加节点
public void add(HeroNode heroNode){
//因为头结点不能移动,暂时复制给临时变量
HeroNode temp = head;
while (true){
//如果是空的就直接加入头结点的下一个
if (temp.getNext()==null){
break;
}
//链表不为空的时候就需要比较该插入到哪个节点
if (temp.getNext().getNo()>heroNode.getNo()){
break;
}
//没有找到就继续往后移一位
temp = temp.getNext();
}
//找到节点后进行插入,先将原来的节点后的子节点放在需要插入节点的next
heroNode.setNext(temp.getNext());
//然后将插入的节点放在原来的节点
temp.setNext(heroNode);
}
//删除节点
public static void delete(HeroNode heroNode,HeroNode head){
//头结点不动
HeroNode temp = head;
if (temp.getNext()==null){
System.out.println("链表为空不能删除");
return;
}
boolean flag = false;
while (true){
if (temp.getNext().getNo()==heroNode.getNo()){
flag = true;
break;
}
temp = temp.getNext();
}
if (flag){
temp.setNext(temp.getNext().getNext());
}else {
System.out.println("没有找到对应节点删除");
}
}
//取出节点
public static HeroNode getHeroNode(HeroNode heroNode,HeroNode head){
//头结点不能动
HeroNode temp = head;
boolean flag = false;
while (true){
if (temp.getNext()==null){
System.out.println("链表为空");
return null;
}
if (temp.getNext().getNo()==heroNode.getNo()){
flag = true;
break;
}
temp = temp.getNext();
}
if (flag){
HeroNode heroNode1 = temp.getNext();
delete(heroNode,head);
return heroNode1;
}else {
System.out.println("没有找到删除的节点");
}
return null;
}
//简单的展示
public static void list(HeroNode head){
//头结点不变动
HeroNode temp = head;
while (temp.getNext()!=null){
System.out.println(temp.getNext());
temp = temp.getNext();
}
}
//翻转链表
public static void reverse(HeroNode head,ArrayListDemo arrayListDemo){
HeroNode pre = head.getNext();
//创建一个新的链表节点用于存储反转的链表
HeroNode temp = new HeroNode(0,"","");
if (head.getNext()==null||head.getNext().getNext()==null){
System.out.println("链表为空或只有一个节点");
return;
}
while (pre!=null){
HeroNode heroNode = getHeroNode(pre,head);
pre = heroNode.getNext();
heroNode.setNext(temp.getNext());
temp.setNext(heroNode);
}
head = temp;
arrayListDemo.setHead(head);
}
//合并两个链表
public static void merge(ArrayListDemo arrayListDemo,ArrayListDemo demo){
HeroNode head1 = arrayListDemo.getHead();
HeroNode head2 = demo.getHead();
while (head1.getNext()!=null&&head2.getNext()!=null){
if (head1.getNext().getNo()>head2.getNext().getNo()){
//如果链表1的大于链表2的值,则将链表2的该节点插入到链表1中
//先取出链表2中的该节点,因为取出后已经移动一位,所以无需再次移动
HeroNode heroNode = getHeroNode(head2.getNext(),demo.getHead());
arrayListDemo.add(heroNode);
//链表2往后移动一位
//head2 = head2.getNext();
}else {
//如果不满足则将链表1往后移动一位
head1 = head1.getNext();
}
}
//链表1还有剩,则不处理直接原样即可,如果是链表2还有剩,则需将链表2中剩余的节点插入到链表1中
while (head2.getNext()!=null){
HeroNode heroNode = getHeroNode(head2.getNext(),demo.getHead());
arrayListDemo.add(heroNode);
//链表2往后移动一位
//head2 = head2.getNext();
}
//arrayListDemo.setHead(head1);
}
}
有些地方的编码思想加了注释,有些简单的就没有注释了
才开始学数据结构,有些地方考虑不周,算法也不能算最优,还请各位 批评指正 !!!