SlideShare a Scribd company logo
1 of 7
Download to read offline
Please find the answer to the above problem as follows:-
/* Program to insert in a sorted list */
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
void sortedDelete(struct node** head_ref, int number)
{
struct node *temp = *head_ref;
struct node *prev = NULL;
if(*head_ref != NULL && (*head_ref)->data == number){
*head_ref = (*head_ref)->next;
return;
}
while(temp != NULL)
{
if(temp->data == number){
prev->next = temp->next;
free(temp);
return;
}
prev = temp;
temp = temp->next;
}
}
/* function to insert a new_node in a list. Note that this
* function expects a pointer to head_ref as this can modify the
* head of the input linked list (similar to push())*/
void sortedInsert(struct node** head_ref, struct node* new_node)
{
struct node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
if(*head_ref != NULL&&(*head_ref)->data==new_node->data){
return;
}
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
if(current->next!=NULL && current->next->data == new_node->data){
return;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */
/* A utility function to create a new node */
struct node *newNode(int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
if(temp == NULL)
printf(" ");
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Drier program to test count function*/
int main(int argc, char *argv[])
{
/* Start with the empty list */
struct node* head = NULL;
struct node *new_node;
FILE *fp;
fp = fopen(argv[1], "r+");
if(!fp){
printf("error");
return 1;
}
char op = ' ';
int number = 0;
while(fscanf(fp, "%ct%d ", &op, &number)!=EOF){
if(op=='i'){
new_node = newNode(number);
sortedInsert(&head, new_node);
}else if(op=='d'){
sortedDelete(&head, number);
}else{
printf("error");
return;
}
}
printList(head);
printf(" ");
return 0;
}
Solution
Please find the answer to the above problem as follows:-
/* Program to insert in a sorted list */
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
void sortedDelete(struct node** head_ref, int number)
{
struct node *temp = *head_ref;
struct node *prev = NULL;
if(*head_ref != NULL && (*head_ref)->data == number){
*head_ref = (*head_ref)->next;
return;
}
while(temp != NULL)
{
if(temp->data == number){
prev->next = temp->next;
free(temp);
return;
}
prev = temp;
temp = temp->next;
}
}
/* function to insert a new_node in a list. Note that this
* function expects a pointer to head_ref as this can modify the
* head of the input linked list (similar to push())*/
void sortedInsert(struct node** head_ref, struct node* new_node)
{
struct node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
if(*head_ref != NULL&&(*head_ref)->data==new_node->data){
return;
}
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
if(current->next!=NULL && current->next->data == new_node->data){
return;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */
/* A utility function to create a new node */
struct node *newNode(int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
if(temp == NULL)
printf(" ");
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Drier program to test count function*/
int main(int argc, char *argv[])
{
/* Start with the empty list */
struct node* head = NULL;
struct node *new_node;
FILE *fp;
fp = fopen(argv[1], "r+");
if(!fp){
printf("error");
return 1;
}
char op = ' ';
int number = 0;
while(fscanf(fp, "%ct%d ", &op, &number)!=EOF){
if(op=='i'){
new_node = newNode(number);
sortedInsert(&head, new_node);
}else if(op=='d'){
sortedDelete(&head, number);
}else{
printf("error");
return;
}
}
printList(head);
printf(" ");
return 0;
}

More Related Content

Similar to Please find the answer to the above problem as follows- Program.pdf

C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfarjunenterprises1978
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
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
 
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
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdfshanki7
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfkavithaarp
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhvasavim9
 
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
 
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
 
c++ Computational Complexity filling in the following three .pdf
c++ Computational Complexity filling in the  following three .pdfc++ Computational Complexity filling in the  following three .pdf
c++ Computational Complexity filling in the following three .pdfamitbagga0808
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxnoreendchesterton753
 
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
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfudit652068
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfsales98
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfarpitcomputronics
 

Similar to Please find the answer to the above problem as follows- Program.pdf (20)

C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.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
 
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
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdf
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdf
 
C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
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
 
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
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
c++ Computational Complexity filling in the following three .pdf
c++ Computational Complexity filling in the  following three .pdfc++ Computational Complexity filling in the  following three .pdf
c++ Computational Complexity filling in the following three .pdf
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
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
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdf
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
write recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdfwrite recursive function that calculates and returns the length of a.pdf
write recursive function that calculates and returns the length of a.pdf
 

More from angelfragranc

C ) common in nature, but commonly used psychological measures rare.pdf
 C ) common in nature, but commonly used psychological measures rare.pdf C ) common in nature, but commonly used psychological measures rare.pdf
C ) common in nature, but commonly used psychological measures rare.pdfangelfragranc
 
Question evidence 1. Comparison of DNA sequences among single-ce.pdf
    Question evidence   1. Comparison of DNA sequences among single-ce.pdf    Question evidence   1. Comparison of DNA sequences among single-ce.pdf
Question evidence 1. Comparison of DNA sequences among single-ce.pdfangelfragranc
 
The purpose of alchol (ethanol) is to dissolve io.pdf
                     The purpose of alchol (ethanol) is to dissolve io.pdf                     The purpose of alchol (ethanol) is to dissolve io.pdf
The purpose of alchol (ethanol) is to dissolve io.pdfangelfragranc
 
The components may not separate properly, because.pdf
                     The components may not separate properly, because.pdf                     The components may not separate properly, because.pdf
The components may not separate properly, because.pdfangelfragranc
 
The answer would be 5 because Dehydration in a .pdf
                     The answer would be 5 because  Dehydration in a .pdf                     The answer would be 5 because  Dehydration in a .pdf
The answer would be 5 because Dehydration in a .pdfangelfragranc
 
Nucleophile is any negative ion or any neutral mo.pdf
                     Nucleophile is any negative ion or any neutral mo.pdf                     Nucleophile is any negative ion or any neutral mo.pdf
Nucleophile is any negative ion or any neutral mo.pdfangelfragranc
 
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdf
                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdfangelfragranc
 
it is a salt formed by KOH and HCl it is a neutra.pdf
                     it is a salt formed by KOH and HCl it is a neutra.pdf                     it is a salt formed by KOH and HCl it is a neutra.pdf
it is a salt formed by KOH and HCl it is a neutra.pdfangelfragranc
 
Cultural competence refers to an ability to inter.pdf
                     Cultural competence refers to an ability to inter.pdf                     Cultural competence refers to an ability to inter.pdf
Cultural competence refers to an ability to inter.pdfangelfragranc
 
Conformers can also be named as conformational is.pdf
                     Conformers can also be named as conformational is.pdf                     Conformers can also be named as conformational is.pdf
Conformers can also be named as conformational is.pdfangelfragranc
 
Cl S Se Solution Cl .pdf
                     Cl  S  Se  Solution                     Cl .pdf                     Cl  S  Se  Solution                     Cl .pdf
Cl S Se Solution Cl .pdfangelfragranc
 
benzene sulphonic acid - SO3H on benzene ring .pdf
                     benzene sulphonic acid  - SO3H on benzene ring   .pdf                     benzene sulphonic acid  - SO3H on benzene ring   .pdf
benzene sulphonic acid - SO3H on benzene ring .pdfangelfragranc
 
The HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdfThe HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdfangelfragranc
 
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdfThe genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdfangelfragranc
 
ans D because NO2 has higher priority and should.pdf
                     ans D because NO2 has higher priority and should.pdf                     ans D because NO2 has higher priority and should.pdf
ans D because NO2 has higher priority and should.pdfangelfragranc
 
The objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdfThe objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdfangelfragranc
 
A. He has a smaller radius than H because He has .pdf
                     A. He has a smaller radius than H because He has .pdf                     A. He has a smaller radius than H because He has .pdf
A. He has a smaller radius than H because He has .pdfangelfragranc
 
standard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdfstandard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdfangelfragranc
 
A transducer is a device, usually electrical, ele.pdf
                     A transducer is a device, usually electrical, ele.pdf                     A transducer is a device, usually electrical, ele.pdf
A transducer is a device, usually electrical, ele.pdfangelfragranc
 

More from angelfragranc (20)

C ) common in nature, but commonly used psychological measures rare.pdf
 C ) common in nature, but commonly used psychological measures rare.pdf C ) common in nature, but commonly used psychological measures rare.pdf
