## sleepybug -4

i m trying to do this program to get digits from a number using linked lists..but don't know something is wrong.a logical error:-/
at the place of actual output it shows nothing clear blank ecept the message.so here's the code:

``````//DigitNode.cpp
# define NULL 0

class DigiNode
{
private:
DigiNode* Front;
int digit;
public:
DigiNode()
{
digit=NULL;
Front=NULL;
}
DigiNode(int digit)
{
this->digit=digit;
Front=NULL;
}
void SetFront(DigiNode* Front)
{
this->Front=Front;
}
DigiNode* GetFront()
{
return Front;
}
void SetDigit(int digit)
{
this->digit=digit;
}
int GetDigit()
{
return digit;
}
};``````

``````# include "DigitNode.cpp"
# define NULL 0
# include <cmath>

{
private:
public:
{
}
bool Empty()
{
return true;
else
return false;
}
void DigiNum(int x)
{
int r, q;

q=x;
while(q>0)
{
r=q%10;
q/=10;
CurNode=new DigiNode();
CurNode->SetDigit(r);

{
return;
}
else
{
}
}
}
int ShowDigit()
{
if(Empty())
throw "END";
else
{
int i=CurNode->GetDigit();
delete CurNode;
return i;
}
}
};``````

now the main:

``````# include <iostream>
using namespace std;
void main()
{
int x;

cout<<"\nInput The Number:";
cin>>x;
D.DigiNum(x);

cout<<"\nFollowing Are Your Input Elements In A Stack:\n";
while(true)
{
try
{
cout<<D.ShowDigit()<<", ";
}
catch (char *msg)
{
cout<<msg<<'\n';
break;
}
}
}``````

## firstPerson 761

>>

``````bool Empty()
{
return true;
else
return false;
}``````

condense that down to :

``bool isEmpty(){ return Head->GetFront == NULL; }``

>> code in DigiNode, "DigiNode* Front". That name , Front is misleading. Consider renaming it nextNode or something similar.

This part of the code from DigiNum function :

``````Head->SetFront(CurNode);

Is wrong in a sense. After it gets executed, the Head is no longer the head, that is, it no longer points to the first element in the list. Rather
it acts like a broke down tail. You should change your function name
to something better. DigiNum is basically, getting a number and extracting each digit and inputting it into the list. So in you code :

``````void DigiNum(int x)
{
int r, q;

q=x;
while(q>0)
{
r=q%10;
q/=10;
CurNode=new DigiNode();
CurNode->SetDigit(r);

{
return;
}
else
{
}
}
}``````

Thats what it essentially does. But there are minor mistakes. You
got the part correct, where you extract the last digit of a number
in each iteration. The only part incorrect is when you add the number
to the list.

I would suggest doing something like this :

``````void extractDigitsIntoList(int num)
{
int lastDigit = -1;

while(num > 0)
{
lastDigit = num % 10;//get the last digit
num /= 10; //remove the old last digit

DigiNode *newNode = new DigiNode(lastDigit);

}
else
{