Week 9 Code Examples

#include <stdio.h>
#include "list.h"

void test_search_and_delete(void);
void test_search_and_delete_all(void);

int main(void) {

    //test_search_and_delete();
    test_search_and_delete_all();
    
    return 0;
}


void test_search_and_delete(void) {
    struct node *head = NULL;
  
    for (int i = 0; i < 5; i++) {
        head = insert_tail(head, i);
    }

    // 0->1->2->3->4
    print_list(head);

    // Deleting in the middle
    printf("delete 2\n");
    head = search_and_delete(head, 2);
    print_list(head);    

    // Nothing to delete, as 99 is not in the list
    printf("delete 99\n");
    head = search_and_delete(head, 99);
    print_list(head);  
    
    // Deleting first node
    printf("delete 0\n");
    head = search_and_delete(head, 0);
    print_list(head); 

    // Deleting last node 
    printf("delete 4\n");
    head = search_and_delete(head, 4);
    print_list(head); 

    
    printf("delete 3\n");
    head = search_and_delete(head, 3);
    print_list(head); 


    // Deleting from a list with only 1 node
    printf("delete 1\n");
    head = search_and_delete(head, 1);
    print_list(head); 

    
    free_list(head);
}

void test_search_and_delete_all(void) {
    struct node *head = NULL;
  
    for (int i = 0; i < 10; i++) {
        head = insert_tail(head, 2);
    }


    print_list(head);

    // Deleting all nodes!
    printf("delete 2\n");
    head = search_and_delete_all(head, 2);
    print_list(head);    

    // Nothing to delete, as 99 is not in the list
    printf("delete 99\n");
    head = search_and_delete_all(head, 99);
    print_list(head);  
    
}
// Search and delete in a linked list

#include <stdio.h>
#include <stdlib.h>
#include "list.h"


struct node *create_node(int data) {
    struct node *new_node = malloc(sizeof(struct node));
    if (new_node == NULL) {
        return NULL;
    }
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

void print_list(struct node *head) {
    printf("list:\n");
    struct node *curr = head;
    while (curr != NULL) {
        printf("%d ", curr->data);
        curr = curr->next;
    }
    printf("\n");
}

struct node *insert_tail(struct node *head, int data) {
    struct node *new_node = create_node(data);
    struct node *current = head;

    // Case for when the list is empty
    if (head == NULL) {
        head = new_node;
    } else {
        // Find the last node in the list
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
    return head;
}

// Deletes and frees the memory of the first node in the list
// returns the head of the list
struct node *delete_first(struct node *head) {
    if (head != NULL) {
        struct node *tmp = head;
        head = head->next;
        free(tmp);
    }
    return head;
}

// frees memory of all nodes in the given list
void free_list(struct node *head) {
     struct node *current = head;
     while (current != NULL) {
        head = head->next;
        free(current);
        current = head;
     }
}

// Original copy of buggy code
struct node *search_and_delete_buggy(struct node *head, int value) {
    struct node *prev = NULL;
    struct node *curr = head;

    // Find node to delete
    while (curr != NULL && curr->data != value) {
        prev = curr;
        curr = curr->next;
    }
    prev->next = curr->next;
    free(curr);

    return head;
}

// searches for a value in the list, deletes it and frees
// the memory.
// returns the head of the list
// Only deletes the first occurrence of a value in the list
// TEST AND DEBUG
struct node *search_and_delete(struct node *head, int value) {
    struct node *prev = NULL;
    struct node *curr = head;

    // Find node to delete
    while (curr != NULL && curr->data != value) {
        prev = curr;
        curr = curr->next;
    }
    if (curr != NULL) {
        if (prev != NULL) {
            prev->next = curr->next; //HERE prev NULL
        } else {
            head = curr->next;
        }
        free(curr);
    }
    
    return head;
}


// Modify to delete all occurrences of value, not just the first one.
struct node *search_and_delete_all(struct node *head, int value) {
     struct node *prev = NULL;
    struct node *curr = head;

    // Find node to delete
    while (curr != NULL) {
        if (curr->data == value){
            if (prev != NULL) {
                prev->next = curr->next; 
                free(curr);
                curr = prev->next;
            } else {
                head = curr->next;
                free(curr);
                curr = head;
            }
        } else {
            prev = curr;
            curr = curr->next;
        }
    }
    
    
    return head;
}
struct node {
    int data;
    struct node *next;
};

// Function that mallocs and returns a new node
// Params:
//      - num: data for the node
// Returns:
//      - pointer to a struct node
struct node *create_node(int num);

// FUnction to print a linked list
// Params: 
//      - head: a pointer to teh first node in the list
// Return:
//      nothing
void print_list(struct node *head);

// Function to insert a node at the tail of the list
// Params:
//      - head: pointer to the head of the list
//      - num: the dnode data we weant to insert
// Returns:
//      - head: a pointer to the head of the list
struct node *insert_tail(struct node *head, int num);


// Function to delete and free the memory from the first node from list
//
// Params:
//      - head: a pointer to the first node in the list (can be NULL)
// Return:
//      - head: a pointer to the new head of the list (can be NULL)
struct node *delete_first_node(struct node *head);

// Frees all nodes in a linked list.
//
// After this function returns, the memory occupied by every node in the
// list has been released. The list should not be accessed afterwards.
//
// Params:
//      - head: pointer to the first node in the list (may be NULL)
void free_list(struct node *head);


// Search for the first occurrence of the given value in the list
// and delete it and free the memory
//
// Params:
//      - head: a pointer to the first node in the list (can be NULL)
//      - value: a value to search for in the list to delete
// Return:
//      - head: a pointer to the new head of the list (can be NULL)
struct node *search_and_delete(struct node *head, int value);



// Search for the all occurrences of the given value in the list
// and delete and free the memory
//
// Params:
//      - head: a pointer to the first node in the list (can be NULL)
//      - value: a value to search for in the list to delete
// Return:
//      - head: a pointer to the new head of the list (can be NULL)
struct node *search_and_delete_all(struct node *head, int value);
// email_management_system.c
// Tammy Zhong
// Implementation file for email management system functions
// Modified by Angela Finlayson
// (TODO)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "email_management_system.h"

// Function prototypes 
// ADD YOUR OWN FUNCTION PROTOTYPES HERE 


// Function definitions

// Creates a new empty email folder 
//(TODO)
struct folder *create_folder(char name[]) {
    // TODO: implement this function
    printf("create_folder not yet implemented.\n");
    exit(1);  
}

// Add a new email to the folder at the head 
// Similar to previous lecture code insert_head
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[], 
    char subject[], double size, enum email_type type, enum priority_type priority) {
     // TODO: implement this function
    printf("insert_email_at_head not yet implemented.\n");
    exit(1); 
}

// Given a string, look for email where the subject is this string 
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]) {
    // TODO: implement this function
    printf("search_email not yet implemented.\n");
    exit(1); 
}

