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;
	}
};

DigitLink.cpp

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

class DigiLink
{
private:
	DigiNode *Head, *CurNode;
public:
	DigiLink()
	{
		Head=CurNode=NULL;
	}
	bool Empty()
	{
		if(Head->GetFront()==NULL)
			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);
			
			if(Head==NULL)
			{
				Head=CurNode;
				return;
			}
			else
			{
				Head->SetFront(CurNode);
				Head=CurNode;
			}
		}
	}
	int ShowDigit()
	{
		if(Empty())
			throw "END";
		else
		{
			CurNode=Head;
			Head=Head->GetFront();
			int i=CurNode->GetDigit();
			delete CurNode;
			return i;
		}
	}
};

now the main:

# include <iostream>
# include "DiGiTLinK.cpp"
using namespace std;
void main()
{
	DigiLink D;
	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;
		}
	}
}

>>

bool Empty()
{
    if(Head->GetFront()==NULL)
        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);
   Head=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);
			
			if(Head==NULL)
			{
				Head=CurNode;
				return;
			}
			else
			{
				Head->SetFront(CurNode);
				Head=CurNode;
			}
		}
	}

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);
			
			if(Head == NULL){
				Head = newNode;				
			}
			else
			{
                              DigiNode *pos = Head;
                          // move pos until the end of the list
                              while(pos.nextNode != null) pos = pos.nextNode;
                    //now add the new node
                    pos.nextNode = newNode;
	}
		}
	}

I can't assure you the above is 100% correct, but it should give
you the basic idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.