In this assignment, you will write a C program toc . Read texts from a file 2. B
ID: 3903707 • Letter: I
Question
In this assignment, you will write a C program toc . Read texts from a file 2. Build a doubly linked list from the texts; 3. Perform a quick-sort (in the ascending order) for the above linked list. I General Requirements Your doubly linked list node data structure is defined as in the following: struct mynode const char "text: struct mynode "next struct mynode prev Your program's implementation must include the following features: .Your program must read texts from a file. The name of the file is given as the first argument Each line is put into a node?The nodes form The program will read one line a time from the file. a doubly link list. .The program prints the initial, unsorted list and a sorted list. Your program must be compiled from 3 source files: o main.c (Handles input and output, as well as top-level program logic.) o node h (Declares the data structure and function quicksort, which sorts a given doubly linked list with the ascending order), and printlist, which prints a linked list to the screen. o node.c (Defines the function quicksort and printlist, as declared in node h.) Your node h file must contain the proper preprocessor directives to prevent multiple inclusion The function quicksort must be declared exactly as follows. o return value: struct mynode o argument (only one): struct mynode ,which is the head of the given linked list. o return value: void. o argument (only one): struct mynode ,which is the head of the given linked list The function printlist must be declared exactly as follows. .You may use any systems or tools to create and run your program. However, your program must run correctly on a departmental Linux computer Please follow the guidelines in the programming guideline document. A sample run may look like this: . %./a.out poem . txt original text: ime flies, and memories fade People change and new friendships are made Only the true remain forever at our side Eventually the disappointment and pain wil1 subside Sorted text: Eventually the disappointment and pain will subside Only the true remain foreverat our side People change and new friendships are madeExplanation / Answer
here is your program : ------------>>>>>>>>>>>>
node.h : -------->>>>>>>>
#ifndef __MYNODE__H
#define __MYNODE__H
typedef struct mynode{
const char *text;
struct mynode *next;
struct mynode *prev;
}Mynode;
void swap(Mynode *n1,Mynode *n2);
Mynode* quicksort(Mynode* list);
Mynode* newNode();
Mynode* addNode(Mynode *list,char *text);
void printList(Mynode *list);
#endif
node.c : ------------>>>>>>>>
#include "node.h"
#include<stdio.h>
#include<string.h>
void swap(Mynode *n1,Mynode *n2){
Mynode *temp = n1->prev;
n1->prev = n2->prev;
n2->prev = temp;
temp = n1->next;
n1->next = n2->next;
n2->next = temp;
if(n1->prev != NULL)
n1->prev->next = n1;
if(n1->next != NULL)
n1->next->prev = n1;
if(n2->prev != NULL)
n2->prev->next = n2;
if(n2->next != NULL)
n2->next->prev = n2;
}
Mynode* newNode(){
Mynode *n;
n = (Mynode*)malloc(sizeof(Mynode));
n->text = (char *)malloc(sizeof(char)*500);
n->next = NULL;
n->prev = NULL;
return n;
}
Mynode* addNode(Mynode *list,char *text){
Mynode *l = list;
if(l == NULL){
list = newNode();
strcpy(list->text,text);
return list;
}
while(l->next != NULL){
l = l->next;
}
l->next = newNode();
l->next->prev = l;
strcpy(l->next->text,text);
return list;
}
Mynode* quicksort(Mynode *list){
if(list == NULL){
return NULL;
}
if(list->next == NULL){
return list;
}
Mynode *left = list;
Mynode *right = list;
while(right->next != NULL){
right = right->next;
}
Mynode *pivot = right;
right = right->prev;
while(right != left){
if(strcmp(left->text,pivot->text) <= 0){
left = left->next;
continue;
}
if(strcmp(right->text,pivot->text) >= 0){
right = right->prev;
continue;
}
if(strcmp(left->text,pivot->text) > 0 && strcmp(right->text,pivot->text) < 0){
swap(left,right);
left = left->next;
}
}
right->prev->next = NULL;
left = quicksort(list);
pivot->prev->next = NULL;
right = quicksort(right);
Mynode *l = left;
if(left != NULL)
while(left->next != NULL){
left = left->next;
}
if(left != NULL)
left->next = pivot;
pivot->prev = left;
pivot->next = right;
if(right != NULL)
right->prev = pivot;
return l;
}
void printList(Mynode *list){
Mynode *l = list;
while(l != NULL){
printf(" %s",l->text);
l = l->next;
}
}
main.c : --------->>>>>>>>>>>
#include "node.c"
#include<stdlib.h>
int main(int argc,char *argv[]){
if(argc < 2){
printf("Please provide file name as argument");
return -1;
}
FILE *fp;
fp = fopen(argv[1],"r");
if(fp == NULL){
printf("file opening error !!! ");
return -1;
}
Mynode *list = NULL;
char line[500];
while(!feof(fp)){
fgets(line,500,fp);
list = addNode(list,line);
}
Mynode *sortList = quicksort(list);
printf("Unsorted list : ");
printList(list);
printf(" Sorted list : ");
printList(sortList);
}