Project 3:
List.c Template
#include <stdio.h>
#include <stdlib.h>
#define MAX_INTS 1000
/* A node in the linked list */
struct node {
int data;
struct node *next;
};
struct node* create_list(int intarray[], int len);
struct node* add_item_at_start(struct node *head, int data);
int search_array(int integers[], int numints, int element);
int search_list(struct node *head, int element);
struct node* create_sorted_list(struct node *head);
struct node* add_item_sorted(struct node *head, int data);
int copy_list_to_array(struct node *head, int *array);
void print_list(struct node *head);
void print_array(int integers[], int len);
int main(int argc, char *argv[])
{
/* TODO: Complete the main method according to the steps outlined */
/* Open a file for reading */
/* Read the numbers from the file, into an array */
/* Print the array */
/* Create a linked list with the integers from the array */
/* Print the linked list */
/* Repeatedly prompt the user for a number to be searched.
* Search the array for the number and print out the result as shown in the specs.
* Search the linked list for the number and print out the result as shown in the specs.
* Stop prompting when the user enters 'q' (just the character q without the single quotes).
*/
/* Create a sorted list(in ascending order) from the unsorted list */
/* Print the sorted list */
/* Copy the sorted list to an array with the same sorted order */
/* Print out the sorted array */
/* Print the original linked list again */
/* Print the original array again */
/* Open a file for writing */
/* Write the sorted array to a file named "sorted_numbers.txt" */
/* Print out the number of integers written to the file */
}
struct node* create_list(int intarray[], int len)
{
/* TODO: Complete this function */
}
struct node* add_item_at_start(struct node *head, int data)
{
/* TODO: Complete this function */
}
int search_list(struct node *head, int element)
{
/* TODO: Complete this function */
}
int search_array(int integers[], int numints, int element)
{
/* TODO: Complete this function */
}
int copy_list_to_array(struct node *head, int *array)
{
/* TODO: Complete this function */
}
struct node* create_sorted_list(struct node *head)
{
/* TODO: Complete this function */
}
struct node* add_item_sorted(struct node *sorted_head, int data)
{
/* TODO: Complete this function */
}
void print_list(struct node *head)
{
if (head == NULL) {
printf("Linked List is Empty.\n");
} else {
struct node *temp = head;
printf("head->");
while (temp != NULL) {
printf("|%d|->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}
void print_array(int integers[], int len)
{
int i;
for (i = 0; i < len; i++) {
printf("| %d ", integers[i]);
}
printf("|\n");
}
Accomplish the following functions
-
void insert(node_t * head, int data)
-
ssize_t build
Program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
#define SEED 42
int main(int argc, char *argv[]) {
int i, rand_num, max_num, total_num;
if (argc != 3) {
printf("USAGE: %s <total_nums> <max_num>\n", argv[0]);
exit(1);
}
total_num = atoi(argv[1]);
max_num = atoi(argv[2]);
srand(SEED);
for (i = 0; i < total_num; ++i) {
rand_num = rand() % max_num;
printf("| %d ", rand_num);
}
printf("|\n");
return 0;
}
Example of program
Run Code:
// ./generate
// USAGE: ./generate <total_nums> <max_num>
./generate 10 100
Output:
| 94 | 23 | 9 | 43 | 26 | 1 | 1 | 60 | 50 | 45 |
myList.c Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INTS 1000
#define MAX_STRL 65535
const char *USAGE = "Usage: ./mylist <-i|-d> <OUTPUT_FILE> <FILE>...";
const char *BADFILE = "ERROR: unable to process file %s\n";
const char *LSTRECV = "LIST CONTENT AS OF FILE %s: %s\n";
const char *ORIGLST = "ORIGINAL LIST: %s\n";
const char *SORTEDLST = "SORTED LIST: %s\n";
typedef struct node {
int data;
struct node *next;
} node_t;
char *list_to_string(node_t *head) {
char *s = calloc(MAX_STRL, sizeof(char));
if (head->next == NULL) {
strcat(s, "Linked List is Empty.");
} else {
node_t *curr = head->next;
strcat(s, "head->");
while (curr != NULL) {
char tempstr[12];
sprintf(tempstr, "|%d|->", curr->data);//print as string
strcat(s, tempstr);
curr = curr->next;
}
strcat(s, "NULL");
}
return s;
}
void insert(node_t *head, int data) {
if(head!=NULL){
node_t *new_node = malloc(sizeof(node_t));
new_node->data = data;
new_node->next = head->next;
head->next = new_node;
}
}
ssize_t build_list_from_file(node_t *head, const char *filename) {
ssize_t numints = 0;//signed size_t
FILE *fin=fopen(filename, "r");
if(fin==NULL){
printf(BADFILE,filename);
return -1;
}
ssize_t read;
size_t len = 0;
char *token;
char * line = NULL;
const char tok[2] = "|";
while ((read = getline(&line, &len, fin)) != -1){
//printf("%s\n",line);
token = strtok(line, tok);
while (token != NULL){
if(strlen(token) > 1){
numints++;
insert(head,atoi(token));
}
token = strtok(NULL, tok);
}
}
// printf("abcdabcdabcd\n");
fclose(fin);
return numints;
}
void list_to_array(int *integers, node_t *head) {
head = head->next;
int i = -1;
while (head != NULL){
integers[++i] = head->data;
head = head->next;
}
return;
}
int qsort_inc(const void *p1, const void *p2) {
return *((int *)p1) > *(int *)p2;
}
int qsort_dec(const void *p1, const void *p2) {
return *((int *)p1) < *(int *)p2;
}
void array_to_list(node_t *head, int *integers, size_t len)
{
for(int i=0;i<len;i++){
node_t *tmp = malloc(sizeof(node_t));
tmp->data = integers[i];
tmp->next = NULL;
head->next = tmp;
head = tmp;
}
return;
}
int write_to_file(const char *filename, node_t *orighead, node_t *newhead) {
// printf("ORIGINAL LIST: %s\n",list_to_string(orighead));
// printf("SORTED LIST: %s\n",list_to_string(newhead));
FILE *fp = fopen(filename, "w");
if(fp == NULL){
//printf(BADFILE,filename);
printf("%s\n",BADFILE);
return -1;
}
fputs("ORIGINAL LIST: ",fp);
fputs(list_to_string(orighead),fp);
fputs("\n",fp);
fputs("SORTED LIST: ",fp);
fputs(list_to_string(newhead),fp);
fputs("\n",fp);
fclose(fp);
return 0;
}
//Usage: ./mylist <-i|-d> <OUTPUT_FILE> <FILE>...
int main(int argc, char *argv[]) {
node_t head = {.data = 0, .next = NULL};
// Verify and process command line arguments
if(argc < 4){
printf("%s\n",USAGE);
return -1;
}
int mode = 0;
if(strcmp(argv[1],"-i") == 0){
mode = 1;
}
else if(strcmp(argv[1],"-d") == 0){
mode = 2;
}
else{
printf("%s\n",USAGE);
return -1;
}
// Read from files and generate a list
int fileNum = (argc-3);
int total = 0;
for(int i=0;i<fileNum;i++){
int len = build_list_from_file(&head,argv[i+3]);
if(len>0){
total += len;
printf(LSTRECV,argv[i+3],list_to_string(&head));
}
}
// Convert the list to an array
int *arrs = (int *)malloc(total*sizeof(int));
list_to_array(arrs,&head);
node_t *orighead = malloc(sizeof(node_t));
array_to_list(orighead,arrs,total);
// Sort the array
if(mode == 1){
qsort(arrs,total,sizeof(int),qsort_inc);
}
else if(mode == 2){
qsort(arrs,total,sizeof(int),qsort_dec);
}
// Convert the sorted array to a new list
node_t *newhead = malloc(sizeof(node_t));
array_to_list(newhead,arrs,total);
// Write both lists to an output file
write_to_file(argv[2],orighead,newhead);
return 0;
}
Example of running Program
// ./mylist
// Usage: ./mylist <-i|-d> <OUTPUT_FILE> <FILE>...
Output: