hi, I have to create a linked list that uses an .txt file and alphabetizes the names into an output file. im struggling on how to sort the input file.
here is the code i have so far
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
char name[15];
char title[15];
int year;
struct Node *next;
struct Node *prev;
};
typedef struct Node* Box;
Box build_node(FILE *inputp);
int main()
{
Box head=NULL,temp;
FILE *inputp, *outputp;
int i;
inputp = fopen("input.txt", "r");
outputp=fopen("output.txt", "w");
head=build_node(inputp);
printf( "%s, %s, %d \n", head->name, head->title, head->year);
for(i=0; i<5; i++)
{
temp=build_node(inputp);
printf( "%s, %s, %d \n", temp->name, temp->title, temp->year);
}
return 0;
}
Box build_node(FILE *inputp)
{
Box temp=NULL;
temp=(Box)malloc(sizeof(struct Node));
fscanf_s(inputp, "%s", &temp->name);
fscanf_s(inputp, "%s", &temp->title);
fscanf_s(inputp, " %d", &temp->year);
temp->next=NULL;
temp->prev=NULL;
return temp;
}
im pretty sure that to sort it i need to use this for loop but i cant figure it out exactly
Code:
for(i=0, i<5; i++)
for(here=head; ? ; here=here->next)
if(here->data > here->next->data)
swap(here->data, here->next->data)
i have attached the input file, any help is appreciated