单链表的缺点
双向链表的思路分析
源代码实现增删改查
package list
;
import java
.awt
.font
.TextMeasurer
;
import java
.beans
.beancontext
.BeanContext
;
import java
.util
.Stack
;
public class DoubleListDemo {
public static void main(String
[] args
) {
DoubleList doubleList
= new DoubleList();
doubleList
.addEleById(new HeroLink2(1, "宋江", "及时雨"));
doubleList
.addEleById(new HeroLink2(2, "卢俊义", "玉麒麟"));
doubleList
.addEleById(new HeroLink2(4, "吴用", "智多星"));
doubleList
.addEleById(new HeroLink2(3, "林冲", "豹子头"));
doubleList
.showAllEle(doubleList
.getHead());
}
}
class DoubleList {
private HeroLink2 head
= new HeroLink2(0, "", "");
public HeroLink2
getHead() {
return head
;
}
public void showAllEle(HeroLink2 head
) {
if (head
.next
== null
) {
System
.out
.println("链表为空,显示全部为无");
return;
}
HeroLink2 temp
= head
.next
;
while (temp
!= null
) {
System
.out
.println(temp
);
temp
= temp
.next
;
}
}
public void addElementAtLast(HeroLink2 heroLink2
) {
if (head
.next
== null
) {
head
.next
= heroLink2
;
heroLink2
.pre
= head
;
return;
}
HeroLink2 temp
= head
.next
;
while (temp
.next
!= null
)
temp
= temp
.next
;
temp
.next
= heroLink2
;
heroLink2
.pre
= temp
;
}
public void addEleById(HeroLink2 heroLink2
) {
if (head
.next
== null
) {
head
.next
= heroLink2
;
heroLink2
.pre
= head
;
return;
}
HeroLink2 temp
= head
.next
;
while (true) {
if (temp
.next
== null
) {
if (heroLink2
.id
> temp
.id
) {
temp
.next
= heroLink2
;
heroLink2
.pre
= temp
;
}
if (heroLink2
.id
< temp
.id
) {
temp
.pre
.next
= heroLink2
;
temp
.pre
= heroLink2
;
heroLink2
.pre
= temp
.pre
;
heroLink2
.next
= temp
;
}
if (heroLink2
.id
== temp
.id
)
System
.out
.println("此元素已经存在!!,不能添加。");
break;
}
if (heroLink2
.id
< temp
.id
) {
temp
.pre
.next
= heroLink2
;
temp
.pre
= heroLink2
;
heroLink2
.pre
= temp
.pre
;
heroLink2
.next
= temp
;
break;
}
if (heroLink2
.id
== temp
.id
)
System
.out
.println("此元素已经存在!!,不能添加。");
temp
= temp
.next
;
}
}
public void delById(int id
) {
if (head
.next
== null
) {
System
.out
.println("此链表为空,不能进行删除!!!");
return;
}
HeroLink2 temp
= head
.next
;
while (true) {
if (temp
.next
== null
) {
if (temp
.id
== id
) {
temp
.pre
.next
= null
;
}else
System
.out
.println("没有找到要删除的元素");
break;
}
if (temp
.id
== id
) {
temp
.next
.pre
= temp
.pre
;
temp
.pre
.next
= temp
.next
;
break;
}else
System
.out
.println("没有找到要删除的元素");
temp
= temp
.next
;
}
}
public void updateById(HeroLink2 newHeroLink
) {
if (head
.next
== null
) {
System
.out
.println("此链表为空,不能进行修改!!!");
return;
}
HeroLink2 temp
= head
.next
;
boolean flag
= false;
while (temp
!= null
) {
if (temp
.id
== newHeroLink
.id
) {
flag
= true;
break;
}
temp
= temp
.next
;
}
if (flag
) {
temp
.nickName
= newHeroLink
.nickName
;
}else
System
.out
.println("没有查找到需要修改的元素!!!");
}
}
class HeroLink2 {
public int id
;
public String name
;
public String nickName
;
public HeroLink2 next
;
public HeroLink2 pre
;
public HeroLink2(int id
, String name
, String nickName
) {
this.id
= id
;
this.name
= name
;
this.nickName
= nickName
;
}
@Override
public String
toString() {
return "HeroLink2{" +
"id=" + id
+
", name='" + name
+ '\'' +
", nickName='" + nickName
+ '\'' +
'}';
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-33200.html