package DataStructures.linkedList;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
System.out.println("双向链表的测试:");
HeroNode2 hero1 = new HeroNode2(1,"宋江","及时雨");
HeroNode2 hero2 = new HeroNode2(2,"卢俊义","玉麒麟");
HeroNode2 hero3 = new HeroNode2(3,"吴用","智多星");
HeroNode2 hero4 = new HeroNode2(4,"林冲","豹子头");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(hero1);
doubleLinkedList.add(hero2);
doubleLinkedList.add(hero4);
doubleLinkedList.list();
// //修改
// HeroNode2 newHeroNode = new HeroNode2(4,"linchong","baozitou");
// doubleLinkedList.update(newHeroNode);
// doubleLinkedList.list();
//
// //删除
// doubleLinkedList.delete(4);
// doubleLinkedList.delete(1);
// doubleLinkedList.delete(1);
// doubleLinkedList.list();
System.out.println("按顺序添加:");
doubleLinkedList.addByOrder(hero3);
doubleLinkedList.list();
}
}
//创建双向链表
class DoubleLinkedList{
//创建头节点
private HeroNode2 head = new HeroNode2(0,"","");
public HeroNode2 getHead(){
return head;
}
//遍历单链表
public void list(){
//判断链表是否为空
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
while(temp != null){
System.out.println(temp);
temp = temp.next;
}
}
//尾插法增加节点
public void add(HeroNode2 hero){
//通过辅助变量来判断是否到达链表最后一个节点
HeroNode2 temp = head;
while(true){
if(temp.next == null){
break;
}
temp = temp.next;
}
hero.next = temp.next;
temp.next = hero;
hero.pre = temp;
}
//根据编号修改节点信息
public void update(HeroNode2 newHeroNode){
HeroNode2 temp = head.next;
boolean flag = false;
while(true){
if(temp == null ){
System.out.println("链表为空");
break;
}
if(temp.no == newHeroNode.no){
// temp.name = newHeroNode.name;
// temp.nickname = newHeroNode.nickname;
flag = true;
break;
}
temp = temp.next;
}
if(flag){ // 找到要更新信息节点的编号,进行更新
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
}else{ //编号节点不存在,不能修改
System.out.printf("没有找到编号为%d的节点,不能修改!",newHeroNode.no);
}
}
//根据编号删除节点
/**
* 1.对于双向链表,可以直接找到要删除的节点,不需要像单链表一样找要删除节点的前一个节点
* @param 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.pre.next = temp.next;
if(temp.next != null){
temp.next.pre = temp.pre; //最后一个节点不需要执行此条语句,否则会出现空指针异常
}
}else{
System.out.printf("没有找到编号为%d的节点,不能删除!",no);
}
}
//按顺序添加节点
public void addByOrder(HeroNode2 hero){
//因为是单链表,所以找到的节点必须位于要插入节点的前一个节点
HeroNode2 temp = head;
boolean flag = false; //标志添加的编号是否存在,默认为false
while(true){
if(temp.next == null ){ //说明temp已经在链表尾部
break;
}
if( temp.next.no > hero.no){ //位置找到,在temp节点后面插入当前节点
// hero.next = temp.next;
// temp.next = hero;
break;
}else if(temp.next.no == hero.no ){ //要添加的编号已经存在
flag = true;
break;
}
temp = temp.next; //继续遍历单链表
}
//判断flag
if(flag){ //编号存在,不能加入
System.out.printf("当前编号%d已经存在,不能加入!\n",hero.no);
}else{
hero.next = temp.next;
temp.next.pre = hero;
temp.next = hero;
hero.pre = temp;
}
}
}
//创建节点
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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
|