- #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();
}