// Removes all emails from a folder
// clears and delete associated memory of folder and emails in it
void clear_folder(struct folder *email_folder) {
     // TODO: implement this function
    printf("clear_folder not yet implemented.\n");
    exit(1);
}

// Remove email(s) with certain priority (e.g. filter out low priority emails)
void delete_email_of_priority(struct folder *email_folder, enum priority_type priority) {
    // TODO: implement this function
    printf("delete_email_of_priority not yet implemented.\n");
    exit(1);
}

// Append emails from folder_2 after folder_1 and returns them in the one folder
// Assumption made here assumes not same subject email in folder_1 and folder_2
struct folder *merge_folders(char merged_folder_name[], 
    struct folder *folder_1, struct folder *folder_2) {
    // TODO: implement this function
    printf("merge_folders not yet implemented.\n");
    exit(1);
}

// Split emails in a given folder into 3 folders based on email type 
// keeping original ordering
// assumption: received, sent and draft are folders already malloc-ed and initialised
void split_folder(struct folder *given_folder, 
    struct folder *received, struct folder *sent, struct folder *draft) {
    // TODO: implement this function
    printf("split_folder not yet implemented.\n");
    exit(1);
}

// returns number of emails in a given folder 
// (EXTENSION TODO: Improve this)
int count_emails(struct folder *email_folder) {
    int counter = 0;
    struct email *current = email_folder->emails;
    while (current != NULL) {
        counter++;
        current = current->next;
    }
    return counter;
}


// Your functions here (include function comments):


////////////////////////////////////////////////////////////////////////////////
//                              Provided Code                              //
////////////////////////////////////////////////////////////////////////////////

// Print/display all emails in folder 
// Similar to previous lecture code print_list
void print_emails(struct folder *email_folder) {
    printf("Folder Name: %s\n", email_folder->name);
    struct email *current = email_folder->emails;

    if (current == NULL) {
        printf("There are no emails in this folder.\n");
    }

    while (current != NULL) {
        printf("---\n");
        print_single_email(current);
        current = current->next;
    }
}

// Prints out details of a single email 
void print_single_email(struct email *email) {
    if (email == NULL) {
        printf("Email is NULL.\n");
        return;
    }

    printf("Sender: %s\n", email->sender);
    printf("Subject: %s\n", email->subject);
    printf("Size: %lfmb\n", email->size);
    printf("Type: ");
    if (email->type == RECEIVED) {
        printf("RECEIVED\n");
    } else if (email->type == SENT) {
        printf("SENT\n");
    } else if (email->type == DRAFT) {
        printf("DRAFT\n");
    } else {
        printf("UNKNOWN\n");
    }
    printf("Priority: ");
    if (email->priority == HIGH_PRIORITY) {
        printf("HIGH_PRIORITY\n");
    } else if (email->priority == NORMAL_PRIORITY) {
        printf("NORMAL_PRIORITY\n");
    } else if (email->priority == LOW_PRIORITY) {
        printf("LOW_PRIORITY\n");
    } else {
        printf("UNKNOWN\n");
    }
}
// email_management_system.h
// Tammy Zhong
// Header file for email management system functions
// Modified by Angela Finlayson
// (Provided)

// #defines
#define MAX_LEN 100

// enum definition
enum email_type {
    DRAFT,
    RECEIVED,
    SENT,
};

enum priority_type {
    LOW_PRIORITY,
    NORMAL_PRIORITY,
    HIGH_PRIORITY
};

// struct definitions
struct folder {
    char name[MAX_LEN];
    //int num_emails; // to use later :)
    struct email *emails;
};

struct email {
    char sender[MAX_LEN];
    char subject[MAX_LEN];
    double size;
    enum email_type type;
    enum priority_type priority; 
    struct email *next;
};

// Creates a new empty email folder 
// (TODO)
struct folder *create_folder(char name[]);

// Add a new email to the folder at the head 
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[], 
    char subject[], double size, enum email_type type, enum priority_type priority);

// Given a string, look for email where the subject is this string 
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]);

// Remove email(s) with certain priority (e.g. filter out low priority emails)
// (TODO)
void delete_email_of_priority(struct folder *email_folder, enum priority_type priority);

// Append emails from folder_2 after folder_1 and returns them in the one folder
// (TODO)
struct folder *merge_folders(char merged_folder_name[], 
    struct folder *folder_1, struct folder *folder_2);

// Split emails in a given folder into 3 folders based on email type 
// keeping original ordering
// (TODO)
void split_folder(struct folder *given_folder, 
    struct folder *received, struct folder *sent, struct folder *draft);

// Removes all emails from a folder
// (TODO)
void clear_folder(struct folder *email_folder); // leak-check demo

