一. 定义商品类(商品节点):
/*商品节点*/
public class GoodsNode {
private int id;
private double price;
private String name;
public GoodsNode next;
public GoodsNode(int id, double price, String name) {
this.id = id;
this.price = price;
this.name = name;
}
public int getId() {
return id;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "GoodsNode{" +
"id=" + id +
", price=" + price +
", name='" + name + '\'' +
'}';
}
}
二. 定义链表类以及链表对应增删改查功能
/*链表类*/
public class GoLinkedList {
private GoodsNode head = new GoodsNode(0,0.0,"");
/*1.往链表中添加结点*/
public void add(GoodsNode goodsNode){
GoodsNode temp;
temp = head;
while (true){
if(temp.next==null){
break;
}
temp = temp.next;
}
temp.next = goodsNode;
}
/*2.按照商品编号id值进行添加,从小到大的顺序*/
boolean flg = false;
public void addOrder(GoodsNode goodsNode){
/*如果头节点后没有节点,则直接插入节点*/
GoodsNode temp = head;
while (true){
if(temp.next==null){
break;
}
if(temp.next.getId()>goodsNode.getId()){
break;
}
if(temp.next.getId()==goodsNode.getId()){
flg = true;
break;
}
temp = temp.next; /*遍历移动到此结点,进行goodsNode结点插入*/
}
if(flg){
System.out.println("编号重复,不可插入");
}else {
/*结点插入*/
goodsNode.next = temp.next;
temp.next = goodsNode;
}
}
/*3.根据编号修改链表结点*/
public void updateNode(GoodsNode goodsNode){
/*判断是否为空链表,空链表的话不需要往下查找结点修改*/
if(head.next==null){
System.out.println("此为空链表");
}
GoodsNode temp = head.next;//定义temp结点为除头节点第一个结点
boolean flg = false;//标识符,标识是因为找到编号退出的循环还是此链表中没有对应编号商品退出的循环
while (true){
if(temp.getId()==goodsNode.getId()){
flg = true;
break; //找到此编号的商品(结点)
}
if(temp==null){
break; //此链表中没有对应编号商品
}
temp = temp.next;
}
if(flg){
//真正的修改点
temp.setName(goodsNode.getName());
temp.setPrice(goodsNode.getPrice());
}else {
System.out.println("链表中没有此编号商品");
}
}
/*4.根据编号删除链表结点*/
public void deletNode(int id){
GoodsNode temp = head;
boolean flg = false;
while (true){
if(temp.next==null){
break; //表示没有找到此编号结点
}
if(temp.next.getId()==id){
flg = true;
break;//表示找到了该删除的结点
}
temp = temp.next;
}
if(flg){
temp.next = temp.next.next;
}else {
System.out.println("没有找到该删除结点");
}
}
/*5.查看该链表结点元素*/
public void List(){
if(head.next==null){
System.out.println("此链表为空链表");
}
GoodsNode temp = head.next;
while (true){
if(temp==null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
/*6.面试题:计算单链表中的结点个数,不包括头节点*/
public int listSize(){
if(head.next==null){
System.out.println("此链表为空链表");
return 0;//为空链表
}
/*方法一*/
// int i = 1;
// GoodsNode temp = head.next;
// while (true){
// if(temp.next==null){
// break;//后面不再有节点了
// }
// temp = temp.next;
// i++;
// }
//
// return i;
/*方法二*/
GoodsNode temp = head.next;
int lenth = 0;
while (temp != null){
lenth++;
temp = temp.next;
}
return lenth;
}
}
三. 测试类:
/*测试类*/ public class LinkedText { public static void main(String[] args) { GoodsNode goodsNode1 = new GoodsNode(1,20.3,"衣服"); GoodsNode goodsNode2 = new GoodsNode(2,20.4,"鞋子"); GoodsNode goodsNode3 = new GoodsNode(3,20.5,"裤子"); GoLinkedList linkedList = new GoLinkedList(); linkedList.addOrder(goodsNode1); linkedList.addOrder(goodsNode3); linkedList.addOrder(goodsNode2); linkedList.updateNode(new GoodsNode(2,30.0,"阿迪达斯鞋")); linkedList.deletNode(2); linkedList.List(); System.out.println(linkedList.listSize()); /*面试题:计算单链表中的结点个数,不包括头节点*/ } }