THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED
HELP FIXING THIS CODE?
THIS IS THE PROBLEM
Write a program that allows the user to enter two positive integers and outputs their sum. Sounds
simple? Here\'s the catch: The numbers may be any size! They may be way too large to store in a
single variable. You may not assume any particular maximum number of digits.
HERE IS THE CODE
#include
#include
struct node /* Linked list node*/
{
int data;
struct node* next;
};
struct node *newNode(int data) /* Function to create a new node with giving the data*/
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void push(struct node** head_ref, int new_data) /* Function to insert a node at the beginning of
the Doubly Linked List*/
{
struct node* new_node = newNode(new_data); /*allocate a node*/
new_node->next = (*head_ref); /* link the old list off to the new node*/
(*head_ref) = new_node; /*move the head to point to the new node*/
}
/* Adding the contents of two linked lists and return the head node of resultant list*/
struct node* addTwoLists (struct node* first, struct node* second)
{
struct node* res = NULL; /* res is head node of the resultant list*/
struct node *temp, *prev = NULL;
int carry = 0, sum;
while (first != NULL || second != NULL) /*while both the lists exists*/
{
/* Calculate value of next digit in resultant list.
// The next digit is sum of following things
// (i) Carry
// (ii) Next digit of first list (if there is a next digit)
// (ii) Next digit of second list (if there is a next digit)*/
sum = carry + (first? first->data: 0) + (second? second->data: 0);
carry = (sum >= 10)? 1 : 0; /* update carry for next calulation*/
sum = sum % 10; /*update sum if it is greater than 10*/
temp = newNode(sum); /*Create a new node with sum as data*/
if(res == NULL) /*if this is the first node then set it as head of the resultant list*/
res = temp;
else /* If this is not the first node then connect it to the rest.*/
prev->next = temp;
prev = temp; /* Set prev for next insertion*/
if (first) first = first->next; /* Move first pointers to next node*/
if (second) second = second->next; /*Move second pointer to the next node*/
}
if (carry > 0)
temp->next = newNode(carry);
return res; /* return head of the resultant list*/
}
void printList(struct node *node) /* function to print a linked list*/
{
while(node != NULL)
{
printf(\"%d \", node->data);
node = node->next;
}
printf(\"\ \");
}
int main(void) /* program to test above function*/
{
struct node* res = NULL;
struct node* first = NULL;
struct node* second = NULL;
printf(\"Enter first positive integer: \");
scanf(\"%d\",&first);
printList(first);
printf(\"Enter second positive integer: \");
scanf(\"%d\",&second);
printList(second);
/*Add the two lists and see result*/
res = addTwoLists(first, second);
printf(\"Sum is: %d\");
printList(res);
return 0;
}
Solution
#include
#include
typedef struct Node
{
int da.
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
1. THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED
HELP FIXING THIS CODE?
THIS IS THE PROBLEM
Write a program that allows the user to enter two positive integers and outputs their sum. Sounds
simple? Here's the catch: The numbers may be any size! They may be way too large to store in a
single variable. You may not assume any particular maximum number of digits.
HERE IS THE CODE
#include
#include
struct node /* Linked list node*/
{
int data;
struct node* next;
};
struct node *newNode(int data) /* Function to create a new node with giving the data*/
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void push(struct node** head_ref, int new_data) /* Function to insert a node at the beginning of
the Doubly Linked List*/
{
struct node* new_node = newNode(new_data); /*allocate a node*/
new_node->next = (*head_ref); /* link the old list off to the new node*/
(*head_ref) = new_node; /*move the head to point to the new node*/
}
/* Adding the contents of two linked lists and return the head node of resultant list*/
struct node* addTwoLists (struct node* first, struct node* second)
{
struct node* res = NULL; /* res is head node of the resultant list*/
2. struct node *temp, *prev = NULL;
int carry = 0, sum;
while (first != NULL || second != NULL) /*while both the lists exists*/
{
/* Calculate value of next digit in resultant list.
// The next digit is sum of following things
// (i) Carry
// (ii) Next digit of first list (if there is a next digit)
// (ii) Next digit of second list (if there is a next digit)*/
sum = carry + (first? first->data: 0) + (second? second->data: 0);
carry = (sum >= 10)? 1 : 0; /* update carry for next calulation*/
sum = sum % 10; /*update sum if it is greater than 10*/
temp = newNode(sum); /*Create a new node with sum as data*/
if(res == NULL) /*if this is the first node then set it as head of the resultant list*/
res = temp;
else /* If this is not the first node then connect it to the rest.*/
prev->next = temp;
prev = temp; /* Set prev for next insertion*/
if (first) first = first->next; /* Move first pointers to next node*/
if (second) second = second->next; /*Move second pointer to the next node*/
}
if (carry > 0)
temp->next = newNode(carry);
return res; /* return head of the resultant list*/
}
void printList(struct node *node) /* function to print a linked list*/
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
3. printf(" ");
}
int main(void) /* program to test above function*/
{
struct node* res = NULL;
struct node* first = NULL;
struct node* second = NULL;
printf("Enter first positive integer: ");
scanf("%d",&first);
printList(first);
printf("Enter second positive integer: ");
scanf("%d",&second);
printList(second);
/*Add the two lists and see result*/
res = addTwoLists(first, second);
printf("Sum is: %d");
printList(res);
return 0;
}
Solution
#include
#include
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
}node;
void push(node *head, int data)
{
/* Iterate through the list till we encounter the last node.*/
while(head->next!=NULL)
4. {
head = head -> next;
}
/* Allocate memory for the new node and put data in it.*/
head->next = (node *)malloc(sizeof(node));
(head->next)->prev = head;
head = head->next;
head->data = data;
head->next = NULL;
}
/* Adding the contents of two linked lists and return the head node of resultant list*/
void print(node *printnode)
{
if(printnode==NULL)
{
return;
}
printf("%d ",printnode->data);
print(printnode->next);
}
int main()
{
node *first,*temp,*second,*res,*stemp;
first = (node *)malloc(sizeof(node));
temp = first;
temp -> next = NULL;
temp -> prev = NULL;
//second list
second= (node *)malloc(sizeof(node));
stemp=second;
stemp -> next = NULL;
stemp -> prev = NULL;
5. printf(" 1.Insert ");
printf("2 Print ");
printf("3 quit");
while(1)
{
int choice;
printf("enter the choice");
scanf("%d",&choice);
if(choice==1)
{
int data;
printf("enter the data to insert in first list");
scanf("%d",&data);
push(first,data);
int data1;
printf("enter the data to insert in second list");
scanf("%d",&data1);
push(second,data1);
// printf("enter the choice");
}
else if(choice==2)
{
printf("The first list is ");
print(first->next);
printf(" ");
printf("The second list is ");
print(second->next);
printf(" ");
//
6. //printf("enter the choice");
}
//break;
else if(choice==3)
{
break;
}
}
}
output
1.Insert
2 Print
3 quitenter the choice
1
enter the data to insert in first list
10
enter the data to insert in second list
20
enter the choice
2
The first list is 10
The second list is 20
enter the choice
1
enter the data to insert in first list
30
enter the data to insert in second list
40
enter the choice2
The first list is 10 30
The second list is 20 40
enter the choice
3