//Convert the binary number represented to a decimal number //The binary number are stored in Singly LinkedList public static void Q1() { string binaryNum = "0000011111"; SinglyLinkedList.Node root = CreateLinkedList(binaryNum); long decValue = ConvertBinaryToDecimal(root); Console.WriteLine("Binary number {0} decimal value is {1}", binaryNum, decValue); }
private static long ConvertBinaryToDecimal(SinglyLinkedList.Node head) { StringBuilder nums = new StringBuilder(); while (head != null) { nums.Append(head.data); head = head.next; } long power = nums.Length - 1, baseValue = 2, decValue = 0; for (int i = 0; i < nums.Length; i++) { long binary = nums[i] == '0' ? 0 : 1; decValue += binary * getPowerValue(baseValue, power); power--; } return(decValue); }