Home > Back-end >  Convert a Linked List into a reversed integer number - C
Convert a Linked List into a reversed integer number - C

Time:09-19

I want to convert a Linked List into a reversed integer number

For eg:
LinkedList: 3-> 4-> 2
Output: 243

Eg2:
LinkedList: 5-> 6-> 4
Output: 465

LAYOUT CODE:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct Node{
    int val;
    struct Node *next;
}node;

int getCount(node* head){ 

} 

int getNumber(node* ptr,int size){
      
}

int main(){
    struct Node * head;
    struct Node * second;
    struct Node * third;

    head = (node*) malloc(sizeof(node));
    second = (node*) malloc(sizeof(node));
    third = (node*) malloc(sizeof(node));

    head->val = 2;     head->next = second;
    second->val = 4;   second->next = third;
    third->val = 6;    third->next = NULL;

    printf("%d",getNumber(head,3));

    return 0;
}

CodePudding user response:

You don't need math.h to do this (and baring floating point applications, you rarely need it whatsoever). Just walk the list, each time increasing the next node element added as a multiple of an increasing power of 10 (which can be accomplished multiple ways).

you don't need getCount. Nor do you need a size argument if the list is properly null-terminated. See below:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int val;
    struct Node *next;
} Node;

int getNumber(const Node *ptr)
{
    int value = 0;
    int exp = 1;
    while (ptr)
    {
        value  = ptr->val * exp;
        ptr = ptr->next;
        exp *= 10;
    }
    return value;
}

int main()
{
    int values[] = {2, 4, 6, 8, 1, 3, 5, 7};

    Node *head = NULL; // will hold the list head
    Node **pp = &head; // used to build the list

    for (size_t i = 0; i < sizeof values / sizeof *values;   i)
    {
        *pp = malloc(sizeof **pp);
        (*pp)->val = values[i];
        pp = &(*pp)->next;
    }
    *pp = NULL;

    // print the resulting number
    printf("%d\n", getNumber(head));

    // free the list;
    while (head)
    {
        void *p = head;
        head = head->next;
        free(p);
    }

    return 0;
}

Output

75318642

Note that this gets complicated when you have 'terms' that are are not in the set {0...9} (ex: negative terms, or values >= 10), but no specification or requirement was mentioned for that.

Recursion

Not that anyone sane would choose this, but it is a subtle recursive algorithm boiling down to a single ternary expression, in case you're curious:

int getNumber(const Node *ptr)
{
    return ptr ? ptr->val   10 * getNumber(ptr->next) : 0;
}

CodePudding user response:

So we are here passing the First Node of the Linked List Here with its size (Number Of Elements) we can use a function to do that too or we can pass the size manually
Function to get the size of the Linked List:

int getCount(node* head) 
{ 
    int count = 0; // Initialize count 
    node* curr = head; // Initialize current 
    while (curr != NULL) 
    { 
        count  ; 
        curr = curr->next; 
    } 
    return count; 
} 

Now we have the size of the Linked List
So now we have to complete the getNumber Funtion:
int getNumber(node* ptr,int size){
    int sum = 0;
    int power;
    int a=10;
    // i   i*10   i*100 .....
    for(int i=0; i < size; i  ){
        power = pow(a,i);
        sum = sum   ((ptr->val)*power);
        ptr = ptr->next;
    }
    return sum;
}

Here we are using a simple logic to get the number that is we have to multiply the first number from the linked list with the power of zero starting from zero
ie. From Example 1. we have
LinkedList: 3-> 4-> 2
Output: 243
we have to convert this into 243 So all we have to do is
3*(10^0) 4*(10^1) 3*(10^2) = 243
**Hope this Helps!
I was Looking for this code tommorow but I didn't find on the internet so I created one**
Final Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct Node{
    int val;
    struct Node *next;
}node;

int getCount(node* head){ 
    int count = 0; // Initialize count 
    node* curr = head; // Initialize current 
    while (curr != NULL) { 
        count  ; 
        curr = curr->next; 
    } 
    return count; 
} 

int getNumber(node* ptr,int size){
    int sum = 0;
    int power;
    int a=10;
    // i   i*10   i*100 .....
    for(int i=0; i < size; i  ){
        power = pow(a,i);
        sum = sum   ((ptr->val)*power);
        ptr = ptr->next;
    }
    return sum;
}

int main(){
    struct Node * head;
    struct Node * second;
    struct Node * third;

    head = (node*) malloc(sizeof(node));
    second = (node*) malloc(sizeof(node));
    third = (node*) malloc(sizeof(node));

    head->val = 2;     head->next = second;
    second->val = 4;   second->next = third;
    third->val = 6;    third->next = NULL;

    printf("%d",getNumber(head,getCount(head)));

    return 0;
}
  • Related