class GoodsNode
public class GoodsNode {
public int id;
public String name;
public double price;
public GoodsNode next;
public GoodsNode ( ) {
}
public GoodsNode ( int id, String name, double price) {
this . id = id;
this . name = name;
this . price = price;
}
@Override
public String toString ( ) {
return "GoodsNode{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}' ;
}
}
class DLLinkedList
public class DLLinkedList {
private GoodsNode node = new GoodsNode ( ) ;
public void add ( GoodsNode goodsNode) {
GoodsNode temp = node;
while ( true ) {
if ( temp. next== null ) {
break ;
}
temp= temp. next;
}
temp. next= goodsNode;
}
public void addOrder ( GoodsNode goodsNode) {
GoodsNode temp= node;
boolean flg = false ;
while ( true ) {
if ( temp. next== null ) {
break ;
}
if ( temp. next. id> goodsNode. id) {
break ;
} else if ( temp. next. id== goodsNode. id) {
flg= true ;
break ;
}
temp= temp. next;
}
if ( flg) {
throw new RuntimeException ( "已经存了该商品不能重复添加" ) ;
} else {
goodsNode. next= temp. next;
temp. next= goodsNode;
}
}
public void updateNode ( GoodsNode goodsNode) {
if ( node. next== null ) {
throw new RuntimeException ( "链表为空" ) ;
}
GoodsNode temp= node. next;
boolean flg = false ;
while ( true ) {
if ( temp== null ) {
break ;
}
if ( temp. id== goodsNode. id) {
flg= true ;
break ;
}
temp= temp. next;
}
if ( flg) {
temp. name= goodsNode. name;
temp. price= goodsNode. price;
} else {
throw new RuntimeException ( "在整个链表中未找到目标节点。。。" ) ;
}
}
public void delNode ( int id) {
GoodsNode temp= node;
boolean flg= false ;
while ( true ) {
if ( temp. next== null ) {
break ;
}
if ( temp. next. id== id) {
flg= true ;
break ;
}
temp= temp. next;
}
if ( flg) {
temp. next= temp. next. next;
} else {
throw new RuntimeException ( "未找到要删除的目标节点" ) ;
}
}
public void list ( ) {
if ( node. next== null ) {
throw new RuntimeException ( "链表中没有元素" ) ;
}
GoodsNode temp = node. next;
while ( true ) {
if ( temp== null ) {
break ;
}
System . out. println ( temp) ;
temp= temp. next;
}
}
public int getLength ( ) {
int count= 0 ;
if ( node. next== null ) {
return count;
}
GoodsNode temp= node. next;
while ( true ) {
if ( temp== null ) {
return count;
} else {
temp= temp. next;
count++ ;
}
}
}
}
class LinkedTest
public class LinkedTest {
public static void main ( String [ ] args) {
DLLinkedList dlLinkedList = new DLLinkedList ( ) ;
GoodsNode goodsNode1= new GoodsNode ( 1 , "耐克运动鞋1" , 199.00 ) ;
GoodsNode goodsNode2= new GoodsNode ( 2 , "耐克运动鞋2" , 299.00 ) ;
GoodsNode goodsNode3= new GoodsNode ( 3 , "耐克运动鞋3" , 399.00 ) ;
GoodsNode goodsNode4= new GoodsNode ( 4 , "耐克运动鞋4" , 499.00 ) ;
GoodsNode goodsNode5= new GoodsNode ( 5 , "耐克运动鞋5" , 599.00 ) ;
dlLinkedList. addOrder ( goodsNode1) ;
dlLinkedList. addOrder ( goodsNode3) ;
dlLinkedList. addOrder ( goodsNode2) ;
dlLinkedList. addOrder ( goodsNode5) ;
dlLinkedList. addOrder ( goodsNode4) ;
System . out. println ( dlLinkedList. getLength ( ) ) ;
dlLinkedList. list ( ) ;
}
}