This homework is to examine arrays, strings and data structures in c language. T
ID: 3717799 • Letter: T
Question
This homework is to examine arrays, strings and data structures in c language. The homework is due in two parts
Part one:
Read in a file of names: last name, first name will be the format.
A) Print the list of names sorted by last names, i.e., lastName, firstName
Findler, Michael
Miller, Teri
Pochert, Lois
B) Print the list of names sorted by first names., i.e., firstName lastName
Lois Pochert
Michael Findler
Teri Miller
C) To get ahead of the game, read these in as two separate arrays.
Part Two:
Final version will require the names to be stored in an array of structs.
Explanation / Answer
//Question 1 solve completely
//With full of code u can change it to second also.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "SLLFunc.h"
typedef struct Node
{
char *l_name,*f_name;
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->l_name[0] < second->l_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,char *l,char *f)
{
char *buf1=(char *)malloc(sizeof(char) * strlen(l));
char *buf2=(char *)malloc(sizeof(char) * strlen(f));
strcpy(buf1,l);
strcpy(buf2,f);
Node *temp=newnode();
temp->next=list;
list->prev=temp;
list->l_name=buf1;
list->f_name=buf2;
return temp;
}
void display(Node *list)
{
if(list==NULL)
{
printf(" ");
return;
}
else
{
printf("<-[%s]->",list->l_name);
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 main()
{
char fname[50];
printf("Enter file name ");
scanf("%s",fname);
FILE *fp=fopen(fname,"r");
if(fp==NULL)
exit(0);
char buffer1[100],buffer2[100];
Node *list=NULL;
while(!feof(fp))
{
fscanf(fp,"%s%s",buffer1,buffer2);
if(feof(fp))
break;
list=addfront(list,buffer1,buffer2);
}
list=mergeSort(list);
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.