5. [20 points] Write a function with the following signature: int sort_by_name (
ID: 3719311 • Letter: 5
Question
5. [20 points] Write a function with the following signature: int sort_by_name (const char *filename); The file specified by filename contains the binary representation of an array of structures of the following type. struct person f int char name [256] int char birthplace [256]; unique_ID; age; This structure's integer fields are stored in the same endianness as the machine that you are using to process the file. In other words, the data within the structure's fields will be stored in the file just as they would be in memory. Currently, the structures within the given file are ordered by their unique ID. Instead, we want to order them ASCIIbetically by name. The function sort_by name O must use mmap (2) and qsort(3) to read, sort, and rewrite the contents of the file. On success, the function should return 0. In case of an error, it should return -1. In either case, success or failure, the function should release any resources that it acquiredExplanation / Answer
//im going to create first link list and then i will sort the list then write in to file again.
// Perfect code what u want is as is implement
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "SLLFunc.h"
typedef struct Node
{
int u_id;
char name[256];
int age;
char b_place[256];
struct Node *next;
struct Node *prev;
}Node;
struct Node *split(struct Node *head);
// Function to merge two linked lists
struct Node *merge(struct Node *first, struct Node *second)
{
// If first linked list is empty
if (!first)
return second;
// If second linked list is empty
if (!second)
return first;
// Pick the smaller value
if (first->name[0] < second->name[0])
{
first->next = merge(first->next,second);
first->next->prev = first;
first->prev = NULL;
return first;
}
else
{
second->next = merge(first,second->next);
second->next->prev = second;
second->prev = NULL;
return second;
}
}
// Function to do merge sort
struct Node *mergeSort(struct Node *head)
{
if (!head || !head->next)
return head;
struct Node *second = split(head);
// Recur for left and right halves
head = mergeSort(head);
second = mergeSort(second);
// Merge the two sorted halves
return merge(head,second);
}
Node *newnode()
{
Node *temp;
temp=(Node *)malloc(sizeof(Node));
temp->next=NULL;
temp->prev=NULL;
return temp;
}
Node *addfront(Node *list,Node *temp)
{
if(list==NULL)
return temp;
temp->next=list;
list->prev=temp;
return temp;
}
void display(Node *list)
{
if(list==NULL)
{
printf(" ");
return;
}
else
{
printf("%d ",list->u_id);
display(list->next);
}
}
struct Node *split(struct Node *head)
{
struct Node *fast = head,*slow = head;
while (fast->next && fast->next->next)
{
fast = fast->next->next;
slow = slow->next;
}
struct Node *temp = slow->next;
slow->next = NULL;
return temp;
}
int sort_by_name(char *fname)
{
FILE *fp=fopen(fname,"r");
if(fp==NULL)
exit(0);
Node *list=NULL;
int u_id;
char name[256];
int age;
char b_place[256];
while(!feof(fp))
{
fscanf(fp,"%d%s%d%s",&u_id,name,&age,b_place);
if(feof(fp))
break;
Node *temp=newnode();
temp->u_id=u_id;
strcpy(temp->name,name);
temp->age=age;
strcpy(temp->b_place,b_place);
list=addfront(list,temp);
}
list=mergeSort(list);
display(list);
fclose(fp);
FILE *fp2;
fp2=fopen(fname,"w");
while(list)
{
fprintf(fp2,"%d %s %d %s ",list->u_id,list->name,list->age,list->b_place);
list=list->next;
}
fclose(fp2);
return 0;
}
//thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.