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, \"%c\\t%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 point.
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){