简体   繁体   English

使用Java中的单个链表和递归方法将二进制转换为十进制

[英]convert binary to decimal using single linked list and recursive method in java

How we can convert binary to decimal using single linked list and recursive method in java? 我们如何使用Java中的单个链表和递归方法将二进制转换为十进制? :-s :-s

Ex: 例如:

Input: 1->0->0->NULL 输入: 1-> 0-> 0-> NULL

Output: 4 输出 4

It's a bit tricky recursively because you need to scale "earlier" values depending on the list length, simplest way seems to be to hand down the result "so far" via an additional parameter. 递归有点棘手,因为您需要根据列表长度缩放“较早”的值,最简单的方法似乎是通过附加参数将结果“到目前为止”传递。

class Node {
  int value;
  Node next;

  int toDecimal(int resultSoFar) {
    int resultSoFar = 2 * resultSoFar + value;
    return next == null ? resultSoFar : toDecimal(resultSoFar);
  } 

  int toDecimal() {
    toDecimal(0);
  }
}

Try this (Without the null at the end of the list) : 试试这个(列表末尾没有null):

public Integer binToDec(LinkedList<Integer> list){
        Double size = (double) list.size();
        if(size == 0D) return 0;
        Integer number = list.getFirst();
        list.removeFirst();
        if(number != 0) return binToDec(list) + number*new Double(Math.pow(2D, size - 1)).intValue();
        else return binToDec(list);
    }

Be aware that it will clear the list. 请注意,它将清除列表。

I can think of two ways to solve it: 我可以想到两种解决方法:

1- If length of list is known: 1-如果列表长度已知:

// To find length of list
public int length(Node head) {
      int count = 0;
      while(head != null) {
         count++;
         head = head.next;
      }
      return count;
   }

public static int convertWhenLengthIsKnown(Node head, int len) {
      int sum = 0;
      if(head.next != null) {
         sum = convertWhenLengthIsKnown(head.next, len-1);
      }
      return sum + head.data * (int)Math.pow(2,len);
   }


   // Call this function as below:

convertWhenLengthIsKnown(head, length(head)-1);
  1. If we don't want to calculate length, then we can have a sum variable which is globally accessible, 如果我们不想计算长度,那么我们可以有一个可以全局访问的sum变量,

     private static int sum = 0; public static int convert(Node head,int i) { if(head.next != null) { i = convert(head.next, i); } sum += head.data * (int)Math.pow(2,i); return i+1; } // Call this function as below: convert(head,0); 

Below is the Node class: 下面是Node类:

class Node {
   int data;
   Node next;

   Node(int data) {
      this.data = data;
   }
}

Hope It helps you. 希望对您有帮助。

As you did not tell anything about your linked list, I assume it's similar to the Node class from pbajpai21 . 由于您没有告诉您有关链表的任何信息,因此我认为它类似于pbajpai21中Node类。

Without knowing the length of the chain you could with each level shift the value one position to the left. 不知道链的长度,您可以在每个级别将值向左移动一个位置。

the class for each node in the list 列表中每个节点的类

class Node {
    int digit;
    Node child;

    Node(int data) {
        this.digit = data;
    }

    public int getDigit() {
        return digit;
    }

    public void setChild(Node child) {
        this.child = child;
    }

    public Node getChild() {
        return child;
    }
}

the class for demonstration 示范班

public class Bits {
    public static void main(String[] args) {
        int[] ints = new int[] {1, 0, 1, 0, 1, 0};
        Node parent = new Node(ints[0]);
        Node root = parent;
        for (int i = 1; i < ints.length; i++) {
            Node child = new Node(ints[i]);
            parent.setChild(child);
            parent = child;
        }

        long value = computeValue(0, root);
        System.out.println();
        System.out.println("value = " + value);
    }

    private static long computeValue(long parentValue, Node node) {
        if (node == null) {
            return parentValue;
        }
        // only to print the current digit
        System.out.print(node.getDigit());
        long value = (parentValue << 1) + node.getDigit();
        return computeValue(value, node.getChild());
    }
}

output 输出

101010
value = 42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM