双向链表按照顺序添加-----------韩顺平老师讲的数据结构

addOrderBy就是按照顺序添加,所有代码可以直接复制测试,测试用例也已经写好

package com.atguigu.linkedlist;

public class DoubleLinkedListDemo {
     
		public static void main(String[] args) {
     
			//先创建节点
			HeroNode2 hero1 = new HeroNode2(1, "松江", "及时雨");
			HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
			HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
			HeroNode2 hero4 = new HeroNode2(4, "李逵", "黑旋风");
			
			DoubleLinkedList linkedList = new DoubleLinkedList();
			linkedList.addOrderBy(hero2);
			linkedList.addOrderBy(hero1);
			linkedList.addOrderBy(hero3);
			linkedList.addOrderBy(hero4);
			
			System.out.println("原来链表~");
			linkedList.list();
			
			HeroNode2 newHero = new HeroNode2(3, "小用", "不智");
			System.out.println("修改链表~");
			linkedList.update(newHero);
			linkedList.list();
			
			System.out.println("删除链表~");
			linkedList.delete(1);
			linkedList.list();
		}
}

class DoubleLinkedList{
     
	//初始化头结点,头结点不能动;不存放具体的数据
		private HeroNode2 head = new HeroNode2(0, "", "");
		
		//返回头结点
		public HeroNode2 getHead() {
     
			return head;
		}
		
	//添加一个节点到双向链表
	public void add(HeroNode2 heroNode) {
     
		HeroNode2 temp = head;
		while(true) {
     
			if(temp.next == null) {
     
				break;
			}
			temp = temp.next;
		}
		//形成一个双向链表
		temp.next = heroNode;
		heroNode.pre = temp;
	}
	//按照顺序添加双向链表,辅助变量无需指向下一个,直接指向头节点,反正也不可能存在  能够添加到头结点之前的节点
	public void addOrderBy(HeroNode2 heroNode) {
     
			HeroNode2 temp = head;
			boolean flag = false;
			boolean flag1 = false;
			while(true) {
     
				if(temp.no > heroNode.no) {
     
					break;
				}
				if(temp.next == null) {
     
					flag = true;
					break;
				}
				if(temp.no == heroNode.no) {
     
					flag1 = true;
					break;
				}
				temp = temp.next;
			}
			if(flag1) {
     
				System.out.printf("存在编号为%d的英雄,无法添加",heroNode.no);
			}else if(flag) {
     
				temp.next = heroNode;
				heroNode.pre = temp;
			}else {
     
				//temp就是被添加的当前元素,没有指向上一个或者下一个
				temp.pre.next = heroNode;
				heroNode.pre = temp.pre;
				heroNode.next = temp;
				temp.pre = heroNode;
			}
	}
	
	public void update(HeroNode2 heroNode) {
     
		if(head.next == null) {
     
			System.out.println("链表为空,无法修改");
			return;
		}
		HeroNode2 temp = head.next;
		boolean flag = false;
		while(temp != null) {
     
			if(temp.no == heroNode.no) {
     
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if(flag) {
     
			temp.name = heroNode.name;
			temp.nickname = heroNode.nickname;
		}else {
     
			System.out.printf("没有找到编号为%d的英雄,无法修改\n",heroNode.no);
		}
		
	}

	public void delete(int no) {
     
		if(head.next == null) {
     
			System.out.println("链表为空,无法删除");
			return;
		}
		HeroNode2 temp = head.next;//辅助变量
		boolean flag = false;
		while(true) {
     
			if(temp == null) {
     
				break;
			}
			if(temp.no == no) {
     
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if(flag) {
     
			
			//如果是最后一个节点,就不需要执行下面的代码
			//temp已经是最后一个节点,那么temp.next必然为null,null.next报空指针异常
			if(temp.next != null) {
     
			temp.next.pre = temp.pre;
			}
			temp.pre.next = temp.next;
		}else {
     
			System.out.printf("没有找到编号为%d的英雄,无法删除\n",no);
		}
	}
	
	public void list() {
     
		if(head.next == null) {
     
			System.out.println("链表为空~");
			return;
		}
		
		HeroNode2 temp = head.next;
		while(true) {
     
			if(temp == null) {
     
				break;
			}
			System.out.println(temp);
			temp = temp.next;
		}
	}
	
	
}
//定义HeroNode ,每个HeroNode对象就是一个节点
class HeroNode2{
     
	public int no;
	public String name;
	public String nickname;
	public HeroNode2 next;//指向下一个节点
	public HeroNode2 pre;//指向上一个节点
	//构造器
	public HeroNode2(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 + "]";
	}
}

你可能感兴趣的:(数据结构和算法,java,eclipse)