带头节点的双向链表
什么是双向链表: 双链表是链表的一种,由节点组成,每个数据结点中都有两个指针,分别指向直接后继和直接前驱。
模拟结点
public class DoubleNode {
public int id;
public String name;
public int price;
public DoubleNode pre;
public DoubleNode next;
public DoubleNode ( int id, String name, int price) {
this . id = id;
this . name = name;
this . price = price;
}
@Override
public String toString ( ) {
return "DoubleNode{id=" + id + ", name=‘" + name + '’' + ", price=" + price + '}' ;
}
}
双向链表的增删改查
public class DoubleLinkList {
private DoubleNode headNode = new DoubleNode ( 0 , "" , 0 ) ;
public void addEnd ( DoubleNode newNode) {
DoubleNode temp = headNode;
while ( true ) {
if ( temp. next == null ) {
temp. next = newNode;
newNode. pre = temp;
return ;
}
temp = temp. next;
}
}
public void addById ( DoubleNode newNode) {
if ( headNode. next == null ) {
headNode. next= newNode;
return ;
}
DoubleNode temp = headNode;
while ( true ) {
if ( temp. id > newNode. id) {
if ( temp. pre== null ) {
headNode. next= newNode;
newNode. next= temp;
temp. pre= newNode;
} else {
newNode. pre= temp. pre;
newNode. next= temp;
temp. pre. next= newNode;
temp. pre= newNode;
}
return ;
}
if ( temp. next == null ) {
temp. next= newNode;
newNode. pre= temp;
return ;
} if ( temp. id == newNode. id) {
System . out. println ( "id重复无法插入" ) ;
return ;
}
temp = temp. next;
}
}
public void update ( DoubleNode newNode) {
if ( headNode. next == null ) {
System . out. println ( "当前链表为空" ) ;
return ;
}
DoubleNode temp = headNode. next;
while ( true ) {
if ( temp. id == newNode. id) {
temp. name = newNode. name;
temp. price = newNode. price;
return ;
}
if ( temp. next == null ) {
System . out. println ( "未找到要修改的结点" ) ;
return ;
}
temp = temp. next;
}
}
public void delete ( int id) {
if ( headNode. next == null ) {
System . out. println ( "当前链表为空,无法删除结点" ) ;
return ;
}
DoubleNode temp = headNode. next;
boolean isFind = false ;
while ( true ) {
if ( temp. next == null ) {
System . out. println ( "没有找到目标结点" ) ;
return ;
}
if ( temp. id == id) {
temp. pre. next = temp. next;
if ( temp. next != null ) {
temp. next. pre = temp. pre;
}
temp. pre = null ;
temp. next = null ;
return ;
}
temp = temp. next;
}
}
public void show ( ) {
if ( headNode. next == null ) {
System . out. println ( "当前双向链表为空" ) ;
return ;
}
DoubleNode temp = headNode;
while ( true ) {
if ( temp. next== null ) {
return ;
}
temp = temp. next;
System . out. println ( temp) ;
}
}
}
测试
public class DoubleLinkTest {
public static void main ( String [ ] args) {
DoubleNode doubleNode1 = new DoubleNode ( 1 , "三国演义" , 100 ) ;
DoubleNode doubleNode2 = new DoubleNode ( 2 , "水浒传" , 100 ) ;
DoubleNode doubleNode3 = new DoubleNode ( 3 , "西游记" , 100 ) ;
DoubleNode doubleNode4 = new DoubleNode ( 4 , "红楼梦" , 100 ) ;
DoubleNode doubleNode5 = new DoubleNode ( 5 , "山海经" , 100 ) ;
DoubleNode doubleNode6 = new DoubleNode ( 6 , "史记" , 100 ) ;
DoubleNode doubleNode7 = new DoubleNode ( 7 , "资治通鉴" , 100 ) ;
DoubleNode doubleNode8 = new DoubleNode ( 8 , "儒林外史" , 100 ) ;
DoubleLinkList list = new DoubleLinkList ( ) ;
list. addById ( doubleNode1) ;
list. addById ( doubleNode4) ;
list. addById ( doubleNode2) ;
list. addById ( doubleNode3) ;
list. addEnd ( doubleNode5) ;
list. addEnd ( doubleNode6) ;
list. addEnd ( doubleNode7) ;
list. addEnd ( doubleNode8) ;
list. update ( new DoubleNode ( 8 , "test" , 100 ) ) ;
list. show ( ) ;
}
}