Storing a number as digits in a linked list

  • Thread starter Raghav Gupta
  • Start date
  • Tags
    List
In summary: Alt() may be working for odd number of digits, but I'm getting an invalid memory access. I added a check to avoid looking for NULL->next:#include<iostream.h>#include<conio.h>class Digit{ int data; Digit*next,*head; public: Digit() { head=NULL; } void read(); void delAlt(); void display(); }; void Digit::read(){
  • #1
Raghav Gupta
1,011
76

Homework Statement


Create a class named digit, which stores a node holding a digit. Include appropriate data

members and member functions to do the following: (a) Read a long number (datatype is double)

from user and store each digit of the long number as a node value. For example, if the input number

is 8475483 then list is 8->4->7->5->4->8->3. (b) Delete alternate digits, get back the number and

display. For example for the list in (a) the result of delete alternate digits is 8->7->4->3 and

display.

Homework Equations


NA

The Attempt at a Solution


I am not able to extract digits as % is not valid for double datatype. Otherwise the program runs fine when I change the datatype to int and value is in range of int.
C:
#include<iostream.h>
#include<conio.h>

class Digit{
    int data;
    Digit*next,*head;
    public:
    Digit()
    {
    head=NULL;
    }
    void read();
    void delAlt();
    void display();
      };

    void Digit::read(){
    Digit*node=new Digit;
    int num;
    cout<<"Enter number:" ;
    cin>>num;
    node->data=num%10;
    node->next=NULL;
    head=node;
    num=num/10;
    while(num>0){
    node=new Digit;
    node->data=num%10;
    node->next=head;
    head=node;
    num=num/10;}}

    void Digit:: delAlt(){
    Digit*temp,*temp1;
    temp1=head;
    while(temp1!=NULL){
    temp=temp1->next;
    temp1->next=temp->next;
    delete temp;
    temp1=temp1->next;}}

    void Digit::display(){
    Digit*temp=head;
    while(temp!=NULL){
    cout<<temp->data;
    temp=temp->next;}}

    void main(){
    clrscr();
    Digit d;
    d.read();
    d.delAlt();
    d.display();
    getch();
         }
 
Physics news on Phys.org
  • #2
You should be able to use fmod() for doubles. Include < math.h > . Are you supposed to handle numbers with fractional components such as 123.456 or negative numbers?
 
  • #3
rcgldr said:
You should be able to use fmod() for doubles.
I used fmod() but then the output is not coming as required.
Output is like this
000000000
000000874 something like that.
 
  • #4
After using fmod(), convert the remainder to an integer.
 
  • #5
rcgldr said:
After using fmod(), convert the remainder to an integer.
I modified the code to,
double num;
and node->data=(int) fmod(num, 10) ;
but the output is leading with 0. the end digits are fine in this output:
00000000
000008743
 
  • #6
Why is the code calling d.delAlt() before doing d.display()?
 
  • #7
rcgldr said:
Why is the code calling d.delAlt() before doing d.display()?
Because it is not asked in question to display before deleting alternate nodes.
If I input number as 8475483, and display before delAlt and also after delAlt, then output is like this
0000000000
0000000000
0084754830
0000000000
0087430000
 
  • #8
Follow up, while (num > 0) isn't going to work for doubles, as you'll have a fraction part after each divide. The following should work to fix this (assuming user enters integer type numbers), you'll need to make this change in two places:

Code:
        // ...
        num -= node->data;   // subtract the low order digit from num
        num /= 10.;          // divide num by 10.

Also delAlt() has a problem if an odd number of digits is entered.
 
Last edited:
  • Like
Likes Raghav Gupta
  • #9
rcgldr said:
Follow up, while (num > 0) isn't going to work for doubles, as you'll have a fraction part after each divide. The following should work to fix this (assuming user enters integer type numbers), you'll need to make this change in two places:

Code:
        // ...
        num -= node->data;   // subtract the low order digit from num
        num /= 10.;          // divide num by 10.

Also delAlt() has a problem if an odd number of digits is entered.

Thanks it is working fine now. delAlt() is working fine for me even when there are odd number of digits.
For eg:
input number: 8475483 (odd number of digits)
Output: 8475483 (number as separate digits in link list)
8743 ( deleting alternate nodes)
 
  • #10
Here is an alternate way of doing the divide part:

Code:
	num = floor(num/10.);

delAlt() may be working for odd number of digits, but I'm getting an invalid memory access. I added a check to avoid looking for NULL->next:

Code:
void Digit::delAlt(){
    Digit*temp,*temp1;
    temp1=head;
    while(temp1 != NULL){
        temp=temp1->next;
        if(temp == NULL){
			break;}
        temp1->next=temp->next;
        delete temp;
        temp1=temp1->next;}
}
 
Last edited:
  • #11
rcgldr said:
Here is an alternate way of doing the divide part:

Code:
    num = floor(num/10.);
Thanks, I understand the floor function aka greatest integer function.
delAlt() may be working for odd number of digits, but I'm getting an invalid memory access. I added a check to avoid looking for NULL->next:
In Turbo c++ it was not giving a warning or error. I think it takes NULL->next as NULL in turbo c++
 

1. How does storing a number as digits in a linked list work?

Storing a number as digits in a linked list involves breaking down the number into its individual digits and storing each digit as a node in the linked list. The first node will represent the ones place, the second node the tens place, and so on.

2. What are the advantages of storing a number as digits in a linked list?

Storing a number as digits in a linked list allows for efficient storage of large numbers, as the size of the linked list can dynamically adjust to the number of digits. It also makes it easier to perform mathematical operations on the number, as each digit can be accessed and manipulated separately.

3. How do you convert a number stored as digits in a linked list back to its integer form?

To convert a number stored as digits in a linked list back to its integer form, you would iterate through the linked list and multiply each digit by its corresponding place value. Then, you would add all of the products together to get the integer value.

4. Can a negative number be stored as digits in a linked list?

Yes, a negative number can be stored as digits in a linked list. One method is to use a separate boolean variable to indicate the sign of the number, and then store the absolute value of the number in the linked list.

5. What is the time complexity for accessing and manipulating a number stored as digits in a linked list?

The time complexity for accessing and manipulating a number stored as digits in a linked list is O(n), where n is the number of digits in the number. This is because you would need to traverse the linked list to access or manipulate each digit.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top