// Returns number of emails in a given folder 
// (PROVIDED - TODO CAN BE IMPROVED)
int count_emails(struct folder *email_folder);

// CHALLENGE at home for fun: Sort emails in folder based on size
// (slightly out of scope of COMP1511 - touches on sorting algorithms)
// (TODO)
void sort_emails(struct folder *email_folder);

// Prints out details of a single email 
// (PROVIDED)
void print_single_email(struct email *email);

// Print/display all emails in folder 
// (PROVIDED)
void print_emails(struct folder *email_folder);
// main.c
// Angela Finlayson 
// Uses the email management system to allow a user to create a folder
// and and emails (with not content) to it.
// Only works for 1 folder... 
// (Provided)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "email_management_system.h"

void print_instructions(void);
void handle_search(struct folder *current_folder);
void handle_add_email(struct folder *current_folder);
void trim_newline(char str[]);

int main(void) {
    struct folder *current_folder = create_folder("inbox");
    char command;

    print_instructions();
    printf("\nEnter command: ");
    while (scanf(" %c", &command) == 1 && command != 'q') {
        if (command == 'a') {
            handle_add_email(current_folder);
        } else if (command == 'p') {
            print_emails(current_folder);
        } else if (command == 'n') { 
            printf("Number of emails: %d\n", count_emails(current_folder));
        } else if (command == 's') {
            handle_search(current_folder);
        } else {
            printf("Unknown command. Try again.\n");
        }
        printf("\nEnter command: ");
    }
    printf("Exiting\n");
    clear_folder(current_folder);
    return 0;
}

// Prints instructions to users
void print_instructions(void) {
    printf("Email Manager - Command Interface\n");
    printf("Commands:\n");
    printf("a - Add a new email to the folder\n");
    printf("p - Print all emails in the folder\n");
    printf("n - Count emails in the folder\n");
    printf("s - Search for an email by subject\n");
    printf("q - Quit\n");
}

// Handles adding a new email to the folder
// Assumes no error checking needed
void handle_add_email(struct folder *current_folder) {
    char sender[MAX_LEN] = "";
    char subject[MAX_LEN]= "";
    double size;
    int priority, type;

    // Remove whitespace from buffer
    int whitespace_ch;
    while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}

    printf("Enter sender: ");
    fgets(sender, MAX_LEN, stdin);
    trim_newline(sender);
    printf("Enter subject: ");
    fgets(subject, MAX_LEN, stdin);
    trim_newline(subject);
    printf("Enter size (in MB): ");
    scanf("%lf", &size);
    printf("Enter email type (0 for DRAFT, 1 for RECEIVED, 2 for SENT): ");
    scanf("%d", &type);
    printf("Enter priority (0 for LOW, 1 for NORMAL, 3 for HIGH): ");
    scanf("%d", &priority);
    insert_email_at_head(current_folder, sender, subject, size, type, priority);
    printf("Email added to folder '%s'.\n", current_folder->name);
}

// Handles searching for an email by subject in the folder
void handle_search(struct folder *current_folder) {
    char subject[MAX_LEN];
    
    // Remove whitespace from buffer
    int whitespace_ch;
    while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}

    printf("Enter subject to search: ");
    fgets(subject, MAX_LEN, stdin);
    trim_newline(subject);
    
    struct email *found_email = search_email(current_folder, subject);
    if (found_email != NULL) {
        printf("Email found:\n");
        print_single_email(found_email);
    } else {
        printf("No email with subject '%s' found.\n", subject);
    }
}

// removes trailing newline character
void trim_newline(char str[]) {
    int len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';  
    }
}
// main.c
// Tammy Zhong
// Program to test email management system functions
// Modified by Angela Finlayson 9/11/2025
// (Provided)


#include <stdio.h>
#include <stdlib.h>

#include "email_management_system.h"

