Java单链表实现多项式相加、多种方式实现约瑟夫环

数据结构复习,代码是最好的说明。

多项式相加-单链表:

节点类:

public class Node implements Cloneable{

    //系数、指数、下一个(前两者可用double)
    private int coefficient;

    private int exponent;

    private Node next;

    public Node(int coefficient, int exponent) {
        this.coefficient = coefficient;
        this.exponent = exponent;
    }

    public int getCoefficient() {
        return coefficient;
    }

    public void setCoefficient(int coefficient) {
        this.coefficient = coefficient;
    }

    public int getExponent() {
        return exponent;
    }

    public void setExponent(int exponent) {
        this.exponent = exponent;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    /**
     * 深拷贝
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    public Object clone() throws CloneNotSupportedException {
        Node node = null;
        node = (Node) super.clone();
        if (node.next!=null){
            node.next = (Node) node.next.clone();
        }

        return node;
    }

}
单链表类:

public class SingleLinkedList implements Cloneable{

    private Node head;

    public SingleLinkedList(Node head) {
        this.head = head;
    }

    /**
     * 深拷贝
     * @return
     */
    @Override
    public Object clone(){
        SingleLinkedList s = null;
        try {
            s = (SingleLinkedList) super.clone();
            s.head = (Node) head.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return s;
    }

    /**
     * 多项式append,按指数从大到小排序
     * @param coefficient
     * @param exponent
     */
    public void append(int coefficient, int exponent){
        if (coefficient==0){
            return;
        }
        Node temp = head;

        //头处理
        if (exponent>head.getExponent()){
            Node node = new Node(coefficient,exponent);
            node.setNext(head);
            head = node;
            return;
        }

        //
        while (temp.getNext()!=null){
            if (exponent<temp.getExponent()&&exponent>temp.getNext().getExponent()){
                Node node = new Node(coefficient,exponent);
                node.setNext(temp.getNext());
                temp.setNext(node);
                return;
            }else if (exponent==temp.getExponent()){
                temp.setCoefficient(temp.getCoefficient()+coefficient);
                return;
            }

            temp = temp.getNext();
        }

        //尾处理
        if(exponent==temp.getExponent()){
            temp.setCoefficient(temp.getCoefficient()+coefficient);
            return;
        }else if(exponent<temp.getExponent()){
            Node node = new Node(coefficient,exponent);
            temp.setNext(node);
            return;
        }

    }


    /**
     * 打印
     */
    public void printAll(){
        Node temp = head;
        while (temp.getNext()!=null){
            System.out.print(temp.getCoefficient()+"x^"+temp.getExponent()+(temp.getNext().getCoefficient()>0?"+":""));
            temp = temp.getNext();
        }
        System.out.print(temp.getCoefficient()+"x^"+temp.getExponent());
        System.out.println();
    }

    /**
     * 多项式相加返回结果,用的深拷贝
     * @param anotherSingleLinkedList
     * @return
     */
    public SingleLinkedList add(SingleLinkedList anotherSingleLinkedList) {
        SingleLinkedList result = (SingleLinkedList) anotherSingleLinkedList.clone();

        Node temp = head;
        while (temp.getNext()!=null){
            result.append(temp.getCoefficient(),temp.getExponent());
            temp = temp.getNext();
        }
        return result;

    }

    public static void main(String[] args) {
        SingleLinkedList a=new SingleLinkedList(new Node(10,5));
        a.append(10,4);
        a.append(10,3);
        a.append(10,2);
        a.append(10,1);


        SingleLinkedList b=new SingleLinkedList(new Node(-5,10));
        b.append(-5,11);
        b.append(-5,8);
        b.append(-5,9);
        b.append(-5,7);
        b.append(-5,5);

        System.out.println("相加之前:");
        a.printAll();
        b.printAll();

        System.out.println("结果:");
        a.add(b).printAll();

        System.out.println("相加之后:");
        a.printAll();
        b.printAll();

    }

}



约瑟夫环-循环链表:

节点类:

public class Node {
    private int num;

    private Node next;

    public Node(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
主类:

public class Josehus {

    /**
     *
     * @param n 一公多少人,例如8个人,则编号1-8号
     * @param s 开始号码,例如1,则从1号开始报数
     * @param m 指定号码,例如3,则报到3的人移除
     */
    public void josehus(int n,int s,int m){

        //表头
        Node head = new Node(1);

        //pre为当前报数者的前继,使得变量最少
        Node pre = null;

        //初始化循环链表,help为辅助变量
        Node help = head;
        for (int i = 2; i <= n; i++) {
            if (i==s){
                pre = help;
            }
            help.setNext(new Node(i));
            help = help.getNext();
        }
        help.setNext(head);

        if (pre == null){
            pre = help;
        }

        //开始报数,报到数则移除链表
        int j = 0;
        while (pre.getNum()!=pre.getNext().getNum()){
            if(j == m-1){
                System.out.print(pre.getNext().getNum()+"-");
                pre.setNext(pre.getNext().getNext());
                j = 0;
                continue;
            }else {
                j++;
            }
            pre = pre.getNext();
        }
        System.out.print(pre.getNum());
        System.out.println();

    }

    public static void main(String[] args) {
        Josehus j=new Josehus();
        j.josehus(8,1,3);
    }

}

约瑟夫环-数组:

public void josehus(int n,int s,int m){
    int [] array = new int[n];
    for (int i = 0; i < n; i++)
        array[i] = i+1;

    int count = 0;
    int num = n;

    //两层循环,相当于一个n*n的遍历
    while (num > 0) {
        for (int i = 0; i < n; i++) {
            //下标移到报数起始位置
            if (s != 1) {
                i = s - 1;
                s = 1;
            }

            //该位置已被移除继续,到下个位置
            if (array[i] == 0)
                continue;

            //报数增加
            count++;

            //符合要求,则打印、该位置抹0、个数自减
            if (count == m) {
                System.out.print(array[i]+(num==1?"":"-"));
                array[i] = 0;
                count = 0;
                num--;
            }
        }
    }
}


参考:http://www.cnblogs.com/michael110/archive/2011/03/25/1995715.html

如有错误,欢迎纠正!


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值