Question
users.txt
matt
charlie 33
maverick
xyz 123
When you execute your program, your command will print all the users from the linked list, NOT from the file. Use the following skeleton code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 100
void build_a_lst(),print_a_line(),print_lst();
void insert_at_end();
struct clip {
int views;
char *user;
char *id;
char *title;
struct clip *next;
} *head;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 100
void build_a_lst(),print_a_line(),print_lst();
void insert_at_end();
void int_radix_sort();
struct clip {
int views;
char *user;
char *id;
char *title;
struct clip *next;
} *head;
int main(int argc, char **argv) {
build_a_lst(*(argv+1));
print_lst(head); /* prints the table */
int_radix_sort(head);
print_lst(head); /* prints the table but now sorted */
return 0;
}
void build_a_lst(char *fn) {
FILE *fp;
char line[LINE_LENGTH];
// char *line;
if ((fp = fopen(fn,"r")) != NULL) {
while (fgets(line, LINE_LENGTH, fp) != NULL) {
insert_at_end(line); /* insert a user at end of the list */
}
fclose (fp);
}
}
void insert_at_end(char *s) {
// split s into four fields and insert all of them
// views need to be converted to int using atoi(views_str)
// FILL IN
// ...
// FILL IN
}
void int_radix_sort(struct clip *cp) {
/*
you will need a wrapper here for radix_sort only, without having to involve
struct clip or linked list
*/
// we'll talk more on this on Thur, 3/7/2013
// FILL IN
// ...
// FILL IN
}
/* prints all the users */
void print_lst(struct clip *cp) {
// FILL IN
// ...
// FILL IN
}
/* end */
int main(int argc, char **argv) {
build_a_lst(*(argv+1));
print_lst(head); /* prints all the users */
return 0;
}
void build_a_lst(char *fn) {
FILE *fp;
char line[LINE_LENGTH];
// char *line;
if ((fp = fopen(fn,"r")) != NULL) {
while (fgets(line, LINE_LENGTH, fp) != NULL) {
insert_at_end(line); /* insert a user at end of the list */
}
fclose (fp);
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 100
void build_a_lst(),print_a_line(),print_lst();
void insert_at_end();
void int_radix_sort();
struct clip {
int views;
char *user;
char *id;
char *title;
struct clip *next;
} *head;
int main(int argc, char **argv) {
build_a_lst(*(argv+1));
print_lst(head); /* prints the table */
int_radix_sort(head);
print_lst(head); /* prints the table but now sorted */
return 0;
}
void build_a_lst(char *fn) {
FILE *fp;
char line[LINE_LENGTH];
// char *line;
if ((fp = fopen(fn,"r")) != NULL) {
while (fgets(line, LINE_LENGTH, fp) != NULL) {
insert_at_end(line); /* insert a user at end of the list */
}
fclose (fp);
}
}
void insert_at_end(char *s) {
// split s into four fields and insert all of them
// views need to be converted to int using atoi(views_str)
// FILL IN
// ...
// FILL IN
}
void int_radix_sort(struct clip *cp) {
/*
you will need a wrapper here for radix_sort only, without having to involve
struct clip or linked list
*/
// we'll talk more on this on Thur, 3/7/2013
// FILL IN
// ...
// FILL IN
}
/* prints all the users */
void print_lst(struct clip *cp) {
// FILL IN
// ...
// FILL IN
}
/* end */
}
void insert_at_end(char *s) {
// FILL IN
// ...
// FILL IN
}
/* prints all the users */
void print_lst(struct clip *cp) {
// FILL IN
// ...
// FILL IN
}
/* end */
----------------------------------------------------------------------------------------------------
(b) Fill the structure with all four values: views, user, times, titles. And sort in ascending order of views.
Prepare a CSV file (comma separated value file) containing all the values. Each line of the CSV file will consist of views, user, times, titles. Use the same commands you used to create a table, except now that you have to put in "," (commas) in between the four values as delimiter. Note however that before you put in commas, you need to convert those pre-existing commas (some titles have commas unfortunately) to something else such as MY_COMMA or something to that sort. Later on you can revert MY_COMMA back to real commas when everything is said and done.
The skeleton code remains almost the same, except now that you have to put in all four values in strct clip you just malloc'ed.
Use strtok() function to split a line into four fields with "," as the delimiter. As cautioned above, you need to convert commas to something else which can then be reverted back to commas after you build a list.
insert_at_end(char *s) will insert not just user but all the remaining values as well.
Sort the list in ascending of views using int radix sort See the skeleton for calling int_radix_sort(head).
Use the following skeleton code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 100
void build_a_lst(),print_a_line(),print_lst();
void insert_at_end();
void int_radix_sort();
struct clip {
int views;
char *user;
char *id;
char *title;
struct clip *next;
} *head;
int main(int argc, char **argv) {
build_a_lst(*(argv+1));
print_lst(head); /* prints the table */
int_radix_sort(head);
print_lst(head); /* prints the table but now sorted */
return 0;
}
void build_a_lst(char *fn) {
FILE *fp;
char line[LINE_LENGTH];
// char *line;
if ((fp = fopen(fn,"r")) != NULL) {
while (fgets(line, LINE_LENGTH, fp) != NULL) {
insert_at_end(line); /* insert a user at end of the list */
}
fclose (fp);
}
}
void insert_at_end(char *s) {
// split s into four fields and insert all of them
// views need to be converted to int using atoi(views_str)
// FILL IN
// ...
// FILL IN
}
void int_radix_sort(struct clip *cp) {
/*
you will need a wrapper here for radix_sort only, without having to involve
struct clip or linked list
*/
// we'll talk more on this on Thur, 3/7/2013
// FILL IN
// ...
// FILL IN
}
/* prints all the users */
void print_lst(struct clip *cp) {
// FILL IN
// ...
// FILL IN
}
/* end */
users.txt
matt
charlie 33
maverick
xyz 123
title.txt
mattdamon
charlie33attacks
maverickcrashes
xyz123 alphabet numbers
views.txt
13532
35423
9
55555
times.txt
2:00
3:00
1:00
3:30
------------------------------------------------------------------------------------------------------
(c)
Write a C program to build an array of linked lists each of which holds a table like you did part b. Use the following example.
#define MAX_CLIP 60
struct clip *hourly[MAX_CLIPS];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define LINE_LENGTH 200
#define MAX_CLIPS 100
void print_a_line(),print_a_clip(),print_a_lst(),print_lsts();
void split_line();
void build_lsts();
void int_radix_sort();
void sort_lsts();
struct clip *insert_at_end();
struct clip *build_a_lst();
struct clip *hourly[MAX_CLIPS];
struct clip {
int number;
int views;
char *user;
char *id;
char *title;
char *time;
struct clip *next;
};
int main(int argc, char **argv) {
char filename[LINE_LENGTH];
build_lsts(*(argv+1)); /* filename prefix */
print_lsts(hourly);
// sort_lsts(); /* sort hourly in descending order of views */
// print_lsts(hourly);
return 0;
}
void sort_lsts() {
/*
sort individual lists in descending order of views
such that the first clip in each list will have the highest views
this one is the same as HW4
once you are done sorting individual lists, sort the entire array
this one is again the same as HW4 except now that you use values from the first clip of each list
*/
}
/* use print_a_lst(struct clip *cp); */
void print_lsts(struct clip **lst) {
// FILL IN
// ...
// FILL IN
}
/* prefix can be such as: index-2013-03-01-12
the directory will have 60 files with the above prefix */
void build_lsts(char *prefix) {
FILE *fp;
char *cmd,*filename;
int i;
for (i=0;i<MAX_CLIPS;i++) hourly[i] = NULL;
sprintf(cmd,"ls %s*",prefix);
fp = popen(cmd,"r");
/* read output from command */
i=0;
while (fscanf(fp,"%s",filename) == 1) {
hourly[i] = build_a_lst(filename);
i++;
// print_a_lst(hourly[i]);
}
fclose(fp);
}
/* open the file, read one line at a time, split and insert */
struct clip *build_a_lst(char *fn) {
FILE *fp;
struct clip *hp;
char *fields[5];
char line[LINE_LENGTH];
int cnt=0;
hp=NULL;
// open fn
// while no more lines
// read a line
// split the line into five substrings and store them in fields
// insert at the end of the list
// return the head pointer holding the list
return hp;
}
/* fields will have five values stored */
void split_line(char **fields,char *line) {
int i=0;
char *token, *delim;
delim = ",n";
/*
call strtok(line, delim);
malloc and strcpy token to fields
repeat until strtok returns NULL using strtok(NULL, delim);
each time you get token, malloc and strcpy to fields
*/
}
/* set the five values into a clip
insert a clip at the of the list */
struct clip *insert_at_end(struct clip *hp,char **five) {
struct clip *cp,*tp;
/*
malloc tp
set views using atoi(*five)
malloc for four strings.
strcpy four strings to tp
insert tp at the end of the list pointed by hp
use cp to traverse the list
return hp
*/
return hp;
}
void print_a_lst(struct clip *cp) {
/*
use a while loop and the statement below to print the list
printf("%d,%s,%s,%s,%sn",cp->views,cp->user,cp->id,cp->title,cp->time);
*/
}
/* end */
(Assume that you have 60 index.txt)
First, sort each list (an index page) using int radix sort in descending order of views. This is part B where you used two arrays of index and values. At the end of this sorting step, the first clip in each list will have the highest views while the last one will have the lowest.
Second, sort the entire array of 60 tables on views. Since each list is already sorted in descending order, all you have to do is use the first one in each list.
Print the top clip in each list. This should give you some idea about what happend to the popularity of the clips within an hour period.
skeleton code. (ok to sort only user title views and time table.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define LINE_LENGTH 200
#define MAX_CLIPS 100
void print_a_line(),print_a_clip(),print_a_lst(),print_lsts();
void split_line();
void build_lsts();
void int_radix_sort();
void sort_lsts();
struct clip *insert_at_end();
struct clip *build_a_lst();
struct clip *hourly[MAX_CLIPS];
struct clip {
int number;
int views;
char *user;
char *id;
char *title;
char *time;
struct clip *next;
};
int main(int argc, char **argv) {
char filename[LINE_LENGTH];
build_lsts(*(argv+1)); /* filename prefix */
print_lsts(hourly);
// sort_lsts(); /* sort hourly in descending order of views */
// print_lsts(hourly);
return 0;
}
void sort_lsts() {
/*
sort individual lists in descending order of views
such that the first clip in each list will have the highest views
this one is the same as HW4
once you are done sorting individual lists, sort the entire array
this one is again the same as HW4 except now that you use values from the first clip of each list
*/
}
/* use print_a_lst(struct clip *cp); */
void print_lsts(struct clip **lst) {
// FILL IN
// ...
// FILL IN
}
/* prefix can be such as: index-2013-03-01-12
the directory will have 60 files with the above prefix */
void build_lsts(char *prefix) {
FILE *fp;
char *cmd,*filename;
int i;
for (i=0;i<MAX_CLIPS;i++) hourly[i] = NULL;
sprintf(cmd,"ls %s*",prefix);
fp = popen(cmd,"r");
/* read output from command */
i=0;
while (fscanf(fp,"%s",filename) == 1) {
hourly[i] = build_a_lst(filename);
i++;
// print_a_lst(hourly[i]);
}
fclose(fp);
}
/* open the file, read one line at a time, split and insert */
struct clip *build_a_lst(char *fn) {
FILE *fp;
struct clip *hp;
char *fields[5];
char line[LINE_LENGTH];
int cnt=0;
hp=NULL;
// open fn
// while no more lines
// read a line
// split the line into five substrings and store them in fields
// insert at the end of the list
// return the head pointer holding the list
return hp;
}
/* fields will have five values stored */
void split_line(char **fields,char *line) {
int i=0;
char *token, *delim;
delim = ",n";
/*
call strtok(line, delim);
malloc and strcpy token to fields
repeat until strtok returns NULL using strtok(NULL, delim);
each time you get token, malloc and strcpy to fields
*/
}
/* set the five values into a clip
insert a clip at the of the list */
struct clip *insert_at_end(struct clip *hp,char **five) {
struct clip *cp,*tp;
/*
malloc tp
set views using atoi(*five)
malloc for four strings.
strcpy four strings to tp
insert tp at the end of the list pointed by hp
use cp to traverse the list
return hp
*/
return hp;
}
void print_a_lst(struct clip *cp) {
/*
use a while loop and the statement below to print the list
printf("%d,%s,%s,%s,%sn",cp->views,cp->user,cp->id,cp->title,cp->time);
*/
}
/* end */
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 100
void build_a_lst(), print_a_line(), print_lst();
void insert_at_end();
struct clip {
int views;
char *user;
char *id;
char *title;
struct clip *next;
} *head;
void int_radix_sort(struct clip *cp);
int main(int argc, char **argv) {
build_a_lst(*(argv+1));
print_lst(head); /* prints the table */
return 0;
}
void build_a_lst(char *fn) {
FILE *fp;
char line[LINE_LENGTH];
// char *line;
if ((fp = fopen(fn,"r")) != NULL) {
while (fgets(line, LINE_LENGTH, fp) != NULL) {
insert_at_end(line); /* insert a user at end of the list */
}
fclose (fp);
}
}...