int main(void) {
    // STAGE 1
    printf("*****1. Creating Folder 1...*****\n");
    struct folder *folder_1 = create_folder("Folder 1");

    printf("\n*****2. Number of emails in Empty Folder 1:*****\n");
    printf("%d\n", count_emails(folder_1));

    printf("\n*****3. Printing Empty Folder 1...*****\n");
    print_emails(folder_1);

    printf("\n*****4. Inserting emails in Empty Folder 1 *****\n");
 
    insert_email_at_head(folder_1, "Jon", "Surfing Trip", 10.2, RECEIVED, HIGH_PRIORITY);
    insert_email_at_head(folder_1, "Tim", "Basketball", 20.44, DRAFT, NORMAL_PRIORITY);
    insert_email_at_head(folder_1, "Brianna", "Tax Return", 150.0, SENT, LOW_PRIORITY);
    insert_email_at_head(folder_1, "Tammy", "D&D Night", 90.2, SENT, NORMAL_PRIORITY);
    insert_email_at_head(folder_1, "Sofia", "COMP1511 Exam", 90.0, DRAFT, LOW_PRIORITY);

    printf("\n*****5. Printing Folder 1...*****\n");
    print_emails(folder_1); 

    printf("\n*****6. Number of emails in Folder 1: *****\n");
    printf("%d\n", count_emails(folder_1));
   
    
    printf("\n*****7. Look for email with subject 'Surfing Trip'...*****\n");
    struct email *email_found = search_email(folder_1, "Surfing Trip");
    
    printf("\n*****8. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****9. Look for email with subject 'D&D Night'...*****\n");
    email_found = search_email(folder_1, "D&D Night");
    
    printf("\n*****10. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****11. Look for email with subject 'COMP1511 Exam'...*****\n");
    email_found = search_email(folder_1, "COMP1511 Exam");
    
    printf("\n*****12. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****13. Look for email with subject 'Holiday'...*****\n");
    email_found = search_email(folder_1, "Holiday");
    
    printf("\n*****14. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);
    

    printf("\n*****15. Creating Folder 2... and inserting emails...*****\n");
    struct folder *folder_2 = create_folder("Folder 2");
    insert_email_at_head(folder_2, "Liam", "E", 50.5, SENT, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Sophie", "F", 5.97, RECEIVED, HIGH_PRIORITY);
    insert_email_at_head(folder_2, "Grace", "G", 30.28, SENT, NORMAL_PRIORITY);
    insert_email_at_head(folder_2, "Andrew", "H", 5.5, RECEIVED, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Holly", "I", 25.25, RECEIVED, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Ibby", "J", 15.25, RECEIVED, NORMAL_PRIORITY);
    insert_email_at_head(folder_2, "Bella", "K", 15.25, RECEIVED, LOW_PRIORITY);
    
    printf("\n*****16. Printing Folder 2...*****\n");
    print_emails(folder_2);
    
    //STAGE 2

    //printf("\n*****17. Deleting LOW_PRIORITY emails from Folder 2...*****\n");
    //delete_email_of_priority(folder_2, LOW_PRIORITY);
    
    //printf("\n*****18. Printing Folder 2...*****\n");
    //print_emails(folder_2);
  
    /*
    printf("\n*****19. Merging Folder 1 and Folder 2...*****\n");
    struct folder *merged_folder = merge_folders("Merged Folder", folder_1, 
                                                                  folder_2);
    
    printf("\n*****20. Printing merged folder...*****\n");
    print_emails(merged_folder);
   
    printf("\n*****21. Split merged folder into inbox, sent, draft...*****\n");
    struct folder *inbox = create_folder("inbox");
    struct folder *sent = create_folder("sent");
    struct folder *draft = create_folder("draft");
    split_folder(merged_folder, inbox, sent, draft);
    
    printf("\n*****22. Printing inbox...*****\n");
    print_emails(inbox);

    printf("\n*****23. Printing sent...*****\n");
    print_emails(sent);

    printf("\n*****24. Printing draft...*****\n");
    print_emails(draft);
    */
    
    // STAGE 1: comment this section out once you implement merge
    printf("\n*****Clearing stage 1 folders folder_1 and folder_2...*****\n");

    clear_folder(folder_1);
    clear_folder(folder_2);

    // STAGE 2 merge:  comment this section out once you implement split
    //printf("\n*****Clearing stage 2 merged_folder...*****\n");
    //clear_folder(merged_folder);

    // STAGE 2 
    //printf("\n*****Clearing inbox, sent, draft (freeing all memory)...*****\n");
    //clear_folder(inbox);
    //clear_folder(sent);
    //clear_folder(draft);
   
    
    return 0;
}
// email_management_system.c
// Tammy Zhong
// Implementation file for email management system functions
// Modified by Angela Finlayson
// (TODO)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "email_management_system.h"

// Function prototypes 
// ADD YOUR OWN FUNCTION PROTOTYPES HERE 
struct email *create_email(char sender[], char subject[], double size, 
    enum email_type type, enum priority_type priority);
void free_list(struct email *head);
struct email *search_and_delete_all(struct email *head, enum priority_type value);
// Function definitions

// Creates a new empty email folder 
//(TODO)
struct folder *create_folder(char name[]) {
    struct folder *new_folder = malloc(sizeof(struct folder));
    strcpy(new_folder->name, name);
    new_folder->emails = NULL;
    return new_folder;
}

// Add a new email to the folder at the head 
// Similar to previous lecture code insert_head
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[], 
    char subject[], double size, enum email_type type, 
    enum priority_type priority) {
    struct email *new_email = create_email(sender, subject, size, type, priority);
    new_email->next = email_folder->emails; //head
    email_folder->emails = new_email;
}

// Given a string, look for email where the subject is this string 
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]) {
    struct email *curr = email_folder->emails;
    while (curr != NULL) {
        if (strcmp(curr->subject, subject) == 0) {
            return curr;
        } 
        curr = curr->next;
    }
    return NULL;
}

// Removes all emails from a folder
// clears and delete associated memory of folder and emails in it
void clear_folder(struct folder *email_folder) {
    free_list(email_folder->emails);
    free(email_folder);
}

// Remove email(s) with certain priority (e.g. filter out low priority emails)
void delete_email_of_priority(struct folder *email_folder, enum priority_type priority) {
    email_folder->emails = search_and_delete_all(email_folder->emails, priority);
}

// Append emails from folder_2 after folder_1 and returns them in the one folder
// Assumption made here assumes not same subject email in folder_1 and folder_2
struct folder *merge_folders(char merged_folder_name[], 
    struct folder *folder_1, struct folder *folder_2) {
    struct folder *merged_folder = create_folder(merged_folder_name);
    if (folder_1->emails != NULL) {
        merged_folder->emails = folder_1->emails;
        //find the last email node in folder 1
        struct email *curr = folder_1->emails;
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = folder_2->emails;  
    } else {
        merged_folder->emails = folder_2->emails;
    }
    free(folder_1);
    free(folder_2);
    return merged_folder;
}


void insert_at_end_of_folder(struct folder *the_folder, struct email *the_email) {
    struct email *curr = the_folder->emails;
    if (curr == NULL) {
        the_folder->emails = the_email;
    } else {
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = the_email;
    }
}

// Split emails in a given folder into 3 folders based on email type 
// keeping original ordering
// assumption: received, sent and draft are folders already malloc-ed and initialised
void split_folder(struct folder *given_folder, 
    struct folder *received, struct folder *sent, struct folder *draft) {
    
    struct email *curr = given_folder->emails; //head
    while (curr != NULL) {
        //Remove curr from the folder list
        //Move it to the appropriate other folder
        given_folder->emails = curr->next;
        curr->next = NULL;

        if (curr->type == RECEIVED) {
            insert_at_end_of_folder(received, curr);
        } else if (curr->type == SENT) {
            insert_at_end_of_folder(sent, curr);
        } else {
            insert_at_end_of_folder(draft, curr);
        }
        curr = given_folder->emails;
    }    
    free(given_folder);
}