C ) common in nature, but commonly used psychological measures rare.pdf
 
Question evidence 1. Comparison of DNA sequences among single-ce.pdf
    Question evidence   1. Comparison of DNA sequences among single-ce.pdf    Question evidence   1. Comparison of DNA sequences among single-ce.pdf
Question evidence 1. Comparison of DNA sequences among single-ce.pdf
 
The purpose of alchol (ethanol) is to dissolve io.pdf
                     The purpose of alchol (ethanol) is to dissolve io.pdf                     The purpose of alchol (ethanol) is to dissolve io.pdf
The purpose of alchol (ethanol) is to dissolve io.pdf
 
The components may not separate properly, because.pdf
                     The components may not separate properly, because.pdf                     The components may not separate properly, because.pdf
The components may not separate properly, because.pdf
 
The answer would be 5 because Dehydration in a .pdf
                     The answer would be 5 because  Dehydration in a .pdf                     The answer would be 5 because  Dehydration in a .pdf
The answer would be 5 because Dehydration in a .pdf
 
SO3 Sol.pdf
                     SO3                                       Sol.pdf                     SO3                                       Sol.pdf
SO3 Sol.pdf
 
Nucleophile is any negative ion or any neutral mo.pdf
                     Nucleophile is any negative ion or any neutral mo.pdf                     Nucleophile is any negative ion or any neutral mo.pdf
Nucleophile is any negative ion or any neutral mo.pdf
 
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdf
                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdf
 
it is a salt formed by KOH and HCl it is a neutra.pdf
                     it is a salt formed by KOH and HCl it is a neutra.pdf                     it is a salt formed by KOH and HCl it is a neutra.pdf
it is a salt formed by KOH and HCl it is a neutra.pdf
 
Cultural competence refers to an ability to inter.pdf
                     Cultural competence refers to an ability to inter.pdf                     Cultural competence refers to an ability to inter.pdf
Cultural competence refers to an ability to inter.pdf
 
Conformers can also be named as conformational is.pdf
                     Conformers can also be named as conformational is.pdf                     Conformers can also be named as conformational is.pdf
Conformers can also be named as conformational is.pdf
 
Cl S Se Solution Cl .pdf
                     Cl  S  Se  Solution                     Cl .pdf                     Cl  S  Se  Solution                     Cl .pdf
Cl S Se Solution Cl .pdf
 
benzene sulphonic acid - SO3H on benzene ring .pdf
                     benzene sulphonic acid  - SO3H on benzene ring   .pdf                     benzene sulphonic acid  - SO3H on benzene ring   .pdf
benzene sulphonic acid - SO3H on benzene ring .pdf
 
The HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdfThe HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdf
 
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdfThe genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdf
 
ans D because NO2 has higher priority and should.pdf
                     ans D because NO2 has higher priority and should.pdf                     ans D because NO2 has higher priority and should.pdf
ans D because NO2 has higher priority and should.pdf
 
The objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdfThe objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdf
 
A. He has a smaller radius than H because He has .pdf
                     A. He has a smaller radius than H because He has .pdf                     A. He has a smaller radius than H because He has .pdf
A. He has a smaller radius than H because He has .pdf
 
standard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdfstandard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdf
 
A transducer is a device, usually electrical, ele.pdf
                     A transducer is a device, usually electrical, ele.pdf                     A transducer is a device, usually electrical, ele.pdf
A transducer is a device, usually electrical, ele.pdf
 

Recently uploaded

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
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
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
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

