Deprecated: Implicit conversion from float-string "1534009346.689" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1534009346.689" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1534009346.689" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1534009346.689" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1534009346.689" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1534014104.609" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1534014104.609" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1534014104.609" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1534014104.609" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1534014104.609" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1534015171.871" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1534015171.871" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1534015171.871" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1534015171.871" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1534015171.871" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1534133978.313" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1534133978.313" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1534133978.313" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1534133978.313" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1534133978.313" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594
Programming in C: GO Classroom(Linked List)
502 views
0 votes
0 votes

Is my analysis correct?

I analyze code that if it will take 

1->2->3 and 4->1->2 as input

then output will be reverse of 5->3->2 i.e. 2->3->5

Why code is showing runtime error?

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
	char digit;
	struct node * next;
};
struct node * makenode(char digit)
{
	struct node *A;
	//int count=0;
	A=NULL;
	struct node *temp=(struct node*)malloc(sizeof(struct node));
	//while(((temp->next)==NULL) && (count<3)){
		//temp=(struct node*)malloc(sizeof(struct node));
		temp->next=digit;
		temp->next=NULL;
		A=temp;
		//count++;
	}
		return 0;
}
 
void printList(struct node * list)
{
	printf("%d->",*list);
}
struct node * readNum()
{
	char c;
	struct node * head, * trav;
	scanf("%c", &c);
	head = trav = makenode(c - '0');//head node creation here
	do
	{
		scanf("%c", &c);
		if(!isdigit(c)) break;
		trav -> next = makenode(c - '0');//more nodes are created and as input is in char, 48 
//subtracted from each node
		trav = trav -> next;
 
	}while(1);
	return head;
}
struct node* reverse(struct node * list, struct node **head)
{
	if(list -> next == NULL)
	{
		*head = list;//head of reversed list;
		return list;
	}
	reverse(list -> next, head) -> next = list;
	list -> next = NULL;
	return list;
}
struct node * simultrav(struct node* trav1, struct node * trav2, char carry)
{
	struct node * temp;
	char sum = carry;
	if(trav1 && trav2)
	{
		sum += trav1 -> digit + trav2 -> digit;
	}
	else if (trav1)
	{
		sum += trav1 -> digit;
	}	
	else if (trav2)
	{
		sum += trav2 -> digit;
	}	
	carry = sum /10;
	sum = sum % 10;
	temp = makenode(sum);
 
	if(trav1  && trav2)
	{
		temp -> next = simultrav(trav1-> next, trav2 -> next, carry);
	}
	else if(trav1)
	{
		temp -> next =  simultrav(trav1-> next, trav2, carry);
	}
	else if(trav2)
	{
		temp -> next =  simultrav(trav1, trav2 -> next, carry);
	}
	else 
	{
		if(carry)
		{
			temp -> next = makenode(carry);
		}
		else
			temp -> next = NULL;
	}
	return temp;
}
 
int main()
{
	struct node * num1, * num2, *rnum1, *rnum2, *sum, *rsum;
	printf("Enter the first number: ");
	num1 = readNum();
	printf("Enter the second number: ");
	num2 = readNum();
	printf("\n");
	reverse( num1, &rnum1);
	reverse( num2, &rnum2);
	rsum = simultrav(rnum1, rnum2, 0);
	reverse( rsum, &sum);
	printf("The sum is : ");
	printList(sum);
	printf("\n");
 return 0;
}

 

Please log in or register to answer this question.

Related questions


Deprecated: Implicit conversion from float-string "1523705434.646" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1523705434.646" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1523705434.646" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1523705434.646" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1533884788.122" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1533884788.122" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1533884788.122" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1533884788.122" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1539748834.459" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1539748834.459" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1539748834.459" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1539748834.459" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803
426
views
0 answers
0 votes
ankitgupta.1729 asked Apr 14, 2018
426 views
I am not getting the problem which is given below. Please helpMatrix transposition must be a familiar task. This problem is a generalization of it. We can assume matrix ...
451
views
0 answers
0 votes
Bhagyashree Mukherje asked Aug 10, 2018
451 views
https://gatecse.in/data_types_operators_in_c/ Q7. #include <stdio.h>int main(){ char *p = "Hello World"; char q[] = "Hello World"; printf("%zd %zd", sizeof p, s...
468
views
1 answers
0 votes
sidjha57 asked Jan 18, 2022
468 views
Why am I not able to access the course content that is to enroll in the courses on go classroom?It says that it has been ended on 21st July is there any other way to acce...
241
views
0 answers
0 votes
sakharam asked Oct 17, 2018
241 views
What is Squareroot(L) and Square(L)?