// returns number of emails in a given folder 
// (EXTENSION TODO: Improve this)
int count_emails(struct folder *email_folder) {
    int counter = 0;
    struct email *current = email_folder->emails;
    while (current != NULL) {
        counter++;
        current = current->next;
    }
    return counter;
}


// Your functions here (include function comments):

struct email *create_email(char sender[], char subject[], double size, 
    enum email_type type, enum priority_type priority) {

        struct email *new_email = malloc(sizeof(struct email));
        strcpy(new_email->sender, sender);
        strcpy(new_email->subject, subject);
        new_email->size = size;
        new_email->type = type;
        new_email->priority = priority;
        new_email->next = NULL;
        return new_email;
}

void free_list(struct email *head) {
     struct email *current = head;
     while (current != NULL) {
        head = head->next;
        free(current);
        current = head;
     }
}

// Modify to delete all occurrences of value, not just the first one.
struct email *search_and_delete_all(struct email *head, enum priority_type value) {
    struct email *prev = NULL;
    struct email *curr = head;

    // Find email to delete
    while (curr != NULL) {
        if (curr->priority == value){
            if (prev != NULL) {
                prev->next = curr->next; 
                free(curr);
                curr = prev->next;
            } else {
                head = curr->next;
                free(curr);
                curr = head;
            }
        } else {
            prev = curr;
            curr = curr->next;
        }
    }
     
    return head;
}


////////////////////////////////////////////////////////////////////////////////
//                              Provided Code                              //
////////////////////////////////////////////////////////////////////////////////

// Print/display all emails in folder 
// Similar to previous lecture code print_list
void print_emails(struct folder *email_folder) {
    printf("Folder Name: %s\n", email_folder->name);
    struct email *current = email_folder->emails;

    if (current == NULL) {
        printf("There are no emails in this folder.\n");
    }

    while (current != NULL) {
        printf("---\n");
        print_single_email(current);
        current = current->next;
    }
}

// Prints out details of a single email 
void print_single_email(struct email *email) {
    if (email == NULL) {
        printf("Email is NULL.\n");
        return;
    }

    printf("Sender: %s\n", email->sender);
    printf("Subject: %s\n", email->subject);
    printf("Size: %lfmb\n", email->size);
    printf("Type: ");
    if (email->type == RECEIVED) {
        printf("RECEIVED\n");
    } else if (email->type == SENT) {
        printf("SENT\n");
    } else if (email->type == DRAFT) {
        printf("DRAFT\n");
    } else {
        printf("UNKNOWN\n");
    }
    printf("Priority: ");
    if (email->priority == HIGH_PRIORITY) {
        printf("HIGH_PRIORITY\n");
    } else if (email->priority == NORMAL_PRIORITY) {
        printf("NORMAL_PRIORITY\n");
    } else if (email->priority == LOW_PRIORITY) {
        printf("LOW_PRIORITY\n");
    } else {
        printf("UNKNOWN\n");
    }
}
// email_management_system.h
// Tammy Zhong
// Header file for email management system functions
// Modified by Angela Finlayson
// (Provided)

// #defines
#define MAX_LEN 100

// enum definition
enum email_type {
    DRAFT,
    RECEIVED,
    SENT,
};

enum priority_type {
    LOW_PRIORITY,
    NORMAL_PRIORITY,
    HIGH_PRIORITY
};

// struct definitions
struct folder {
    char name[MAX_LEN];
    //int num_emails; // to use later :)
    struct email *emails;
};

struct email {
    char sender[MAX_LEN];
    char subject[MAX_LEN];
    double size;
    enum email_type type;
    enum priority_type priority; 
    struct email *next;
};

// Creates a new empty email folder 
// (TODO)
struct folder *create_folder(char name[]);

// Add a new email to the folder at the head 
// (TODO)
void insert_email_at_head(struct folder *email_folder, char sender[], 
    char subject[], double size, enum email_type type, enum priority_type priority);

// Given a string, look for email where the subject is this string 
// (TODO)
struct email *search_email(struct folder *email_folder, char subject[]);

// Remove email(s) with certain priority (e.g. filter out low priority emails)
// (TODO)
void delete_email_of_priority(struct folder *email_folder, enum priority_type priority);

// Append emails from folder_2 after folder_1 and returns them in the one folder
// (TODO)
struct folder *merge_folders(char merged_folder_name[], 
    struct folder *folder_1, struct folder *folder_2);

// Split emails in a given folder into 3 folders based on email type 
// keeping original ordering
// (TODO)
void split_folder(struct folder *given_folder, 
    struct folder *received, struct folder *sent, struct folder *draft);

// Removes all emails from a folder
// (TODO)
void clear_folder(struct folder *email_folder); // leak-check demo

// Returns number of emails in a given folder 
// (PROVIDED - TODO CAN BE IMPROVED)
int count_emails(struct folder *email_folder);

// CHALLENGE at home for fun: Sort emails in folder based on size
// (slightly out of scope of COMP1511 - touches on sorting algorithms)
// (TODO)
void sort_emails(struct folder *email_folder);

// Prints out details of a single email 
// (PROVIDED)
void print_single_email(struct email *email);

// Print/display all emails in folder 
// (PROVIDED)
void print_emails(struct folder *email_folder);
// main.c
// Angela Finlayson 
// Uses the email management system to allow a user to create a folder
// and and emails (with not content) to it.
// Only works for 1 folder... 
// (Provided)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "email_management_system.h"

void print_instructions(void);
void handle_search(struct folder *current_folder);
void handle_add_email(struct folder *current_folder);
void trim_newline(char str[]);

