Javatpoint Logo
Javatpoint Logo

Decimal Equivalent of Binary Linked List

Introduction

Binary numbers are an essential subject in computer science and data representation fields. Computers, which are sophisticated devices made to work with data, depend on the binary number system, which only employs the digits 0 and 1. However, decimal numerals, which have ten digits ranging from 0 to 9, are more common in human thought. We frequently need to translate binary data structures into their decimal equivalents in order to close the gap between the binary realm of computers and the decimal world of humans. At this point, the idea of the "Decimal Equivalent of Binary Linked List" starts to make sense.

What is a Binary Linked List?

In computer science, a linked list is a typical type of data structure. It is a group of nodes, each of which has information and a link pointing to the node after it in the sequence. A unique kind of linked list known as a binary linked list is one in which every node denotes a binary digit, either 0 or 1. A binary number is stored in a linked list, where the nodes are arranged in a sequence that depicts the binary digits from left to right.

Take into consideration, for instance, the binary linked list that contains the binary number "1010." Every node in this linked list contains a single binary digit:

1 -> 0 -> 1 -> 0

The most significant digit (1) is represented by the first node in this linked list, and the last node represents the least significant digit (0).

The Need for Decimal Equivalents

Although computers need binary numbers to process data and conduct calculations effectively, binary numbers could be more user-friendly. Because decimal numbers have ten easily distinct digits, they are a natural choice for everyday arithmetic and are more comfortable for most individuals.

It's frequently required to convert binary data into decimal form in order to make it more readable by humans. For example, you need to know the decimal equivalent of a binary integer that is kept in a binary linked list in order to execute arithmetic operations on it or to understand its value better.

Decimal to Binary Conversion of Linked Lists

When converting a binary linked list to its decimal counterpart, the binary number is created by iterating through the linked list and performing a mathematical transformation to each binary digit.

Let's examine each phase of this conversion procedure in detail:

1. Set up the variables

Setting up a variable to hold the decimal equivalent is the first step. As you navigate the linked list, this variable will be utilized to aggregate the decimal value. This variable will be called decimal, and its initial value will be zero.

2. Go Through the Indexed List

The most important binary digit is at the top of the binary linked list, where you begin. After that, you proceed node by node through the list, going from left to right until you reach the end.

3. Update the Decimal Value

You carry out the following actions for every binary digit in the linked list:

  • Add two to the decimal value now shown. This creates space for the following binary digit in binary and is basically a left-shift operation.
  • To decimal, add the binary value (0 or 1) that is currently stored in the node. This is the point at which the increasing decimal equivalent is adjusted to include the binary digit's real value.

You are essentially creating the decimal representation of the complete binary number by iteratively carrying out these operations for every binary digit in the linked list.

4. Proceed to the Following Node

You proceed to the next node in the linked list and repeat the procedure there until you've reached the end of the list after changing the decimal value for the current node.

5. Result

The decimal variable will store the binary number in the linked list's decimal equivalent once you've reached the end of the list.

Code

Output:

Decimal Equivalent of Binary Linked List

Code Explanation

ListNode Structure

  • Represents a node in a linked list with integer values.
  • Attributes: val (integer value) and next (pointer to the next ListNode).

Binary to Decimal Conversion Function

  • Converts a linked list representing a binary number to its decimal equivalent.
  • Iterates through the linked list, shifting the decimal value to the left and adding the current bit.

Main Function

  • Creates a linked list (head) with the binary representation: 1010.
  • Calls binaryToDecimal to convert the binary linked list to decimal and prints the result.
  • Frees the allocated memory for the linked list.

Memory Deallocation

  • Iterates through the linked list and frees each node's memory to prevent memory leaks.

Output

  • Prints the decimal equivalent: Decimal equivalent: 10.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA