Sorting a linked list and Inserting a new element in descending order in C progr
ID: 3918844 • Letter: S
Question
Sorting a linked list and Inserting a new element in descending order in C programming
Given:
typedef struct person {
unsigned short age;
char *name; // first name, no spaces
struct height {int feet; int inches;} vertical;
int idenifier; // unique identifier
struct person *next;
} person_t;
The linked list should be sorted in descending order depedning on the age.
Say, a file has the data below
31 Alice 5 9 111111
64 Bob 6 11 555555
22 Mira 6 3 7171
The first element of the list is pointed to by person_t *head which in the example would be 64 Bob 6 11 555555.
After sorting, the file output should be
64 Bob 6 11 555555
31 Alice 5 9 111111
22 Mira 6 3 7171
Given : void insert_plist(unsigned short a, char *b, int c, int d, int e);
Then we want to insert a new person in the linked list, the position of the person to be inserted depends on the descending order.
Say if we want to insert 48 Aaron 6 10 77778 into the linked list above. The element would be placed in the second since the age 48 is the second largest number in the list.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
input file: input.txt
-------
31 Alice 5 9 111111
64 Bob 6 11 555555
22 Mira 6 3 7171
main.c
------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct person {
unsigned short age;
char *name; // first name, no spaces
struct height {int feet; int inches;} vertical;
int idenifier; // unique identifier
struct person *next;
} person_t;
person_t *head = NULL;
void insert_plist(unsigned short a, char *b, int c, int d, int e);
void write_to_file(char *filename);
int main(){
FILE *fp;
char in_filename[20], out_filename[20];
char name[20];
int age, feet, inches, id;
printf("Enter input filename: ");
scanf("%s", in_filename);
printf("Enter output filename: ");
scanf("%s", out_filename);
fp = fopen(in_filename, "r");
if(fp == NULL){
printf("ERROR: could not open input file %s ", in_filename);
exit(1);
}
fscanf(fp, "%d %s %d %d %d", &age, name, &feet, &inches, &id);
while(!feof(fp)){
insert_plist(age, name, feet, inches, id);
fscanf(fp, "%d %s %d %d %d", &age, name, &feet, &inches, &id);
}
fclose(fp);
write_to_file(out_filename);
printf("Please check output file %s ", out_filename);
}
void insert_plist(unsigned short a, char *b, int c, int d, int e){
person_t *p = (person_t*) malloc(sizeof(person_t));
person_t *current = head, *previous = NULL;
p->age = a;
p->name = (char*)malloc(strlen(b) + 1);
strcpy(p->name, b);
p->vertical.feet = c;
p->vertical.inches = d;
p->idenifier = e;
p->next = NULL;
while(current != NULL){
if(p->age > current->age)
break;
previous = current;
current = current->next;
}
p->next = current;
if(previous == NULL) //inserting at head
head = p;
else
previous->next = p;
}
void write_to_file(char *filename){
person_t *p = head;
FILE *fp = fopen(filename, "w");
if(fp == NULL){
printf("ERROR: could not open output file %s ", filename);
exit(1);
}
while(p != NULL){
fprintf(fp, "%d %s %d %d %d ", p->age, p->name, p->vertical.feet, p->vertical.inches, p->idenifier);
p = p->next;
}
fclose(fp);
}
output:
=======
Enter input filename: input.txt
Enter output filename: output.txt
Please check output file output.txt
output file: output.txt
----------------------
64 Bob 6 11 555555
31 Alice 5 9 111111
22 Mira 6 3 7171
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.