Please find the answer to the above problem as follows- Program.pdf

  • 1. Please find the answer to the above problem as follows:- /* Program to insert in a sorted list */ #include #include /* Link list node */ struct node { int data; struct node* next; }; void sortedDelete(struct node** head_ref, int number) { struct node *temp = *head_ref; struct node *prev = NULL; if(*head_ref != NULL && (*head_ref)->data == number){ *head_ref = (*head_ref)->next; return; } while(temp != NULL) { if(temp->data == number){ prev->next = temp->next; free(temp); return; } prev = temp; temp = temp->next; } } /* function to insert a new_node in a list. Note that this * function expects a pointer to head_ref as this can modify the * head of the input linked list (similar to push())*/ void sortedInsert(struct node** head_ref, struct node* new_node) { struct node* current;
  • 2. /* Special case for the head end */ if (*head_ref == NULL || (*head_ref)->data >= new_node->data) { if(*head_ref != NULL&&(*head_ref)->data==new_node->data){ return; } new_node->next = *head_ref; *head_ref = new_node; } else { /* Locate the node before the point of insertion */ current = *head_ref; while (current->next!=NULL && current->next->data < new_node->data) { current = current->next; } if(current->next!=NULL && current->next->data == new_node->data){ return; } new_node->next = current->next; current->next = new_node; } } /* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */ /* A utility function to create a new node */ struct node *newNode(int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; new_node->next = NULL; return new_node;
  • 3. } /* Function to print linked list */ void printList(struct node *head) { struct node *temp = head; if(temp == NULL) printf(" "); while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } /* Drier program to test count function*/ int main(int argc, char *argv[]) { /* Start with the empty list */ struct node* head = NULL; struct node *new_node; FILE *fp; fp = fopen(argv[1], "r+"); if(!fp){ printf("error"); return 1; } char op = ' '; int number = 0; while(fscanf(fp, "%ct%d ", &op, &number)!=EOF){ if(op=='i'){ new_node = newNode(number); sortedInsert(&head, new_node); }else if(op=='d'){ sortedDelete(&head, number); }else{ printf("error"); return;
  • 4. } } printList(head); printf(" "); return 0; } Solution Please find the answer to the above problem as follows:- /* Program to insert in a sorted list */ #include #include /* Link list node */ struct node { int data; struct node* next; }; void sortedDelete(struct node** head_ref, int number) { struct node *temp = *head_ref; struct node *prev = NULL; if(*head_ref != NULL && (*head_ref)->data == number){ *head_ref = (*head_ref)->next; return; } while(temp != NULL) { if(temp->data == number){ prev->next = temp->next; free(temp); return; } prev = temp; temp = temp->next;
  • 5. } } /* function to insert a new_node in a list. Note that this * function expects a pointer to head_ref as this can modify the * head of the input linked list (similar to push())*/ void sortedInsert(struct node** head_ref, struct node* new_node) { struct node* current; /* Special case for the head end */ if (*head_ref == NULL || (*head_ref)->data >= new_node->data) { if(*head_ref != NULL&&(*head_ref)->data==new_node->data){ return; } new_node->next = *head_ref; *head_ref = new_node; } else { /* Locate the node before the point of insertion */ current = *head_ref; while (current->next!=NULL && current->next->data < new_node->data) { current = current->next; } if(current->next!=NULL && current->next->data == new_node->data){ return; } new_node->next = current->next; current->next = new_node; } } /* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */ /* A utility function to create a new node */ struct node *newNode(int new_data)
  • 6. { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; new_node->next = NULL; return new_node; } /* Function to print linked list */ void printList(struct node *head) { struct node *temp = head; if(temp == NULL) printf(" "); while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } /* Drier program to test count function*/ int main(int argc, char *argv[]) { /* Start with the empty list */ struct node* head = NULL; struct node *new_node; FILE *fp; fp = fopen(argv[1], "r+"); if(!fp){ printf("error"); return 1; } char op = ' '; int number = 0; while(fscanf(fp, "%ct%d ", &op, &number)!=EOF){
  • 7. if(op=='i'){ new_node = newNode(number); sortedInsert(&head, new_node); }else if(op=='d'){ sortedDelete(&head, number); }else{ printf("error"); return; } } printList(head); printf(" "); return 0; }