Support Infinite Precision Arithmetic & Store a Number as a List
of Digits
#include
#include
#include
#include
struct node
{
int num;
struct node *next;
};
int feednumber(struct node **);
void release(struct node **);
void display(struct node *);
int main()
{
struct node *p = NULL;
int pcount = 0, qcount = 0;
clrscr();
printf("Enter number of any length\n");
pcount = feednumber(&p);
printf("Number of integers entered are: %d\n", pcount);
printf("Displaying the number entered:\n");
display(p);
release(&p);
return 0;
}
int feednumber(struct node **head)
{
char ch, dig;
int count = 0;
struct node *temp, *rear = NULL;
ch = getchar();
while (ch != '\n')
{
dig = atoi(&ch);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = dig;
temp->next = NULL;
count++;
if ((*head) == NULL)
{
*head = temp;
rear = temp;
}
else
{
rear->next = temp;
rear = rear->next;
}
ch = getchar();
}
return count;
}
void display (struct node *head)
{
while (head != NULL)
{
printf("%d", head->num);
head = head->next;
}
printf("\n");
}
void release (struct node **head)
{
struct node *temp = *head;
while ((*head) != NULL)
{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}
Output
Enter number of any length
932429842394820948234948098391830283192193818310398291830131209
Number of integers entered are: 63
Displaying the number entered:
932429842394820948234948098391830283192193818310398291830131209
No comments:
Post a Comment