单链表的简单实现和测试(Java)

1.初始化节点

package linklist;

public class GoodsNode {
    public int id;
    public String name;
    public double price;
    public GoodsNode next;

    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 +
                '}';
    }
}

2.初始化链表以及基本操作 

​
package linklist;

import com.sun.xml.internal.bind.v2.model.core.ID;

public class LinkList {
    // 头结点,并不保存任何数据
    private GoodsNode node = new GoodsNode(0, "", 0.0);

    /**
     * 在链表结尾添加元素
     */
    public void add(GoodsNode goodsNode) {
        GoodsNode temp = node;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = goodsNode;
    }

    /**
     * 按照id从小到大添加元素
     */
    public void addOrder(GoodsNode goodsNode) {
        GoodsNode temp = node;
        boolean flag = true;

        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.next.id > goodsNode.id) {
                break;
            }
            if (temp.next.id == goodsNode.id) {
                flag = false;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            goodsNode.next = temp.next;
            temp.next = goodsNode.next;

        } else
            System.out.println("该元素已存在不可以重复添加");


    }

    /**
     * 按照id删除元素
     */
    public void delNode(int id) {
        if (node.next == null) {
            System.out.println("链表为空");
            return;
        }
        GoodsNode temp = node;
        boolean flag = false;
        while (true) {
            if (temp.next.id == id) {
                break;
            }
            if (temp.next == null) {
                flag = true;
                break;
            }

            temp = temp.next;
        }
        if (flag) {
            System.out.println("没有该节点");
        } else {
            temp.next = temp.next.next;
        }

    }

    /**
     * 修改节点数据
     */
    public void updateNode(GoodsNode goodsNode) {
        boolean flag = false;
        if (node.next == null) {
            System.out.println("链表为空");
            return;
        }
        GoodsNode temp = node;
        while (true) {
            if (temp.id == goodsNode.id) {
                break;
            }
            if (temp == null) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            System.out.println("链表中没有该元素");
        } else {
            temp.name = goodsNode.name;
            temp.price = goodsNode.price;
        }
    }

    /**
     * 遍历链表
     */
    public void list() {
        GoodsNode temp = node.next;
        if (temp == null) {
            System.out.println("这是一个空链表");
        }
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }


}

​

3.简单测试

package linklist;

import java.util.List;

public class ListTest {
    public static void main(String[] args) {
        LinkList list = new LinkList();
        GoodsNode node1 = new GoodsNode(1, "anta", 99);
        GoodsNode node2 = new GoodsNode(2, "lining", 99999);
        GoodsNode node3 = new GoodsNode(3, "hxek", 66);
        GoodsNode node4 = new GoodsNode(4, "peak", 88);

        list.list();

        // 添加
//        list.add(node1);
//        list.add(node2);
//        list.add(node3);
//        list.add(node4);
//
//        list.list();

        // 按id添加
        list.addOrder(node1);
        list.addOrder(node4);
        list.addOrder(node2);
        list.addOrder(node3);

        //list.list();

        // 删除
        list.delNode(1);
        //list.list();

        // 修改
        list.updateNode(new GoodsNode(2, "ln", 9));
        list.list();


    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于实现多项式的相加和相乘,可以使用单链表来存储多项式的每一项。 首先,我们可以创建一个节点类来表示多项式的每一项: ```java class Node { int coefficient; // 系数 int exponent; // 指数 Node next; // 下一个节点 Node(int coefficient, int exponent) { this.coefficient = coefficient; this.exponent = exponent; this.next = null; } } ``` 然后,我们可以创建一个链表类来管理多项式的各个节点: ```java class Polynomial { Node head; Polynomial() { this.head = null; } // 添加节点 void addTerm(int coefficient, int exponent) { Node newNode = new Node(coefficient, exponent); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } } } ``` 接下来,我们可以实现多项式的相加和相乘的方法: ```java class PolynomialOperations { // 相加两个多项式 static Polynomial addPolynomials(Polynomial poly1, Polynomial poly2) { Polynomial result = new Polynomial(); Node p1 = poly1.head; Node p2 = poly2.head; while (p1 != null && p2 != null) { if (p1.exponent == p2.exponent) { result.addTerm(p1.coefficient + p2.coefficient, p1.exponent); p1 = p1.next; p2 = p2.next; } else if (p1.exponent > p2.exponent) { result.addTerm(p1.coefficient, p1.exponent); p1 = p1.next; } else { result.addTerm(p2.coefficient, p2.exponent); p2 = p2.next; } } while (p1 != null) { result.addTerm(p1.coefficient, p1.exponent); p1 = p1.next; } while (p2 != null) { result.addTerm(p2.coefficient, p2.exponent); p2 = p2.next; } return result; } // 相乘两个多项式 static Polynomial multiplyPolynomials(Polynomial poly1, Polynomial poly2) { Polynomial result = new Polynomial(); Node p1 = poly1.head; while (p1 != null) { Node p2 = poly2.head; while (p2 != null) { result.addTerm(p1.coefficient * p2.coefficient, p1.exponent + p2.exponent); p2 = p2.next; } p1 = p1.next; } return result; } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { Polynomial poly1 = new Polynomial(); poly1.addTerm(2, 3); poly1.addTerm(1, 2); poly1.addTerm(3, 0); Polynomial poly2 = new Polynomial(); poly2.addTerm(1, 2); poly2.addTerm(2, 1); poly2.addTerm(4, 0); Polynomial sum = PolynomialOperations.addPolynomials(poly1, poly2); Polynomial product = PolynomialOperations.multiplyPolynomials(poly1, poly2); System.out.println("Sum of polynomials: "); printPolynomial(sum); System.out.println("Product of polynomials: "); printPolynomial(product); } // 打印多项式 static void printPolynomial(Polynomial poly) { Node temp = poly.head; while (temp != null) { System.out.print(temp.coefficient + "x^" + temp.exponent); if (temp.next != null) { System.out.print(" + "); } temp = temp.next; } System.out.println(); } } ``` 这样,我们就可以使用单链表实现多项式的相加和相乘了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值