SlideShare a Scribd company logo
1 of 6
Download to read offline
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 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)
{
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;
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(" ");
//
//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

More Related Content

Similar to THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf

How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfshalins6
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdfsudhinjv
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docxajoy21
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxMatthPYNashd
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfarihantelehyb
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfconnellalykshamesb60
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfarchgeetsenterprises
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 

Similar to THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf (20)

How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 

More from fathimahardwareelect

Which one of the following is NOT a social trend that is currently af.pdf
Which one of the following is NOT a social trend that is currently af.pdfWhich one of the following is NOT a social trend that is currently af.pdf
Which one of the following is NOT a social trend that is currently af.pdffathimahardwareelect
 
Which of the following is normally a major activity of materials man.pdf
Which of the following is normally a major activity of materials man.pdfWhich of the following is normally a major activity of materials man.pdf
Which of the following is normally a major activity of materials man.pdffathimahardwareelect
 
What is the role of civil society in promoting democracySolutio.pdf
What is the role of civil society in promoting democracySolutio.pdfWhat is the role of civil society in promoting democracySolutio.pdf
What is the role of civil society in promoting democracySolutio.pdffathimahardwareelect
 
What mechanism causes plant ion-uptake to acidify soilSolution.pdf
What mechanism causes plant ion-uptake to acidify soilSolution.pdfWhat mechanism causes plant ion-uptake to acidify soilSolution.pdf
What mechanism causes plant ion-uptake to acidify soilSolution.pdffathimahardwareelect
 
using the knowledge about experiments that are related to plant g.pdf
using the knowledge about experiments that are related to plant g.pdfusing the knowledge about experiments that are related to plant g.pdf
using the knowledge about experiments that are related to plant g.pdffathimahardwareelect
 
True or False Justify your answer.The concept of flooding is alwa.pdf
True or False Justify your answer.The concept of flooding is alwa.pdfTrue or False Justify your answer.The concept of flooding is alwa.pdf
True or False Justify your answer.The concept of flooding is alwa.pdffathimahardwareelect
 
True or False. If H is a subgroup of the group G then H is one of it.pdf
True or False.  If H is a subgroup of the group G then H is one of it.pdfTrue or False.  If H is a subgroup of the group G then H is one of it.pdf
True or False. If H is a subgroup of the group G then H is one of it.pdffathimahardwareelect
 
Immunizations that the elderly need to receive on a yearly basis inc.pdf
Immunizations that the elderly need to receive on a yearly basis inc.pdfImmunizations that the elderly need to receive on a yearly basis inc.pdf
Immunizations that the elderly need to receive on a yearly basis inc.pdffathimahardwareelect
 
Question 11 Scarcity means O people need to use marginal analysis to .pdf
Question 11 Scarcity means O people need to use marginal analysis to .pdfQuestion 11 Scarcity means O people need to use marginal analysis to .pdf
Question 11 Scarcity means O people need to use marginal analysis to .pdffathimahardwareelect
 
Question 2 Sba Do you know something about the Statement Of Financ.pdf
Question 2 Sba Do you know something about the Statement Of Financ.pdfQuestion 2 Sba Do you know something about the Statement Of Financ.pdf
Question 2 Sba Do you know something about the Statement Of Financ.pdffathimahardwareelect
 
Question 1. give the CC++ code that defines an empty 5 element arra.pdf
Question 1. give the CC++ code that defines an empty 5 element arra.pdfQuestion 1. give the CC++ code that defines an empty 5 element arra.pdf
Question 1. give the CC++ code that defines an empty 5 element arra.pdffathimahardwareelect
 
List and explain all the components of company culture.Jackson, S..pdf
List and explain all the components of company culture.Jackson, S..pdfList and explain all the components of company culture.Jackson, S..pdf
List and explain all the components of company culture.Jackson, S..pdffathimahardwareelect
 
Please write 5-6 setences about each emotional intelligence capabili.pdf
Please write 5-6 setences about each emotional intelligence capabili.pdfPlease write 5-6 setences about each emotional intelligence capabili.pdf
Please write 5-6 setences about each emotional intelligence capabili.pdffathimahardwareelect
 
Please help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdfPlease help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdffathimahardwareelect
 
Negligence and intentional torts are subject to civil litigation as .pdf
Negligence and intentional torts are subject to civil litigation as .pdfNegligence and intentional torts are subject to civil litigation as .pdf
Negligence and intentional torts are subject to civil litigation as .pdffathimahardwareelect
 
Incuded within the major CPI basket groups are various government-cha.pdf
Incuded within the major CPI basket groups are various government-cha.pdfIncuded within the major CPI basket groups are various government-cha.pdf
Incuded within the major CPI basket groups are various government-cha.pdffathimahardwareelect
 
Identify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdf
Identify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdfIdentify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdf
Identify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdffathimahardwareelect
 
How many grams of solid NaCN have to be added to 1.4 L of water to d.pdf
How many grams of solid NaCN have to be added to 1.4 L of water to d.pdfHow many grams of solid NaCN have to be added to 1.4 L of water to d.pdf
How many grams of solid NaCN have to be added to 1.4 L of water to d.pdffathimahardwareelect
 
How does Warhol add to the semiotic statement of the readymadeS.pdf
How does Warhol add to the semiotic statement of the readymadeS.pdfHow does Warhol add to the semiotic statement of the readymadeS.pdf
How does Warhol add to the semiotic statement of the readymadeS.pdffathimahardwareelect
 
How do I add a ComboBox to this, underneath Enter Student ID tha.pdf
How do I add a ComboBox to this, underneath Enter Student ID tha.pdfHow do I add a ComboBox to this, underneath Enter Student ID tha.pdf
How do I add a ComboBox to this, underneath Enter Student ID tha.pdffathimahardwareelect
 

More from fathimahardwareelect (20)

Which one of the following is NOT a social trend that is currently af.pdf
Which one of the following is NOT a social trend that is currently af.pdfWhich one of the following is NOT a social trend that is currently af.pdf
Which one of the following is NOT a social trend that is currently af.pdf
 
Which of the following is normally a major activity of materials man.pdf
Which of the following is normally a major activity of materials man.pdfWhich of the following is normally a major activity of materials man.pdf
Which of the following is normally a major activity of materials man.pdf
 
What is the role of civil society in promoting democracySolutio.pdf
What is the role of civil society in promoting democracySolutio.pdfWhat is the role of civil society in promoting democracySolutio.pdf
What is the role of civil society in promoting democracySolutio.pdf
 
What mechanism causes plant ion-uptake to acidify soilSolution.pdf
What mechanism causes plant ion-uptake to acidify soilSolution.pdfWhat mechanism causes plant ion-uptake to acidify soilSolution.pdf
What mechanism causes plant ion-uptake to acidify soilSolution.pdf
 
using the knowledge about experiments that are related to plant g.pdf
using the knowledge about experiments that are related to plant g.pdfusing the knowledge about experiments that are related to plant g.pdf
using the knowledge about experiments that are related to plant g.pdf
 
True or False Justify your answer.The concept of flooding is alwa.pdf
True or False Justify your answer.The concept of flooding is alwa.pdfTrue or False Justify your answer.The concept of flooding is alwa.pdf
True or False Justify your answer.The concept of flooding is alwa.pdf
 
True or False. If H is a subgroup of the group G then H is one of it.pdf
True or False.  If H is a subgroup of the group G then H is one of it.pdfTrue or False.  If H is a subgroup of the group G then H is one of it.pdf
True or False. If H is a subgroup of the group G then H is one of it.pdf
 
Immunizations that the elderly need to receive on a yearly basis inc.pdf
Immunizations that the elderly need to receive on a yearly basis inc.pdfImmunizations that the elderly need to receive on a yearly basis inc.pdf
Immunizations that the elderly need to receive on a yearly basis inc.pdf
 
Question 11 Scarcity means O people need to use marginal analysis to .pdf
Question 11 Scarcity means O people need to use marginal analysis to .pdfQuestion 11 Scarcity means O people need to use marginal analysis to .pdf
Question 11 Scarcity means O people need to use marginal analysis to .pdf
 
Question 2 Sba Do you know something about the Statement Of Financ.pdf
Question 2 Sba Do you know something about the Statement Of Financ.pdfQuestion 2 Sba Do you know something about the Statement Of Financ.pdf
Question 2 Sba Do you know something about the Statement Of Financ.pdf
 
Question 1. give the CC++ code that defines an empty 5 element arra.pdf
Question 1. give the CC++ code that defines an empty 5 element arra.pdfQuestion 1. give the CC++ code that defines an empty 5 element arra.pdf
Question 1. give the CC++ code that defines an empty 5 element arra.pdf
 
List and explain all the components of company culture.Jackson, S..pdf
List and explain all the components of company culture.Jackson, S..pdfList and explain all the components of company culture.Jackson, S..pdf
List and explain all the components of company culture.Jackson, S..pdf
 
Please write 5-6 setences about each emotional intelligence capabili.pdf
Please write 5-6 setences about each emotional intelligence capabili.pdfPlease write 5-6 setences about each emotional intelligence capabili.pdf
Please write 5-6 setences about each emotional intelligence capabili.pdf
 
Please help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdfPlease help me find my errors. I am lost as I know what the problem.pdf
Please help me find my errors. I am lost as I know what the problem.pdf
 
Negligence and intentional torts are subject to civil litigation as .pdf
Negligence and intentional torts are subject to civil litigation as .pdfNegligence and intentional torts are subject to civil litigation as .pdf
Negligence and intentional torts are subject to civil litigation as .pdf
 
Incuded within the major CPI basket groups are various government-cha.pdf
Incuded within the major CPI basket groups are various government-cha.pdfIncuded within the major CPI basket groups are various government-cha.pdf
Incuded within the major CPI basket groups are various government-cha.pdf
 
Identify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdf
Identify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdfIdentify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdf
Identify major groupings within the Lophotrochozoa and Ecdy gg g soz.pdf
 
How many grams of solid NaCN have to be added to 1.4 L of water to d.pdf
How many grams of solid NaCN have to be added to 1.4 L of water to d.pdfHow many grams of solid NaCN have to be added to 1.4 L of water to d.pdf
How many grams of solid NaCN have to be added to 1.4 L of water to d.pdf
 
How does Warhol add to the semiotic statement of the readymadeS.pdf
How does Warhol add to the semiotic statement of the readymadeS.pdfHow does Warhol add to the semiotic statement of the readymadeS.pdf
How does Warhol add to the semiotic statement of the readymadeS.pdf
 
How do I add a ComboBox to this, underneath Enter Student ID tha.pdf
How do I add a ComboBox to this, underneath Enter Student ID tha.pdfHow do I add a ComboBox to this, underneath Enter Student ID tha.pdf
How do I add a ComboBox to this, underneath Enter Student ID tha.pdf
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

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