int main(void) {
    struct folder *current_folder = create_folder("inbox");
    char command;

    print_instructions();
    printf("\nEnter command: ");
    while (scanf(" %c", &command) == 1 && command != 'q') {
        if (command == 'a') {
            handle_add_email(current_folder);
        } else if (command == 'p') {
            print_emails(current_folder);
        } else if (command == 'n') { 
            printf("Number of emails: %d\n", count_emails(current_folder));
        } else if (command == 's') {
            handle_search(current_folder);
        } else {
            printf("Unknown command. Try again.\n");
        }
        printf("\nEnter command: ");
    }
    printf("Exiting\n");
    clear_folder(current_folder);
    return 0;
}

// Prints instructions to users
void print_instructions(void) {
    printf("Email Manager - Command Interface\n");
    printf("Commands:\n");
    printf("a - Add a new email to the folder\n");
    printf("p - Print all emails in the folder\n");
    printf("n - Count emails in the folder\n");
    printf("s - Search for an email by subject\n");
    printf("q - Quit\n");
}

// Handles adding a new email to the folder
// Assumes no error checking needed
void handle_add_email(struct folder *current_folder) {
    char sender[MAX_LEN] = "";
    char subject[MAX_LEN]= "";
    double size;
    int priority, type;

    // Remove whitespace from buffer
    int whitespace_ch;
    while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}

    printf("Enter sender: ");
    fgets(sender, MAX_LEN, stdin);
    trim_newline(sender);
    printf("Enter subject: ");
    fgets(subject, MAX_LEN, stdin);
    trim_newline(subject);
    printf("Enter size (in MB): ");
    scanf("%lf", &size);
    printf("Enter email type (0 for DRAFT, 1 for RECEIVED, 2 for SENT): ");
    scanf("%d", &type);
    printf("Enter priority (0 for LOW, 1 for NORMAL, 3 for HIGH): ");
    scanf("%d", &priority);
    insert_email_at_head(current_folder, sender, subject, size, type, priority);
    printf("Email added to folder '%s'.\n", current_folder->name);
}

// Handles searching for an email by subject in the folder
void handle_search(struct folder *current_folder) {
    char subject[MAX_LEN];
    
    // Remove whitespace from buffer
    int whitespace_ch;
    while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {}

    printf("Enter subject to search: ");
    fgets(subject, MAX_LEN, stdin);
    trim_newline(subject);
    
    struct email *found_email = search_email(current_folder, subject);
    if (found_email != NULL) {
        printf("Email found:\n");
        print_single_email(found_email);
    } else {
        printf("No email with subject '%s' found.\n", subject);
    }
}

// removes trailing newline character
void trim_newline(char str[]) {
    int len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';  
    }
}
// main.c
// Tammy Zhong
// Program to test email management system functions
// Modified by Angela Finlayson 9/11/2025
// (Provided)


#include <stdio.h>
#include <stdlib.h>

#include "email_management_system.h"

int main(void) {
    // STAGE 1
    printf("*****1. Creating Folder 1...*****\n");
    struct folder *folder_1 = create_folder("Folder 1");

    printf("\n*****2. Number of emails in Empty Folder 1:*****\n");
    printf("%d\n", count_emails(folder_1));

    printf("\n*****3. Printing Empty Folder 1...*****\n");
    print_emails(folder_1);

    printf("\n*****4. Inserting emails in Empty Folder 1 *****\n");
 
    insert_email_at_head(folder_1, "Jon", "Surfing Trip", 10.2, RECEIVED, HIGH_PRIORITY);
    insert_email_at_head(folder_1, "Tim", "Basketball", 20.44, DRAFT, NORMAL_PRIORITY);
    insert_email_at_head(folder_1, "Brianna", "Tax Return", 150.0, SENT, LOW_PRIORITY);
    insert_email_at_head(folder_1, "Tammy", "D&D Night", 90.2, SENT, NORMAL_PRIORITY);
    insert_email_at_head(folder_1, "Sofia", "COMP1511 Exam", 90.0, DRAFT, LOW_PRIORITY);

    printf("\n*****5. Printing Folder 1...*****\n");
    print_emails(folder_1); 

    printf("\n*****6. Number of emails in Folder 1: *****\n");
    printf("%d\n", count_emails(folder_1));
   
    
    printf("\n*****7. Look for email with subject 'Surfing Trip'...*****\n");
    struct email *email_found = search_email(folder_1, "Surfing Trip");
    
    printf("\n*****8. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****9. Look for email with subject 'D&D Night'...*****\n");
    email_found = search_email(folder_1, "D&D Night");
    
    printf("\n*****10. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****11. Look for email with subject 'COMP1511 Exam'...*****\n");
    email_found = search_email(folder_1, "COMP1511 Exam");
    
    printf("\n*****12. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);

    printf("\n*****13. Look for email with subject 'Holiday'...*****\n");
    email_found = search_email(folder_1, "Holiday");
    
    printf("\n*****14. Printing email found in Folder 1 ...*****\n");
    print_single_email(email_found);
    

    printf("\n*****15. Creating Folder 2... and inserting emails...*****\n");
    struct folder *folder_2 = create_folder("Folder 2");
    insert_email_at_head(folder_2, "Liam", "E", 50.5, SENT, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Sophie", "F", 5.97, RECEIVED, HIGH_PRIORITY);
    insert_email_at_head(folder_2, "Grace", "G", 30.28, SENT, NORMAL_PRIORITY);
    insert_email_at_head(folder_2, "Andrew", "H", 5.5, RECEIVED, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Holly", "I", 25.25, RECEIVED, LOW_PRIORITY);
    insert_email_at_head(folder_2, "Ibby", "J", 15.25, RECEIVED, NORMAL_PRIORITY);
    insert_email_at_head(folder_2, "Bella", "K", 15.25, RECEIVED, LOW_PRIORITY);
    
    printf("\n*****16. Printing Folder 2...*****\n");
    print_emails(folder_2);
    
    //STAGE 2

    printf("\n*****17. Deleting LOW_PRIORITY emails from Folder 2...*****\n");
    delete_email_of_priority(folder_2, LOW_PRIORITY);
    
    printf("\n*****18. Printing Folder 2...*****\n");
    print_emails(folder_2);
  
    
    printf("\n*****19. Merging Folder 1 and Folder 2...*****\n");
    struct folder *merged_folder = merge_folders("Merged Folder", folder_1, 
                                                                  folder_2);
    
    printf("\n*****20. Printing merged folder...*****\n");
    print_emails(merged_folder);
   
    // printf("\n*****21. Split merged folder into inbox, sent, draft...*****\n");
    //Note: received are being put in the inbox
    struct folder *inbox = create_folder("inbox");
    struct folder *sent = create_folder("sent");
    struct folder *draft = create_folder("draft");
    split_folder(merged_folder, inbox, sent, draft);
    
    printf("\n*****22. Printing inbox...*****\n");
    print_emails(inbox);

    printf("\n*****23. Printing sent...*****\n");
    print_emails(sent);

    printf("\n*****24. Printing draft...*****\n");
    print_emails(draft);
    
    
    // STAGE 1: comment this section out once you implement merge
    //printf("\n*****Clearing stage 1 folders folder_1 and folder_2...*****\n");

    //clear_folder(folder_1);
    //clear_folder(folder_2);

    // STAGE 2 merge:  comment this section out once you implement split
    //printf("\n*****Clearing stage 2 merged_folder...*****\n");
    //clear_folder(merged_folder);

    // STAGE 2 
    printf("\n*****Clearing inbox, sent, draft (freeing all memory)...*****\n");
    clear_folder(inbox);
    clear_folder(sent);
    clear_folder(draft);
   
    
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};

// You want to traverse a linked list and 
// find the largest odd number in it 
// For example, 3, 4, 5, 6, 1
// Would return 5
// Assume all values >= 0
int largest_odd(struct node *head) { 
    int max = -1;
    struct node *curr = head;
    while (curr != NULL) {
        if (curr->data > max && curr->data % 2 != 0) {
            max = curr->data;
        }
        curr = curr->next;
    }
    return max;
}

struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    printf("%d\n", largest_odd(head));

    return 0;
}


// DO NOT CHANGE THIS FUNCTION

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct node {
    struct node *next;
    char         data;
};

// List 1: 'a' 'b' 'c' (3 nodes)
// List 2: 'd' 'e' 'f' 'g' 'h' (5 nodes)
// difference function should return 2

int difference(struct node *head1, struct node *head2) {
    return 42;
}


struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    // create two linked lists from command line arguments
    int dash_arg = argc - 1;
    while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
        dash_arg = dash_arg - 1;
    }
    struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
    struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);

    int diff = difference(head1, head2);
    printf("%d\n", diff);

    return 0;
}


// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = strings[i][0];
        head = n;
    }
    return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};

// You want to traverse a linked list and 
// find the largest odd number in it 
// For example, 3, 4, 5, 6, 1 
// Would return 5
int largest_odd(struct node *head) {    
    return -1;
}

struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    printf("%d\n", largest(head));

    return 0;
}


// DO NOT CHANGE THIS FUNCTION

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct node {
    struct node *next;
    char          data;
};

// List 1: 'a' 'b' 'c' (3 nodes)
// List 2: 'd' 'e' 'f' 'g' 'h' (5 nodes)
// difference function should return 2


int difference(struct node *head1, struct node *head2) {
  
   return -1;
}


struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    // create two linked lists from command line arguments
    int dash_arg = argc - 1;
    while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
        dash_arg = dash_arg - 1;
    }
    struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
    struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);

    int diff = difference(head1, head2);
    printf("%d\n", diff);

    return 0;
}


// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = strings[i][0];
        head = n;
    }
    return head;
}
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

// Return the number of elements divisible by 3
int count_multiples(int size, int arr[]) {
    return -1;
}

int main(void) {

    // Only your function is called during testing
    // Any changes in this main function will not
    // be used in testing

    int array1[SIZE] = {9, -3, 1, 5, 12};

    printf("%d are multiples of 3\n", count_multiples(SIZE, array1));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

struct steps {
    int number;
    char direction;
};

// Return the total of steps in the given direction
int step_count(char dir, int size, struct steps array[]) {
    return -42;
}

// This is a simple main function which could be used
// to test your step_count function.
// It will not be marked.
// Only your step_count function will be marked.

#define TEST_ARRAY_SIZE 5

int main(void) {
    struct steps test_array[TEST_ARRAY_SIZE] = {
        { .number = 3, .direction = 'l'},
        { .number = 5, .direction = 'l'},
        { .number = 2, .direction = 'l'},
        { .number = 13, .direction = 'd'},
        { .number = 30, .direction = 'l'}
    };
    printf("%d\n", step_count('l', TEST_ARRAY_SIZE, test_array));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

// Return the number of elements divisible by 3
int count_multiples(int size, int arr[]) {
    return -1;
}

int main(void) {

    // Only your function is called during testing
    // Any changes in this main function will not
    // be used in testing

    int array1[SIZE] = {9, -3, 1, 5, 2};

    printf("%d are multiples of 3\n", count_multiples(SIZE, array1));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

struct steps {
    int number;
    char direction;
};

// Return the total of steps in the given direction
int step_count(char dir, int size, struct steps array[]) {
   return -1;
}

// This is a simple main function which could be used
// to test your step_count function.
// It will not be marked.
// Only your step_count function will be marked.

#define TEST_ARRAY_SIZE 5

int main(void) {
    struct steps test_array[TEST_ARRAY_SIZE] = {
        { .number = 3, .direction = 'l'},
        { .number = 5, .direction = 'l'},
        { .number = 2, .direction = 'u'},
        { .number = 13, .direction = 'd'},
        { .number = 30, .direction = 'l'}
    };
    printf("%d\n", step_count('l', TEST_ARRAY_SIZE, test_array));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};


// Delete the first instance of a duplicate in the given list 
// 1->3->5->3->1->X 
// should return 
// 1->3->5->3->X

// 4->5->1->5->5->X
// 4->5->1->5->X
struct node *delete_duplicate(struct node *head) {
    return head;
}


// DO NOT CHANGE CODE BELOW

struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);


int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = NULL;
    if (argc > 1) {
        // list has elements
        head = strings_to_list(argc - 1, &argv[1]);
    }

    struct node *new_head = delete_duplicate(head);
    print_list(new_head);

    return 0;
}


// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct node {
  struct node *next;
  char data;
};

 
// duplicate should duplicate every node in the list, inserting the new
// node after the original node
// a z c d d..... a a z z c c d d d d
struct node *duplicate(struct node *head) {
	struct node *curr = head;
	while (curr != NULL) {
		struct node *new_node = malloc(sizeof(struct node));
		new_node->data = curr->data;
		new_node->next = curr->next;
		curr->next = new_node;

		curr = curr->next->next;
	}
	return head;
}

