In C please thanks Implement a linked list that maintains a
In C, please. thanks.
Implement a linked list that maintains a list integers in sorted order. Thus, if the list initially contains 2, 5 and 8, and we insert 1, 3, and 10, then 1 will be inserted at the start of the list. 3 will be inserted between 2 and 5. and 10 will be inserted at the end. Input format: This program takes a file name as Argument from command line. The file will have a number of lines. Each line contains a character (either \'i\' or \'d\') followed by a tab character and an integer number. For each of the line that starts with \'i\', your program should insert the number in the linked list in sorted order if it is not already there. Your program should not insert any duplicate values. If the the starts with a \'d\'. your program should delete the value if it is present in the linked list. Your program should silently ignore it if the requested value is not present in the linked list. Output format: At the end of the execution, your program should print all the values of the linked list in sorted order. The values should be in a single line separated by tabs. There should be no leading or trailing white space in the output. Your program should print \"error\" (and nothing else) if the file does not exist or it contains lines with improper structure. Your program should print a blank line if the input file is empty or the resulting linked list has no nodes. Let\'s assume we have 3 text files with the following contents, file1.txt is empty and, file2.txt: i 10 i 12 d 10 i 5 file3.txt: d 7 i 10 i 5 i 10 d 10 ./third file1.txtSolution
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
// insert element by inspecting them and insert in sorted order
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)
{
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;
}
}
// 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;
while(temp != NULL)
{
printf(\"%d \", temp->data);
temp = temp->next;
}
}
/* Given a reference (pointer to pointer) to the head of a list
and a key, deletes the first occurrence of key in linked list */
void deleteNode(struct node **head_ref, int key)
{
// Store head node
struct node* temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->data == key)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change \'prev->next\'
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL) return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
/* Drier program to test count function*/
int main(int argc, char *argv[])
{
printf(\"File is %s\ \", argv[1]);
FILE *fp = fopen(argv[1], \"r\");
char op[10];
int value;
/* Start with the empty list */
struct node* head = NULL;
while(fscanf(fp, \"%s%d\", op, &value) == 2)
{
if (strcmp(op, \"i\") == 0)
{
struct node *new_node = newNode(value);
sortedInsert(&head, new_node);
}
else
{
if (strcmp(op, \"d\") == 0)
{
deleteNode(&head, value);
}
}
}
printList(head);
return 0;
}