////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
	// create linked list from command line arguments
	struct node *head = strings_to_list(argc - 1, &argv[1]);

	// If you're getting an error here,
	// you have returned an uninitialized value
	struct node *new_head = duplicate(head);
	print_list(new_head);

	return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
	struct node *head = NULL;
  	for (int i = len - 1; i >= 0; i = i - 1) {
		struct node *n = malloc(sizeof(struct node));
		assert(n != NULL);
		n->next = head;
		n->data = strings[i][0];
		head = n;
  	}
  	return head;
}

// print linked list
void print_list(struct node *head) {
	printf("[");

  	for (struct node *n = head; n != NULL; n = n->next) {
    	// If you're getting an error here,
   	 	// you have returned an invalid list
    	printf("%c", n->data);
    	if (n->next != NULL) {
      		printf(", ");
    	}
  	}
  	printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};


// Delete the first instance of a duplicate in the given list 
// 1->3->5->3->1->X 
// should return 
// 1->3->5->3->X
struct node *delete_duplicate(struct node *head) {

    return NULL;
}


// DO NOT CHANGE CODE BELOW

struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);


int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = NULL;
    if (argc > 1) {
        // list has elements
        head = strings_to_list(argc - 1, &argv[1]);
    }

    struct node *new_head = delete_duplicate(head);
    print_list(new_head);

    return 0;
}


// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct node {
  struct node *next;
  char data;
};

// duplicate should duplicate every node in the list, inserting the new
// node after the original node
// a z c d d..... a a z z c c d d d d
struct node *duplicate(struct node *head) {
    return head;
}

////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
	// create linked list from command line arguments
	struct node *head = strings_to_list(argc - 1, &argv[1]);

	// If you're getting an error here,
	// you have returned an uninitialized value
	struct node *new_head = duplicate(head);
	print_list(new_head);

	return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
	struct node *head = NULL;
  	for (int i = len - 1; i >= 0; i = i - 1) {
		struct node *n = malloc(sizeof(struct node));
		assert(n != NULL);
		n->next = head;
		n->data = strings[i][0];
		head = n;
  	}
  	return head;
}

// print linked list
void print_list(struct node *head) {
	printf("[");

  	for (struct node *n = head; n != NULL; n = n->next) {
    	// If you're getting an error here,
   	 	// you have returned an invalid list
    	printf("%c", n->data);
    	if (n->next != NULL) {
      		printf(", ");
    	}
  	}
  	printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>

#define NUM_ROW 4
#define NUM_COL 4

// sum_min should return the sum of
// the minimum values in each row of the array
int min_row_sum(int size, int array[NUM_ROW][NUM_COL]) {
    return -1;
}

// This is a simple main function which could be used
// to test your sum_min function.
// It will not be marked.
// Only your sum_min function will be marked.
int main(void) {
    int test_array[NUM_ROW][NUM_COL] = {
        {1, 2, 3, 4}, {4, 2, 2, 0}, {9, 9, 9, 9}, {5, 2, 5, 5}};

    int result = min_row_sum(NUM_COL, test_array);
    printf("%d\n", result);

    return 0;
}
#include <stdio.h>

#define MAX_SIZE 10000

// reads integers into an array from terminal until a number is entered 
// which when multiplied by another number previously entered 
// results in 56. 
// ./array_multiplied
// 2
// 3
// 28
// 28 * 2 = 56

// ./array_multiplied
// 99
// 4
// 1
// 7
// 56
// 56 * 1 = 56

int main(void) {

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

#define NUM_ROW 4
#define NUM_COL 4

// sum_min should return the sum of
// the minimum values in each row of the array
int min_row_sum(int size, int array[NUM_ROW][NUM_COL]) {
    return 42;
}

// This is a simple main function which could be used
// to test your sum_min function.
// It will not be marked.
// Only your sum_min function will be marked.
int main(void) {
    int test_array[NUM_ROW][NUM_COL] = {
        {1, 2, 3, 4}, {4, 2, 2, 0}, {9, 9, 9, 9}, {5, 2, 5, 5}};

    int result = min_row_sum(NUM_COL, test_array);
    printf("%d\n", result);

    return 0;
}
#include <stdio.h>

#define MAX_SIZE 10000

// reads integers into an array from terminal until a number is entered 
// which when multiplied by another number previously entered 
// results in 56. 
// ./array_multiplied
// 2
// 3
// 28
// 28 * 2 = 56

// ./array_multiplied
// 99
// 4
// 1
// 7
// 56
// 56 * 1 = 56